Question

I am trying to write some C code which extracts the MAC no of a computer and prints it. Following is my code.

#ifndef WINVER
#define WINVER 0x0600
#endif

#include <stdlib.h>
#include <winsock2.h>
#include <iphlpapi.h>
#include <stdio.h>
#include <assert.h>
#pragma comment(lib, "IPHLPAPI.lib")

// BYTE has been typedefined as unsigned char
// DWORD has been typedefined as 32 bit unsigned long

static void PrintMACaddress(unsigned char MACData[])
{
    printf("MAC Address: %02X-%02X-%02X-%02X-%02X-%02X\n",
    MACData[0], MACData[1], MACData[2], MACData[3], MACData[4], MACData[5]);
}

// Fetches the MAC address and prints it
static void GetMACaddress(void){
    IP_ADAPTER_ADDRESSES AdapterInfo[16];       // Allocate information for up to 16 NICs
    DWORD dwBufLen = sizeof(AdapterInfo);       // Save memory size of buffer

    // Arguments for GetAdapterAddresses:
    DWORD dwStatus = GetAdaptersAddresses(0, 0, NULL, AdapterInfo, &dwBufLen);
                                                                // [out] buffer to receive data
                                                                // [in] size of receive data buffer

    assert(dwStatus == ERROR_SUCCESS);                          // Verify return value is valid, no buffer overflow
    PIP_ADAPTER_ADDRESSES pAdapterInfo = AdapterInfo;           // Contains pointer to current adapter info

    do {
        PrintMACaddress(pAdapterInfo->Address);                 // Print MAC address
        pAdapterInfo = pAdapterInfo->Next;                      // Progress through linked list
    }while(pAdapterInfo);                                       // Terminate if last adapter
}

int main(){
    GetMACaddress();
    return 0;
}


But when I run my code it gives the following error :
Error : undefined reference to `GetAdaptersAddresses@20'

All though the GetAdaptersAddresses() function is included in iphlpapi.h library.
I also tried running the code using the GetAdaptersInfo() function but also gives the same kind of error.

I am using CodeBlocks to compile my code using the GNU GCC C++ 98 compiler version.
The operating system which I am working on is Windows 7.

Can anybody point out the reason for this kind of error.

Was it helpful?

Solution

GCC does not support #pragma comment and there is no equivalent. You will need to update your project settings to specifically link with the Iphlpapi.lib library.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top