CPP 278
MAC address code from HAC2 - 2019/03/01 By krewat on 1st March 2019 04:42:19 PM
  1. #include <Windows.h>
  2. #include <Iphlpapi.h>
  3.  
  4. // Get a unique identifier - really a MAC address for the ethernet adapter. There might be more than one, just use the last one.
  5. // Shamelessly ripped from here: https://stackoverflow.com/questions/13646621/how-to-get-mac-address-in-windows-with-c
  6. void identify() {
  7.  
  8.         PIP_ADAPTER_INFO AdapterInfo;
  9.         DWORD dwBufLen = sizeof(IP_ADAPTER_INFO);
  10.         char mac_addr[19];
  11.  
  12.         AdapterInfo = (IP_ADAPTER_INFO *)malloc(sizeof(IP_ADAPTER_INFO));
  13.         if (AdapterInfo == NULL) {
  14.                 hkDrawText("Error allocating memory needed to call GetAdaptersinfo", C_TEXT_RED);
  15.                 return;
  16.         }
  17.  
  18.         // Make an initial call to GetAdaptersInfo to get the necessary size into the dwBufLen variable
  19.         if (GetAdaptersInfo(AdapterInfo, &dwBufLen) == ERROR_BUFFER_OVERFLOW) {
  20.                 free(AdapterInfo);
  21.                 AdapterInfo = (IP_ADAPTER_INFO *)malloc(dwBufLen);
  22.                 if (AdapterInfo == NULL) {
  23.                         hkDrawText("Error allocating memory needed to call GetAdaptersinfo", C_TEXT_RED);
  24.                         return;
  25.                 }
  26.         }
  27.  
  28.         if (GetAdaptersInfo(AdapterInfo, &dwBufLen) == NO_ERROR) {
  29.                 // Contains pointer to current adapter info
  30.                 PIP_ADAPTER_INFO pAdapterInfo = AdapterInfo;
  31.                 do {
  32.                         // technically should look at pAdapterInfo->AddressLength
  33.                         //   and not assume it is 6.
  34.                         sprintf(mac_addr, "%02X:%02X:%02X:%02X:%02X:%02X",
  35.                                 pAdapterInfo->Address[0], pAdapterInfo->Address[1],
  36.                                 pAdapterInfo->Address[2], pAdapterInfo->Address[3],
  37.                                 pAdapterInfo->Address[4], pAdapterInfo->Address[5]);
  38.                         //hkDrawPrintf(PREF_DEBUG, C_TEXT_RED, "Address: %s, mac: %s", pAdapterInfo->IpAddressList.IpAddress.String, mac_addr);
  39.                         strcpy_s(hac2_id, mac_addr);
  40.                         pAdapterInfo = pAdapterInfo->Next;
  41.                 } while (pAdapterInfo);
  42.         }
  43.         free(AdapterInfo);
  44.         return;
  45. }

HaloNet.Net is for source code and general debugging text.

Login or Register to edit, delete and keep track of your pastes and more.

Raw Paste

Login or Register to edit or fork this paste. It's free.