17

I need some help with MAC addresses. I have to get it by using some code in C++ so could anybody help me with this? I've already tried a lot of useless codes. If exists any specific method or lib that I should study to find the MAC address, I will be very happy if anybody pass me a link or something to know more about this.

6
  • 4
    This is a Platform specific question, which has nothing to do with (Standard) C++. Thus, please tell us for which OS you are trying to get this. Nov 30, 2012 at 13:57
  • MAC address of what media access controller though? There could be many.
    – user405725
    Nov 30, 2012 at 14:00
  • stackoverflow.com/questions/823553/… <-- If in Windwows, this is a dupe of Nov 30, 2012 at 14:01
  • The OS is Windows (all versions). I will need to do for Linux too, but in a distant future :P
    – guiarrd
    Nov 30, 2012 at 16:56
  • @Prof.Falken: Not a dupe. The other question is highly specific to a scenario that most code should not be attempting to handle.
    – Joshua
    Jul 24, 2017 at 17:31

2 Answers 2

32

I got it people! Me and a guy from the work solve this using this code:

#include <stdio.h>
#include <Windows.h>
#include <Iphlpapi.h>
#include <Assert.h>
#pragma comment(lib, "iphlpapi.lib")

char* getMAC();

int main(){ 
  char* pMac = getMAC();
  system("pause");
  free(pMac);
}
char* getMAC() {
  PIP_ADAPTER_INFO AdapterInfo;
  DWORD dwBufLen = sizeof(IP_ADAPTER_INFO);
  char *mac_addr = (char*)malloc(18);

  AdapterInfo = (IP_ADAPTER_INFO *) malloc(sizeof(IP_ADAPTER_INFO));
  if (AdapterInfo == NULL) {
    printf("Error allocating memory needed to call GetAdaptersinfo\n");
    free(mac_addr);
    return NULL; // it is safe to call free(NULL)
  }

  // Make an initial call to GetAdaptersInfo to get the necessary size into the dwBufLen variable
  if (GetAdaptersInfo(AdapterInfo, &dwBufLen) == ERROR_BUFFER_OVERFLOW) {
    free(AdapterInfo);
    AdapterInfo = (IP_ADAPTER_INFO *) malloc(dwBufLen);
    if (AdapterInfo == NULL) {
      printf("Error allocating memory needed to call GetAdaptersinfo\n");
      free(mac_addr);
      return NULL;
    }
  }

  if (GetAdaptersInfo(AdapterInfo, &dwBufLen) == NO_ERROR) {
    // Contains pointer to current adapter info
    PIP_ADAPTER_INFO pAdapterInfo = AdapterInfo;
    do {
      // technically should look at pAdapterInfo->AddressLength
      //   and not assume it is 6.
      sprintf(mac_addr, "%02X:%02X:%02X:%02X:%02X:%02X",
        pAdapterInfo->Address[0], pAdapterInfo->Address[1],
        pAdapterInfo->Address[2], pAdapterInfo->Address[3],
        pAdapterInfo->Address[4], pAdapterInfo->Address[5]);
      printf("Address: %s, mac: %s\n", pAdapterInfo->IpAddressList.IpAddress.String, mac_addr);
      // print them all, return the last one.
      // return mac_addr;

      printf("\n");
      pAdapterInfo = pAdapterInfo->Next;        
    } while(pAdapterInfo);                        
  }
  free(AdapterInfo);
  return mac_addr; // caller must free.
}
6
  • I see a memory leak in this code: free(AdapterInfo); does not happen if return mac_addr; happens. Another problem is that function does not return anything if return mac_addr; does not happen. But the basic idea seems to be correct.
    – Tsar Ioann
    Aug 16, 2014 at 8:53
  • 1
    You may use following code( in place of sprintf) to avoid compilation error in VS 2015 sprintf_s(mac_addr, 6 * sizeof(pAdapterInfo->Address[1]), "%02X:%02X:%02X:%02X:%02X:%02X", pAdapterInfo->Address[0], pAdapterInfo->Address[1], pAdapterInfo->Address[2], pAdapterInfo->Address[3], pAdapterInfo->Address[4], pAdapterInfo->Address[5]);
    – qqqqq
    Feb 4, 2016 at 22:38
  • 1
    Unless you still support Windows 2000 you should use GetAdaptersAddresses msdn.microsoft.com/en-us/library/windows/desktop/…
    – Joshua
    Jul 24, 2017 at 17:33
  • 1
    And mac_addr = (char*)malloc(17); should be mac_addr = (char*)malloc(18); so there is room for the NUL terminating the string. Aug 29, 2018 at 20:02
4

C++ doesn't have any built-in concept of a "MAC address", it's not something that has to exist in order for C++ code to run. Thus, it's platform-specific. You must tell us which platform you're trying to do this for, and also (of course) read documentation that matches that platform.

If you want to do this in a portable way, you should look for a suitable library that supports all the desired platforms.

Also, note that a computer can have any number of network adapters, so there's no requirement that there is just one MAC address.

1

Not the answer you're looking for? Browse other questions tagged or ask your own question.