国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C++ > 正文

C++實現獲取IP、子網掩碼、網關、DNS等本機網絡參數的方法

2020-01-26 15:27:22
字體:
來源:轉載
供稿:網友

本文以一個完整實例形式介紹了C++實現獲取IP、子網掩碼、網關、DNS等本機網絡參數的方法,供大家參考,具體的完整實例如下:

#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"); //獲取網卡名稱 網卡名稱,網卡別名 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(RegOpenKeyEx(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(strstr((char*)szData, "ethernet") != NULL)//  判斷是不是以太網卡   {   dwBufSize = 256;   if(RegQueryValueEx(hSubKey, "DriverDesc", 0, &dwDataType, szData, &dwBufSize) == ERROR_SUCCESS)   {    strAdapterName = (LPCTSTR)szData;    dwBufSize = 256;    if(RegQueryValueEx(hSubKey, "NetCfgInstanceID", 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 = "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->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->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); //獲取DNS服務器信息 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;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 大同县| 陆川县| 普兰县| 桦甸市| 玛纳斯县| 清流县| 永善县| 加查县| 定西市| 贺兰县| 永昌县| 荥经县| 庆云县| 灵丘县| 彭山县| 菏泽市| 板桥市| 开封市| 曲松县| 淮安市| 皮山县| 嘉祥县| 惠水县| 内丘县| 福建省| 大名县| 建德市| 通山县| 三亚市| 开远市| 虎林市| 宾川县| 天津市| 龙江县| 乌兰察布市| 渭源县| 大丰市| 揭阳市| 冀州市| 庆元县| 镇雄县|