原文連接地址:https://www.cnblogs.com/gccbuaa/p/6708774.htmlhtml
近期因爲項目有需要獲取本機的所有存在的網絡鏈接名稱,在網上也找了資料,有好幾種方法,只是就僅僅有一種是能夠達到我想要的要求api
寫下來給你們參考下網絡
第一種方法:遍歷註冊表來獲取ide
1 void __fastcall MyGetLanAdapterName(char *szLanAdapterName) 2 { 3 HKEY hKey, hSubKey, hNdiIntKey; 4 if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, 5 "System\\CurrentControlSet\\Control\\Class\\{4D36E972-E325-11CE-BFC1-08002BE10318}", 6 0, 7 KEY_READ, 8 &hKey) != ERROR_SUCCESS) 9 return; 10 11 DWORD dwIndex = 0; 12 DWORD dwBufSize = 256; 13 DWORD dwDataType; 14 char szSubKey[256]; 15 unsigned char szData[256]; 16 17 while(RegEnumKeyEx(hKey, dwIndex++, szSubKey, &dwBufSize, NULL, NULL, NULL, NULL) == ERROR_SUCCESS) 18 { 19 if(RegOpenKeyEx(hKey, szSubKey, 0, KEY_READ, &hSubKey) == ERROR_SUCCESS) 20 { 21 if(RegOpenKeyEx(hSubKey, "Ndi\\Interfaces", 0, KEY_READ, &hNdiIntKey) == ERROR_SUCCESS) 22 { 23 dwBufSize = 256; 24 if(RegQueryValueEx(hNdiIntKey, "LowerRange", 0, &dwDataType, szData, &dwBufSize) == ERROR_SUCCESS) 25 { 26 if(strcmp((char*)szData, "ethernet") == 0) // 推斷是否是以太網卡 27 { 28 dwBufSize = 256; 29 if(RegQueryValueEx(hSubKey, "DriverDesc", 0, &dwDataType, szData, &dwBufSize) == ERROR_SUCCESS) 30 { 31 // szData 中即是適配器具體描寫敘述 32 dwBufSize = 256; 33 if(RegQueryValueEx(hSubKey, "NetCfgInstanceID", 0, &dwDataType, szData, &dwBufSize) == ERROR_SUCCESS) 34 { 35 //szData中即是適配器名稱 36 strcpy(szLanAdapterName, (char *)szData); 37 } 38 } 39 } 40 } 41 RegCloseKey(hNdiIntKey); 42 } 43 RegCloseKey(hSubKey); 44 } 45 dwBufSize = 256; 46 } /* end of while */ 47 RegCloseKey(hKey); 48 }
szLanAdapterName即爲網絡鏈接的名稱 如{C6DFC923-0895-4FCE-85F2-740E6133EC6D}函數
另一種方法是經過API:spa
1 void __fastcall GetLanAdapterName() 2 { 3 ULONG ulAdapterInfoSize = sizeof(IP_ADAPTER_INFO); 4 IP_ADAPTER_INFO *pAdapterInfoBkp, *pAdapterInfo = (IP_ADAPTER_INFO*)new char[ulAdapterInfoSize]; 5 if( GetAdaptersInfo(pAdapterInfo, &ulAdapterInfoSize) == ERROR_BUFFER_OVERFLOW ) // 緩衝區不夠大 6 { 7 delete pAdapterInfo; 8 pAdapterInfo = (IP_ADAPTER_INFO*)new char[ulAdapterInfoSize]; 9 pAdapterInfoBkp = pAdapterInfo; 10 } 11 if( GetAdaptersInfo(pAdapterInfo, &ulAdapterInfoSize) == ERROR_SUCCESS ) 12 { 13 do{ // 遍歷所有適配器 14 if(pAdapterInfo->Type == MIB_IF_TYPE_ETHERNET) // 推斷是否爲以太網接口 15 { 16 // pAdapterInfo->Description 是適配器描寫敘述 17 // pAdapterInfo->AdapterName 是適配器名稱 18 } 19 pAdapterInfo = pAdapterInfo->Next; 20 }while(pAdapterInfo); 21 } 22 delete pAdapterInfoBkp; 23 }
AdapterName 也能獲得網絡鏈接的名稱 如{C6DFC923-0895-4FCE-85F2-740E6133EC6D} 適配器描寫敘述如:Realtek RTL8139(A) PCI Fast Ethernet Adaptercode
第三種方法也是使用API:orm
1 void __fastcall GetLanAdapterName() 2 { HANDLE hMprConfig; 3 DWORD dwRet=0; 4 CString strInfo,strTmp; 5 PIP_INTERFACE_INFO plfTable=NULL; 6 IP_ADAPTER_INDEX_MAP AdaptMap; 7 DWORD dwBufferSize=0; 8 WCHAR szFriendName[256]; 9 DWORD tchSize=sizeof(WCHAR)*256; 10 ZeroMemory(&szFriendName,tchSize); 11 12 dwRet=MprConfigServerConnect (NULL,&hMprConfig); 13 dwRet=GetInterfaceInfo(NULL,&dwBufferSize); 14 15 if(dwRet == ERROR_INSUFFICIENT_BUFFER) 16 { 17 plfTable=(PIP_INTERFACE_INFO)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,dwBufferSize); 18 GetInterfaceInfo(plfTable,&dwBufferSize); 19 } 20 strInfo.Empty(); 21 for(UINT i=0;i<plfTable->NumAdapters;i++) 22 { 23 AdaptMap=plfTable->Adapter[i]; 24 dwRet=MprConfigGetFriendlyName(hMprConfig,AdaptMap.Name,(PWCHAR)szFriendName,tchSize); 25 26 27 char szName[256]; 28 memset( szName, 0, 256 ); 29 int nLen = WideCharToMultiByte( CP_ACP,NULL, szFriendName,-1,NULL,0,NULL,FALSE ); 30 WideCharToMultiByte (CP_ACP,NULL,szFriendName,-1,szName,nLen,NULL,FALSE); 31 strTmp.Format(_T("Index:%u\nName:%s\n"),AdaptMap.Index,szName); //szName即爲網絡鏈接的名稱 32 strInfo+=strTmp; 33 } 34 35 HeapFree(GetProcessHeap(),HEAP_ZERO_MEMORY,plfTable); 36 }
szName即爲網絡鏈接的名稱,如 "本地鏈接","本地鏈接1"...後面經過API函數獲取需要增長如下頭文件htm
1 #include <Mprapi.h> 2 #include <iphlpapi.h> 3 #pragma comment( lib, "Iphlpapi.lib" ) 4 #pragma comment( lib, "Mprapi.lib" )
固然, 我需要的是獲取網絡鏈接的顯示名字,因此選擇第三種方法blog
AdaptMap.Index=pAdapterInfo->Index進行匹配