我想寫一些C代碼,它提取計算機的MAC號並打印出來。以下是我的代碼。使用C的MAC地址C
#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;
}
但是當我運行我的代碼,它提供了以下錯誤:
錯誤:未定義的引用`GetAdaptersAddresses @功能包括在20'
所有雖然GetAdaptersAddresses() iphlpapi.h庫。
我也嘗試使用GetAdaptersInfo()函數運行代碼,但也提供了相同種類的錯誤。
我正在使用CodeBlocks編譯我的代碼使用GNU GCC C++ 98編譯器版本。
我正在使用的操作系統是Windows 7。
有人可以指出這種錯誤的原因。
[undefined reference to \'\ _GetAdaptersAddresses @ 20' - 但我包含-liphlpapi]的可能重複(http://stackoverflow.com/questions/10972794/undefined-reference-to-getadaptersaddresses20-but-i-included -liphlpapi) – stijn
我根本沒有看到C++。 – 2013-08-04 18:29:28