写下来给大家参考下
第一种方法:遍历注册表来获取
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;iNumAdapters;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
#include
#pragma comment( lib, "Iphlpapi.lib" )
#pragma comment( lib, "Mprapi.lib" )
当然,我需要的是获取网络连接的显示名字,所以选择第三种方法