本程式示範如何在 Windows 平台以 C 語言撰寫工具 RmtHost.exe,透過 SendARP API 解析指定 IPv4 位址對應的 MAC 位址,並可選掃描連續 IP 範圍。
ws2_32.lib) — 網路基礎通訊;iphlpapi.lib) — 提供 SendARP 函數。WSAStartup);gethostbyaddr 反解主機名;SendARP 發送 ARP 請求封包,取得 6 位元組 MAC 位址;IP - HostName - MAC;// 發送 ARP 請求
ULONG macAddLen = 6;
unsigned char macAddress[6];
iRet = SendARP(nRemoteAddr, 0, (PULONG)&macAddress, &macAddLen);
if (iRet == NO_ERROR) {
printf("MacAddress: ");
for (int i = 0; i < 6; i++) {
printf("%.2x", macAddress[i]);
if (i < 5) printf("-");
}
printf("\n");
} else {
printf("SendARP Error:%d\n", GetLastError());
}
RmtHost.exe 192.168.0.3 // 查詢單一 IP
RmtHost.exe 192.168.0.3 255 // 掃描 255 個連續 IP