Get the NIC MAC address, differentiate the virtual machine network card

Source: Internet
Author: User
Tags goto sprintf

BOOL islocaladapter (const char *padaptername)
{
BOOL ret_value = FALSE;
Char szdatabuf[max_path+1] = {0};
DWORD Dwdatalen = MAX_PATH;
DWORD dwtype = REG_SZ;
Hkey hlocalnet = NULL;


#define Net_card_key (_t ("system\\currentcontrolset\\control\\network\\{4d36e972-e325-11ce-bfc1-08002be10318}"))
sprintf (Szdatabuf, "%s\\%s\\connection", Net_card_key, Padaptername);


if (Error_success!= RegOpenKeyEx (HKEY_LOCAL_MACHINE, SZDATABUF, 0, Key_read, &hlocalnet))
{
return FALSE;
}


/* The network card information in the registry does not have this item
if (error_success!= RegQueryValueEx (hlocalnet, _t ("Mediasubtype"), 0, &dwtype, (BYTE *) Szdatabuf, &dwdatalen))
{
GOTO ret;
}
if (* (DWORD *) szdatabuf)!=0x01)
GOTO ret;
*/
Dwdatalen = MAX_PATH;
memset (szdatabuf,0,sizeof (SZDATABUF));
if (error_success!= RegQueryValueEx (hlocalnet, _t ("Pnpinstanceid"), 0, &dwtype, (BYTE *) Szdatabuf, &dwdatalen) )
{
GOTO ret;
}


if (strncmp (SZDATABUF, "PCI", strlen ("PCI"))
{
"PCI" in this key value in the network card information in the registry is not all in the front
CString id = szdatabuf;
int ret = ID. Find ("PCI");
if (ret = 1)
{
GOTO ret;
}
}


Ret_value = TRUE;


Ret:
RegCloseKey (hlocalnet);


return ret_value;
}


void macaddresstostring (const lpbyte address, LPSTR lpsz, int naddresslength = 6)
{
LPSTR p = lpsz;
for (int i = 0; i < naddresslength; i++)
{
p + + sprintf (p, I?) ":%02x": "%02x", Address[i]);
}
}

#if 1
#include "stdafx.h"
#include <atlbase.h>
#include <atlconv.h>
#include "iphlpapi.h"
#pragma comment (lib, "Iphlpapi.lib")
int Getmacbycmd (char* mac)
{
Pip_adapter_info Padapterinfo;
DWORD adapterinfosize = 0;
DWORD dwretval=0;

GetAdaptersInfo (null,&adapterinfosize);
Padapterinfo = (pip_adapter_info) GlobalAlloc (gptr,adapterinfosize);

GetAdaptersInfo (padapterinfo,&adapterinfosize);

Pip_adapter_info padapter = Padapterinfo;

 while (padapter)
 {
  if (Islocaladapter (padapter->adaptername))
   {
   macaddresstostring (padapter->address,mac,padapter->addresslength);
   break;
  }
  else
  {
   padapter = padapter->next;
  }
&NBSP}
 globalfree (padapterinfo);
 return 0;
}
#endif

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.