5

I am using the following code to get the MAC address:

IP_ADAPTER_INFO adpInfo[16];
DWORD len = sizeof(adpInfo);
GetAdaptersInfo(adpInfo, &len );
printf("%02x%02x%02x%02x%02x%02x", adpInfo[0].Address[0], adpInfo[0].Address[1], adpInfo[0].Address[2], adpInfo[0].Address[3], adpInfo[0].Address[4], adpInfo[0].Address[5]);

However, if the computer has many network adapters (for example: Ethernet and WiFi), then every time I call this code I get a different MAC address.

Is there a way to always get the same MAC address (for example: Ethernet).

John
  • 1,049
  • 1
  • 14
  • 34
  • I always get the same. – adjan May 07 '15 at 23:27
  • 1
    @addy2012 Sorry, I meant you could get another MAC address at some point, and not immediately. – John May 07 '15 at 23:29
  • At some point? How do you mean? – adjan May 07 '15 at 23:29
  • @addy2012 I once called it and got the MAC AF-BB... and then I called it a couple of days later and I got the MAC D1-AE... – John May 07 '15 at 23:33
  • It should normally return information about *all* the adapters (that are installed/enabled) on the machine. In the code above you're only displaying the first one though, so if the order changes, you'll see a different one. Of course, it's also possible (with some adapters) for the user to modify an adapter's MAC address. – Jerry Coffin May 07 '15 at 23:34
  • There are several ways to query the NICs configured on your Windows system: including [GetAdaptersInfo()](https://msdn.microsoft.com/en-us/library/windows/desktop/aa365917%28v=vs.85%29.aspx), [gethostbyname()](https://msdn.microsoft.com/en-us/library/windows/desktop/ms738524%28v=vs.85%29.aspx), etc. Unfortunately, there is no reliable way to be sure you'll get the same NICs in the same order. SUGGESTION: Copy the NICs to a sorted list (or a [dictionary hash](http://stackoverflow.com/questions/4384359/quick-way-to-implement-dictionary-in-c)). – FoggyDay May 07 '15 at 23:40
  • `gethostbyname()` does not return NIC info. And using it to return local IPs is not 100% reliable. – Remy Lebeau May 09 '15 at 01:02

2 Answers2

3

Since GetAdaptersInfo method includes almost as much information as IPCONFIG /ALL (including your DHCP server, Gateway, IP address list, subnet mask and WINS server) you can use that. It also enumerates all the NICs on your PC, even if they are not connected to valid networks (but the NICs do have to be "enabled" in Windows)

Sample, print all interfaces:

static void GetMACaddress(void)
{
  IP_ADAPTER_INFO AdapterInfo[16];

  DWORD dwBufLen = sizeof(AdapterInfo);

  DWORD dwStatus = GetAdaptersInfo(AdapterInfo, &dwBufLen);

  assert(dwStatus == ERROR_SUCCESS);

  PIP_ADAPTER_INFO pAdapterInfo = AdapterInfo;

  do {
    PrintMACaddress(pAdapterInfo->Address);
    pAdapterInfo = pAdapterInfo->Next;
  }
  while(pAdapterInfo);
}

You can save the AdapterName, then compare it in next calls to make sure the MAC of specified adapter is retrieved.

Look at here for IP_ADAPTER_INFO structure: https://msdn.microsoft.com/en-us/library/windows/desktop/aa366062%28v=vs.85%29.aspx

Code from: http://www.codeguru.com/cpp/i-n/network/networkinformation/article.php/c5451/Three-ways-to-get-your-MAC-address.htm

Ciro Pedrini
  • 4,135
  • 1
  • 11
  • 17
1

I believe enumeration of the network adaptor information by the windows os depends on the priority of the network adaptors. Priorities of network adapters can be viewed ,edited by traversing to

Open Network and Sharing Centre -> Change adapter settings ->Advanced[Enable menu bar if not visible]->Advanced settings. One can edit the priorities of the network adapter. enter image description here

GingerJack
  • 3,044
  • 1
  • 17
  • 19