In this paper, a complete example of the implementation of C + + to obtain IP, subnet mask, gateway, DNS and other local network parameters of the method for your reference, the specific complete example is as follows:
#pragma comment (lib, "Ws2_32.lib") #include <Iphlpapi.h> #pragma comment (lib, "Iphlpapi.lib") using namespace std;
typedef struct TAGNETWORKCFG {char szip[18];
Char sznetmask[18];
Char szgateway[18];
Char szdns1[18];
Char szdns2[18];
}networkcfg;
BOOL Getnetworkcfg (networkcfg *cfg) {log_printf ("Get network config");
Get the network card name network card name, network card alias string Stradaptername,stradapteralias;
Hkey hkey, Hsubkey, Hndiintkey; if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, system\\currentcontrolset\\control\\class\\{)
4D36E972-E325-11CE-BFC1-08002BE10318} ", 0, Key_read, &hkey)!= error_success) return FALSE;
DWORD dwindex = 0;
DWORD dwbufsize = 256;
DWORD Dwdatatype;
Char szsubkey[256];
unsigned char szdata[256]; while (RegEnumKeyEx (hkey, dwindex++, Szsubkey, &dwbufsize, NULL, NULL, NULL, NULL) = = ERROR_SUCCESS) {if (Regopenkeye X (hkey, Szsubkey, 0, key_read, &hsubkey) = = ERROR_SUCCESS) {if RegOpenKeyEx (Hsubkey, "Ndi\\interfaces", 0, KEY _read, &hndiintkey) = = ERROR_success) {dwbufsize = 256; if (RegQueryValueEx (Hndiintkey, "LowerRange", 0, &dwdatatype, szdata, &dwbufsize) = = ERROR_SUCCESS) {if (Strst
R ((char*) szdata, "Ethernet")!= NULL)//To determine whether Ethernet card {dwbufsize = 256; if (RegQueryValueEx (Hsubkey, "DriverDesc", 0, &dwdatatype, szdata, &dwbufsize) = = ERROR_SUCCESS) {StrAdapte
Rname = (LPCTSTR) szdata;
Dwbufsize = 256; if (RegQueryValueEx (Hsubkey, "Netcfginstanceid", 0, &dwdatatype, szdata, &dwbufsize) = = ERROR_SUCCESS) {St
Radapteralias = (LPCTSTR) szdata;
Break
}}} RegCloseKey (Hndiintkey);
} regclosekey (Hsubkey);
} dwbufsize = 256;
}/* End of While/* RegCloseKey (HKEY);
if (Stradaptername.empty () | | | Stradapteralias.empty ()) {log_printf ("Failed to get network config");
return false;
} string strkeyname = "System\\currentcontrolset\\services\\tcpip\\parameters\\interfaces\\";
Strkeyname + = Stradapteralias;
if (RegOpenKeyEx (HKEY_LOCAL_MACHINE,Strkeyname.c_str (), 0, Key_read, &hkey)!= error_success) return FALSE;
Dwbufsize = 256; if (RegQueryValueEx (hkey, "dhcpipaddress", 0,&dwdatatype, Szdata, &dwbufsize) = = ERROR_SUCCESS) strcpy (cfg-
>szip, (LPCTSTR) szdata); else{if (RegQueryValueEx (hkey, "IPAddress", 0, &dwdatatype, szdata, &dwbufsize) = = ERROR_SUCCESS) strcpy (CFG-&G
T;szip, (LPCTSTR) szdata);
} dwbufsize = 256; if (RegQueryValueEx (hkey, "Dhcpsubnetmask", 0, &dwdatatype, szdata, &dwbufsize) = = ERROR_SUCCESS) strcpy (cfg-
>sznetmask, (LPCTSTR) szdata); else {if (RegQueryValueEx (hkey, "SubnetMask", 0, &dwdatatype, szdata, &dwbufsize) = ERROR_SUCCESS) trcpy (cfg-&
Gt;sznetmask, (LPCTSTR) szdata);
} dwbufsize = 256; if (RegQueryValueEx (hkey, "DhcpDefaultGateway", 0, &dwdatatype, szdata, &dwbufsize) = = ERROR_SUCCESS) strcpy (
Cfg->szgateway, (LPCTSTR) szdata); else {if (RegQueryValueEx (hkey, "DefaultGateway", 0, &dwdatatype, szdata, &dwbufsize) = = Error_SUCCESS) strcpy (Cfg->szgateway, (LPCSTR) szdata);
} regclosekey (HKEY);
Get DNS server information fixed_info *FI = (Fixed_info *) GlobalAlloc (gptr,sizeof (fixed_info));
ULONG Uloutbuflen = sizeof (Fixed_info);
DWORD ret =:: GetNetworkParams (FI, &uloutbuflen);
if (ret!= error_success) {GlobalFree (FI);
fi = (Fixed_info *) GlobalAlloc (gptr, Uloutbuflen);
RET =:: GetNetworkParams (FI, &uloutbuflen);
if (ret!= error_success) {log_printf ("Get Dns server failed");
return false;
} strcpy (cfg->szdns1,fi->dnsserverlist.ipaddress.string);
Ip_addr_string *pipaddr = fi->dnsserverlist.next;
if (pipaddr!= NULL) strcpy (Cfg->szdns2, pipaddr->ipaddress.string);
return false; }