# 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 "); // Obtain the nic name and Nic alias String strAdapterName, strAdapterAlias; HKEY hKey, hSubKey, hNdiIntKey; If (RegOpenKeyEx (HKEY_LOCAL_MACHINE, "SystemCurrentControlSetControlClass {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) = ERROR_SUCCESS) { If (RegOpenKeyEx (hKey, szSubKey, 0, KEY_READ, & hSubKey) = ERROR_SUCCESS) { If (RegOpenKeyEx (hSubKey, "NdiInterfaces", 0, KEY_READ, & hNdiIntKey) = ERROR_SUCCESS) { DwBufSize = 256; If (RegQueryValueEx (hNdiIntKey, "LowerRange", 0, & dwDataType, szData, & dwBufSize) = ERROR_SUCCESS) { If (strstr (char *) szData, "ethernet ")! = NULL) // determines if it is an Ethernet card { DwBufSize = 256; If (RegQueryValueEx (hSubKey, "DriverDesc", 0, & dwDataType, szData, & dwBufSize) = ERROR_SUCCESS) { StrAdapterName = (LPCTSTR) szData; DwBufSize = 256; If (RegQueryValueEx (hSubKey, "netmask instanceid", 0, & dwDataType, szData, & dwBufSize) = ERROR_SUCCESS) { StrAdapterAlias = (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 = "SYSTEMCurrentControlSetServicesTcpipParametersInterfaces "; 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-> 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) Strcpy (cfg-> szNetmask, (LPCTSTR) szData ); } DwBufSize = 256; If (RegQueryValueEx (hKey, "dhcpdefagateway Gateway", 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 ); // Obtain 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; } |