4

how can i find in c++, the mac address of the computer that an application is currently running on and then compare that mac address with a certain mac address?

so lets say that certain mac address that we want to compare with is AB-12-CD-34-EF-56, how is it possible to find the mac address that the application is running on, then compare that mac address with the AB-12-CD-34-EF-56 mac address? what is the best and simplest way of doing something like this?

codrgii
  • 229
  • 2
  • 7
  • 1
    That depends entirely on the OS you're running on. Given you've tagged this VC++, you'll have to look in the Windows Networking APIs for an appropriate function. – Marc B Apr 28 '12 at 15:16
  • 1
    possible duplicate of [visual 6.0 and finding mac address](http://stackoverflow.com/questions/10365739/visual-6-0-and-finding-mac-address) – Joe Apr 28 '12 at 18:30

1 Answers1

3

On Windows you can use the function GetAdaptersAddresses() to get an IP_ADAPTER_ADDRESSES structure which contains PhysicalAddress[MAX_ADAPTER_ADDRESS_LENGTH]; (that's the mac adress).

This function will introduce the library Iphlpapi.lib and the header file <iphlpapi.h> as dependency. A simple example which will print all mac addresses from available adapters:

#include <Winsock2.h>
#include <iphlpapi.h>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <vector>
#pragma comment(lib, "IPHLPAPI.lib")

int main(int argc, char* argv[]){
  ULONG outBufLen = sizeof(IP_ADAPTER_ADDRESSES);
  GetAdaptersAddresses(0, 0, NULL, NULL, &outBufLen);
  std::vector<uint8_t> bytes(outBufLen, 0);
  PIP_ADAPTER_ADDRESSES pCurrAddresses = (IP_ADAPTER_ADDRESSES *)bytes.data();
  DWORD dwRetVal = GetAdaptersAddresses(0, 0, NULL, pCurrAddresses, &outBufLen);
  if (dwRetVal == NO_ERROR) {
    while (pCurrAddresses != NULL){ 
      for (size_t i = 0; i < (int) pCurrAddresses->PhysicalAddressLength; i++) {
        if (i == (pCurrAddresses->PhysicalAddressLength - 1))
          std::printf("%.2X\n", (int) pCurrAddresses->PhysicalAddress[i]);
        else
          std::printf("%.2X-",(int) pCurrAddresses->PhysicalAddress[i]);
      }
      pCurrAddresses = pCurrAddresses->Next;
    }
  }
  std::system("pause");
  return 0;
}
Constantin
  • 8,721
  • 13
  • 75
  • 126
  • Don't `new POD()`. Use `malloc` or `HeapAlloc` to stay platform specific... or just a `std::vector`. Avoid `naked new()` as much as possible. And when you use it, use it for proper objects, not POD. – CodeAngry Jul 09 '14 at 07:52
  • @CodeAngry Is there any specific reason why I shouldn't use `new` with PODs? The reason why I've used `new` here, is that I've read often on SO, that `malloc` shouldn't be used in C++ at all. i.e. http://stackoverflow.com/questions/184537/in-what-cases-do-i-use-malloc-vs-new – Constantin Jul 09 '14 at 07:59
  • They say don't use `malloc` for the same reason as don't use `new`. To avoid naked pointers because people have a tendency of forgetting to `free`/`delete` them, especially in code with multiple exits `(returns)`. [Checkout rastertek DX tutorials' code and you'll see.] `std::vector<>` frees the memory on destruct and is good for both POD and objects. The rule of thumb is, unless you really know what you're doing, who owns the object and who accesses it, and are exception-safe... don't use new/malloc. Use `std::containers` or `std::unique_ptr` for your heap object storage needs. - *Hai bafta!* – CodeAngry Jul 09 '14 at 08:04
  • @CodeAngry Thank you for your suggestion - I've changed my answer accordingly. – Constantin Jul 09 '14 at 10:43