18

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.

Jared Burrows
  • 54,294
  • 25
  • 151
  • 185
guiarrd
  • 531
  • 1
  • 4
  • 10

2 Answers2

33

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.
}
Jesse Chisholm
  • 3,857
  • 1
  • 35
  • 29
guiarrd
  • 531
  • 1
  • 4
  • 10
  • http://msdn.microsoft.com/en-us/library/windows/desktop/aa365917(v=vs.85).aspx – dlchambers Aug 30 '13 at 00:25
  • 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 '14 at 08: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 04 '16 at 22:38
  • 1
    Unless you still support Windows 2000 you should use GetAdaptersAddresses https://msdn.microsoft.com/en-us/library/windows/desktop/aa365915(v=vs.85).aspx – Joshua Jul 24 '17 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. – Jesse Chisholm Aug 29 '18 at 20:02
  • Just edited the code to format it better, and address some of the memory issues mentioned. – Jesse Chisholm Aug 29 '18 at 21:36
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.

unwind
  • 391,730
  • 64
  • 469
  • 606
  • Thanks for the answer, unwind. I'm using Dev C++ for windows. I tried to use codes like this:http://stackoverflow.com/questions/221894/c-get-mac-address-of-network-adapters-on-vista?rq=1 – guiarrd Nov 30 '12 at 17:01
  • "in Windows" is a good hint – Sam Ginrich May 01 '23 at 18:19