VC Gets the MAC address of the physical network card

Source: Internet
Author: User
Tags printf snmp

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

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.