There are many ways to obtain the MAC address of an Eni, such as NETBIOS, SNMP, and getadaptersinfo. After testing, it is found that the NetBIOS method cannot obtain the Mac when the network cable is pulled out, while the SNMP method sometimes gets the MAC of multiple duplicate network adapters. It is better to try to use the getadaptersinfo method, mac can be obtained when the network cable is pulled out, and it is very accurate and will not be retrieved again.
The getadaptersinfo method is not perfect, and there are also some problems:
1) How to differentiate physical and virtual NICs;
2) how to distinguish between a wireless network card and a wired network card;
3) The disabled Nic cannot be obtained.
My solutions to problems 1 and 2 are as follows:
Distinguish physical and virtual NICs: padapter-> description contains "PCI": Physical Nic. (3 machines are allowed)
Distinguish between a wireless network card and a wired network card: If padapter-> type is 71, it is a wireless network card. (I tried two wireless NICs)
NowCodePost it to share 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 [])
{< br> 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": Physical Nic
| Padapter -> Type = 71 // The value of padapter-> type is 71: Wireless Nic
)
{
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 ;
}