Look for the network connection in which you are connecting and turn on the network Connection Sharing feature of the network connection.
Note: To set up the project for administrator rights start. Otherwise enablesharing will fail.
#include <Windows.h> #include <NetCon.h> #include <locale> #include <stdio.h> #pragma comment ( LIB, "Iphlpapi.lib") #pragma comment (lib, "Rpcrt4.lib")//guid//Enable, disable NIC #pragma comment (lib, "Ole32.lib") int main (int ARGC, char* argv[]) {Inetconnectionmanager *pmanager=null; Inetconnection *pconnection=null; Ienumnetconnection *penum=null; ULONG celtfetched; Inetsharingmanager *pnetsharingmanager=null; Inetconnectionprops *pprops=null; Inetsharingconfiguration *pconfiguration=null; Netcon_properties* Properties=null; Netcon_mediatype mediatype; SetLocale (Lc_ctype, ""); CoInitialize (NULL); CoCreateInstance (Clsid_connectionmanager, NULL, Clsctx_server, Iid_inetconnectionmanager, (void**) &pManager); if (Pmanager = = NULL) {printf ("Get Accept failed, error:%d\n", GetLastError ()); return 0; } pmanager->enumconnections (Ncme_default, &penum); while (Penum->next (1, &pconnection, &celtfetched) = = S_OK) {pconnection->getproperties (&properties); if (Properties->status = = ncs_connected) {if (Properties->dwcharacter & nccf_incoming_only = = 1 {wprintf (L "\"%s\ "is in a connected state, but this connection cannot be shared!) Make sure you have at least 2 network connections! \ n ", properties->pszwname); Break } CoCreateInstance (Clsid_netsharingmanager, NULL, Clsctx_server, Iid_inetsharingmanager, (void**) &pNetShari Ngmanager); if (Pnetsharingmanager = = NULL) {printf ("Get Accept failed, error:%d\n", GetLastError ()); Break } wprintf (L "Discovery \"%s\ "is in the connected state. Try to turn on sharing ... \ n ", properties->pszwname); if (properties->mediatype >= ncm_direct && properties->mediatype <=ncm_pppoe) { Pnetsharingmanager->get_inetsharingconfigurationforinetconnection (pconnection, &pConfiguratio n); if (pconfiguration && SUCCeeded (pconfiguration->enablesharing (icssharingtype_public))) {wprintf (L "successfully set \"%s\ "is Shared status! \ n ", properties->pszwname); Break }} wprintf (L "Settings \"%s\ "Shared state failed! Error:%d\n ", Properties->pszwname,getlasterror ()); }} if (Pmanager) pmanager->release (); if (pconnection) pconnection->release (); if (penum) penum->release (); if (Pnetsharingmanager) pnetsharingmanager->release (); if (pconfiguration) pconfiguration->release (); CoUninitialize (); return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Looking for network connections in a connection