There are many ways to get the MAC address of the NIC, such as: Netbios,snmp,getadaptersinfo. After testing found that the Netbios method in the network cable pull out of the case to get the Mac, and the SNMP method will sometimes get multiple duplicate network card Mac, try to try or GetAdaptersInfo method is better, the network cable unplug the case can get Mac, and very accurate, The NIC will not be repeatedly acquired.
GetAdaptersInfo method is also not perfect, there are some problems:
1 How to distinguish between physical and virtual network card;
2 How to distinguish between wireless card and wired network card;
3 the "disabled" NIC is not available.
With regard to questions 1 and 2 my approach is:
Distinguish between physical and virtual network adapters: Padapter->description contains "PCI" is: Physical network card. (3 machines can be tried)
Distinguish between wireless card and wired network card: Padapter->type 71 is: Wireless network card. (try 2 wireless card also can)
Now post the code and share it with you:
#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 contains "PCI" AS: Physical network card
|| Padapter->type = =//Padapter->type is 71: Wireless network card
)
{
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;
}