// Link with ws2_32.lib and iphlpapi.lib #pragma comment(lib, "ws2_32.lib") #pragma comment(lib, "iphlpapi.lib") #include <stdio.h> #include <windows.h> #include <iphlpapi.h> // 获取指定IP的MAC地址 HRESULT GetMacByIp(const char* szIP, char* szMac) { if ( NULL==szIP || NULL==szMac ) { return E_INVALIDARG; } IPAddr ipAddr; ULONG pulMac[2]; ULONG ulLen; ipAddr = inet_addr (szIP); memset (pulMac, 0xff, sizeof (pulMac)); ulLen = 6; if ( NO_ERROR == SendARP (ipAddr, 0, pulMac, &ulLen) ) { size_t i, j; PBYTE pbHexMac = (PBYTE)pulMac; if ( 6 == ulLen ) { // // Convert the binary MAC address into human-readable // for (i = 0, j = 0; i < ulLen - 1; ++i) { j += sprintf (szMac + j, "%02X:", pbHexMac[i]); } sprintf (szMac + j, "%02X", pbHexMac[i]); return S_OK; } } return E_FAIL; } int main(void) { char szMac[18] = {0}; if ( S_OK == GetMacByIp("192.168.1.1", szMac) ) { printf ("MAC address [%s]/n", szMac); } else { printf ("Can't get MAC address.../n"); } getchar(); return 0; } 注:在VC6.0下无法编译,在VC.net2003下编译成功。