写下来给大家參考下
第一种方法:遍历注冊表来获取
void __fastcall MyGetLanAdapterName(char *szLanAdapterName) { 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; 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(strcmp((char*)szData, "ethernet") == 0) // 推断是不是以太网卡 { dwBufSize = 256; if(RegQueryValueEx(hSubKey, "DriverDesc", 0, &dwDataType, szData, &dwBufSize) == ERROR_SUCCESS) { // szData 中便是适配器具体描写叙述 dwBufSize = 256; if(RegQueryValueEx(hSubKey, "NetCfgInstanceID", 0, &dwDataType, szData, &dwBufSize) == ERROR_SUCCESS) { //szData中便是适配器名称 strcpy(szLanAdapterName, (char *)szData); } } } } RegCloseKey(hNdiIntKey); } RegCloseKey(hSubKey); } dwBufSize = 256; } /* end of while */ RegCloseKey(hKey); }
szLanAdapterName即为网络连接的名称 如{C6DFC923-0895-4FCE-85F2-740E6133EC6D}
另外一种方法是通过API:
void __fastcall GetLanAdapterName() { ULONG ulAdapterInfoSize = sizeof(IP_ADAPTER_INFO); IP_ADAPTER_INFO *pAdapterInfoBkp, *pAdapterInfo = (IP_ADAPTER_INFO*)new char[ulAdapterInfoSize]; if( GetAdaptersInfo(pAdapterInfo, &ulAdapterInfoSize) == ERROR_BUFFER_OVERFLOW ) // 缓冲区不够大 { delete pAdapterInfo; pAdapterInfo = (IP_ADAPTER_INFO*)new char[ulAdapterInfoSize]; pAdapterInfoBkp = pAdapterInfo; } if( GetAdaptersInfo(pAdapterInfo, &ulAdapterInfoSize) == ERROR_SUCCESS ) { do{ // 遍历全部适配器 if(pAdapterInfo->Type == MIB_IF_TYPE_ETHERNET) // 推断是否为以太网接口 { // pAdapterInfo->Description 是适配器描写叙述 // pAdapterInfo->AdapterName 是适配器名称 } pAdapterInfo = pAdapterInfo->Next; }while(pAdapterInfo); } delete pAdapterInfoBkp; }
AdapterName 也能得到网络连接的名称 如{C6DFC923-0895-4FCE-85F2-740E6133EC6D} 适配器描写叙述如:Realtek RTL8139(A) PCI Fast Ethernet Adapter
第三种方法也是使用API:
void __fastcall GetLanAdapterName() { HANDLE hMprConfig; DWORD dwRet=0; CString strInfo,strTmp; PIP_INTERFACE_INFO plfTable=NULL; IP_ADAPTER_INDEX_MAP AdaptMap; DWORD dwBufferSize=0; WCHAR szFriendName[256]; DWORD tchSize=sizeof(WCHAR)*256; ZeroMemory(&szFriendName,tchSize); dwRet=MprConfigServerConnect (NULL,&hMprConfig); dwRet=GetInterfaceInfo(NULL,&dwBufferSize); if(dwRet == ERROR_INSUFFICIENT_BUFFER) { plfTable=(PIP_INTERFACE_INFO)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,dwBufferSize); GetInterfaceInfo(plfTable,&dwBufferSize); } strInfo.Empty(); for(UINT i=0;i<plfTable->NumAdapters;i++) { AdaptMap=plfTable->Adapter[i]; dwRet=MprConfigGetFriendlyName(hMprConfig,AdaptMap.Name,(PWCHAR)szFriendName,tchSize); char szName[256]; memset( szName, 0, 256 ); int nLen = WideCharToMultiByte( CP_ACP,NULL, szFriendName,-1,NULL,0,NULL,FALSE ); WideCharToMultiByte (CP_ACP,NULL,szFriendName,-1,szName,nLen,NULL,FALSE); strTmp.Format(_T("Index:%u\nName:%s\n"),AdaptMap.Index,szName); //szName即为网络连接的名称 strInfo+=strTmp; } HeapFree(GetProcessHeap(),HEAP_ZERO_MEMORY,plfTable); }
szName即为网络连接的名称,如 "本地连接","本地连接1"...后面通过API函数获取须要增加以下头文件
#include <Mprapi.h> #include <iphlpapi.h> #pragma comment( lib, "Iphlpapi.lib" ) #pragma comment( lib, "Mprapi.lib" )
当然, 我须要的是获取网络连接的显示名字,所以选择第三种方法