C++實(shí)現(xiàn)獲取IP、子網(wǎng)掩碼、網(wǎng)關(guān)、DNS等本機(jī)網(wǎng)絡(luò)參數(shù)的方法
本文以一個(gè)完整實(shí)例形式介紹了C++實(shí)現(xiàn)獲取IP、子網(wǎng)掩碼、網(wǎng)關(guān)、DNS等本機(jī)網(wǎng)絡(luò)參數(shù)的方法,供大家參考,具體的完整實(shí)例如下:
#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"); //獲取網(wǎng)卡名稱 網(wǎng)卡名稱,網(wǎng)卡別名 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)// 判斷是不是以太網(wǎng)卡 { 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服務(wù)器信息 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; }
- Shell腳本獲取本地網(wǎng)卡IP、mac地址、子網(wǎng)掩碼、dns IP、外網(wǎng)IP
- Python實(shí)現(xiàn)根據(jù)IP地址和子網(wǎng)掩碼算出網(wǎng)段的方法
- C#設(shè)置本地網(wǎng)絡(luò)如DNS、網(wǎng)關(guān)、子網(wǎng)掩碼、IP等等
- JS 根據(jù)子網(wǎng)掩碼,網(wǎng)關(guān)計(jì)算出所有IP地址范圍示例
- js針對(duì)ip地址、子網(wǎng)掩碼、網(wǎng)關(guān)的邏輯性判斷
- 網(wǎng)絡(luò)編程基礎(chǔ)(局域網(wǎng)、ip、子網(wǎng)掩碼、網(wǎng)關(guān)、DNS)概念理解
相關(guān)文章
C語(yǔ)言實(shí)現(xiàn)關(guān)機(jī)小程序
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)關(guān)機(jī)小程序,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-02-02C語(yǔ)言代碼實(shí)現(xiàn)簡(jiǎn)易三子棋游戲
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言代碼實(shí)現(xiàn)簡(jiǎn)易三子棋游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-05-05C語(yǔ)言實(shí)現(xiàn)電話簿項(xiàng)目管理
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)電話簿項(xiàng)目管理,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-07-07Qt中PaintEvent繪制實(shí)時(shí)波形圖的實(shí)現(xiàn)示例
本文主要介紹了Qt中PaintEvent繪制實(shí)時(shí)波形圖的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06opencv車(chē)道線檢測(cè)的實(shí)現(xiàn)方法
這篇文章主要介紹了opencv車(chē)道線檢測(cè)的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08c語(yǔ)言swap(a,b)值交換的4種實(shí)現(xiàn)方法
c語(yǔ)言swap(a,b)值交換的4種實(shí)現(xiàn)方法,這么好的東西,盡管簡(jiǎn)單,但值得發(fā)表,以此共享。2013-02-02