Obtain the MAC address (wireless network card and wired network card) by VC)

Source: Internet
Author: User
Tags snmp

 

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)

 

 

# Include "stdafx. H "<br/> # include" iphlpapi. H "<br/> # pragma comment (Lib," iphlpapi. lib ") <br/> int main (INT argc, char * argv []) <br/>{< br/> pip_adapter_info padapterinfo; <br/> pip_adapter _ <MCE: script Type = "text/JavaScript" src = "http://hi.images.csdn.net/js/blog/tiny_mce/themes/advanced/langs/zh.js" mce_src = "http://hi.images.csdn.net/js/blog/tiny_mce/themes/advanced/langs/zh.js"> </MCE: SCRIPT> <MCE: script t Ype = "text/JavaScript" src = "http://hi.images.csdn.net/js/blog/tiny_mce/plugins/syntaxhl/langs/zh.js" mce_src = "http://hi.images.csdn.net/js/blog/tiny_mce/plugins/syntaxhl/langs/zh.js"> </MCE: SCRIPT> info padapter = NULL; <br/> DWORD dwretval = 0; <br/> padapterinfo = (optional *) malloc (sizeof (ip_adapter_info); <br/> ulong identifier = sizeof (ip_adapter_info); <br/> If (getadaptersinfo (padapterinfo, & uloutb Uflen )! = Error_success) <br/>{< br/> globalfree (padapterinfo); <br/> padapterinfo = (ip_adapter_info *) malloc (uloutbuflen ); <br/>}< br/> If (dwretval = getadaptersinfo (padapterinfo, & uloutbuflen) = no_error) <br/>{< br/> padapter = padapterinfo; <br/> while (padapter) <br/>{< br/> If (strstr (padapter-> description, "PCI")> 0 // padapter-> description contains "PCI": Physical Nic <br/> | padapter-> type = 71 // padapter-> type is 71: wireless Network Adapter <br/>) <br/ >{< Br/> printf ("outputs/N"); <br/> printf ("adaptername: % s/n", padapter-> adaptername ); <br/> printf ("adapterdesc: % s/n", padapter-> description); <br/> printf ("adapteraddr :"); <br/> for (uint I = 0; I <padapter-> addresslength; I ++) <br/>{< br/> printf ("% 02x % C ", padapter-> Address [I], <br/> I = padapter-> AddressLength-1? '/N':'-'); <br/>}< br/> printf ("adaptertype: % d/N", padapter-> type ); <br/> printf ("IPaddress: % s/n", padapter-> ipaddresslist. IPaddress. string); <br/> printf ("ipmask: % s/n", padapter-> ipaddresslist. ipmask. string); <br/>}< br/> padapter = padapter-> next; <br/>}< br/> else <br/> {<br/> printf ("calltogetadaptersinfofailed. N "); <br/>}< br/> return 0; <br/>} 

Another

# Include "stdafx. H "<br/> # include" lmwksta. H "<br/> # include" lmapibuf. H "<br/> # include" lm. H "<br/> # pragma comment (Lib," netapi32.lib ") <br/> unsigned int getmacaddress (char straddress [], unsigned int max) <br/>{< br/> unsigned char macdata [8]; <br/> char ADDR [20]; // allocate data structure for MAC (6 bytes needed) <br/> unsigned int ncount = 0; <br/> wksta_transport_info_0 * pwkti; // allocate data Str Ucture for NetBIOS <br/> DWORD dwentriesread; <br/> DWORD dwtotalentries; <br/> byte * pbbuffer; <br/> // get MAC address via NetBIOS's enumerate function <br/> net_api_status dwstatus = netwkstatransportenum (<br/> null, // [in] server name <br/> 0, // [in] data structure to return <br/> & pbbuffer, // [out] pointer to buffer <br/> max_preferred_length, // [in] maximum length <br/> & dwentriesread, // [out] Counter of elements actually enumerated <br/> & dwtotalentries, // [out] Total number of elements that cocould be enumerated <br/> null ); // [in/out] resume handle <br/> // assert (dwstatus = nerr_success); <br/> pwkti = (wksta_transport_info_0 *) pbbuffer; // type cast the buffer <br/> for (DWORD I = 1; I <dwentriesread & ncount <= max; I ++) // first address is 00000000, skip it <br/> {// enumerate Macs D print <br/> swscanf (wchar_t *) pwkti [I]. wkti0_transport_address, l "% 2hx % 2hx % 2hx % 2hx % 2hx % 2hx", <br/> & macdata [0], & macdata [1], & macdata [2], & macdata [3], & macdata [4], & macdata [5]); <br/> sprintf (ADDR, "% 02x-% 02x-% 02x-% 02x-% 02x-% 02x", <br/> macdata [0], macdata [1], macdata [2], macdata [3], macdata [4], macdata [5]); <br/> If (! Strcmp (ADDR, _ T ("00-00-00-00-00-00-00") <br/> continue; <br/> strcpy (& straddress [ncount], ADDR); <br/> ncount + = 16; <br/>}< br/> // release pbbuffer allocated by above function <br/> dwstatus = netapibufferfree (pbbuffer ); <br/> // assert (dwstatus = nerr_success); <br/> return ncount; <br/>}< br/> void main () <br/>{< br/> char stra [20]; <br/> getmacaddress (stra, 1); <br/>} 

 

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.