VC 获取物理网卡的MAC地址
获取网卡的MAC地址的方法很多,如:Netbios,SNMP,GetAdaptersInfo等。经过测试发现 Netbios 方法在网线拔出的情况下获取不到MAC,而 SNMP 方法有时会获取多个重复的网卡的MAC,试来试去还是 GetAdaptersInfo 方法比较好,网线拔出的情况下可以获取MAC,而且很准确,不会重复获取网卡。
GetAdaptersInfo 方法也不是十全十美,也存在些问题:1)如何区分物理网卡和虚拟网卡;
2)如何区分无线网卡和有线网卡;
3)“禁用”的网卡获取不到。
关于问题1和问题2我的处理办法是:
区分物理网卡和虚拟网卡:pAdapter->Description中包含"PCI"是:物理网卡。(试了3台机器可以)
区分无线网卡和有线网卡:pAdapter->Type为71的是:无线网卡。(试了2个无线网卡也可以)
现在把代码贴出来和大家分享:
#include "stdafx.h"
#include <atlbase.h>
#include <atlconv.h>
#include "iphlpapi.h"
#pragma comment ( lib, "Iphlpapi.lib" )
int main(int argc, char* argv[])
{
PIP_ADAPTER_INFO pAdapterInfo;
PIP_ADAPTER_INFO pAdapter = NULL;
DWORD dwRetVal = 0;
pAdapterInfo = (IP_ADAPTER_INFO *) malloc( sizeof(IP_ADAPTER_INFO) );
ULONG ulOutBufLen = sizeof(IP_ADAPTER_INFO);
if (GetAdaptersInfo( pAdapterInfo, &ulOutBufLen) != ERROR_SUCCESS)
{
GlobalFree (pAdapterInfo);
pAdapterInfo = (IP_ADAPTER_INFO *) malloc (ulOutBufLen);
}
if ((dwRetVal = GetAdaptersInfo( pAdapterInfo, &ulOutBufLen)) == NO_ERROR)
{
pAdapter = pAdapterInfo;
while (pAdapter)
{
if(
strstr(pAdapter->Description,"PCI") > 0 // pAdapter->Description中包含"PCI"为:物理网卡
|| pAdapter->Type == 71 // pAdapter->Type是71为:无线网卡
)
{
printf("------------------------------------------------------------\n");
printf("Adapter Name: \t%s\n", pAdapter->AdapterName);
printf("Adapter Desc: \t%s\n", pAdapter->Description);
printf("Adapter Addr: \t");
for (UINT i = 0; i < pAdapter->AddressLength; i++)
{
printf("%02X%c", pAdapter->Address[i],
i == pAdapter->AddressLength - 1 ? '\n' : '-');
}
printf("Adapter Type: \t%d\n", pAdapter->Type);
printf("IP Address: \t%s\n", pAdapter->IpAddressList.IpAddress.String);
printf("IP Mask: \t%s\n", pAdapter->IpAddressList.IpMask.String);
}
pAdapter = pAdapter->Next;
}
}
else
{
printf("Call to GetAdaptersInfo failed.\n");
}
return 0;
}
#include <atlbase.h>
#include <atlconv.h>
#include "iphlpapi.h"
#pragma comment ( lib, "Iphlpapi.lib" )
int main(int argc, char* argv[])
{
PIP_ADAPTER_INFO pAdapterInfo;
PIP_ADAPTER_INFO pAdapter = NULL;
DWORD dwRetVal = 0;
pAdapterInfo = (IP_ADAPTER_INFO *) malloc( sizeof(IP_ADAPTER_INFO) );
ULONG ulOutBufLen = sizeof(IP_ADAPTER_INFO);
if (GetAdaptersInfo( pAdapterInfo, &ulOutBufLen) != ERROR_SUCCESS)
{
GlobalFree (pAdapterInfo);
pAdapterInfo = (IP_ADAPTER_INFO *) malloc (ulOutBufLen);
}
if ((dwRetVal = GetAdaptersInfo( pAdapterInfo, &ulOutBufLen)) == NO_ERROR)
{
pAdapter = pAdapterInfo;
while (pAdapter)
{
if(
strstr(pAdapter->Description,"PCI") > 0 // pAdapter->Description中包含"PCI"为:物理网卡
|| pAdapter->Type == 71 // pAdapter->Type是71为:无线网卡
)
{
printf("------------------------------------------------------------\n");
printf("Adapter Name: \t%s\n", pAdapter->AdapterName);
printf("Adapter Desc: \t%s\n", pAdapter->Description);
printf("Adapter Addr: \t");
for (UINT i = 0; i < pAdapter->AddressLength; i++)
{
printf("%02X%c", pAdapter->Address[i],
i == pAdapter->AddressLength - 1 ? '\n' : '-');
}
printf("Adapter Type: \t%d\n", pAdapter->Type);
printf("IP Address: \t%s\n", pAdapter->IpAddressList.IpAddress.String);
printf("IP Mask: \t%s\n", pAdapter->IpAddressList.IpMask.String);
}
pAdapter = pAdapter->Next;
}
}
else
{
printf("Call to GetAdaptersInfo failed.\n");
}
return 0;
}