VC obtains the MAC address of the physical Nic

Source: Internet
Author: User

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 ;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.