Send an ARP request through the SENDARP in the IP Helper API to get the MAC address, simple and convenient, and the disadvantage is that you cannot cross the gateway.
#include
#include
#include
#pragma comment (lib, "iphlpapi.lib") # Pragma Comment (Int Arg, Char * Argv []) {IRGC, DWORD DWIP; BYTE BYMAC [6]; DWORD DWlen; Wsadata Wsadata; WsaStartup (Makeword (2, 0), & WSADATA); __TRY {Printf ("IP2MAC Ver 1.0 - Get Mac Address from IP Address Though ARP Request ./N"); if (Argc! = 2 ) {Printf ("Usage:% s
/ N ", argv [0]); __LEAVE;} dwip = inet_addr (argv [1]); if (dwip == inaddr_none) {Printf (" Invalid IP Address:% s. / n ", Argv [1]) ; __leave;} // Send an ARP query package to get the MAC address dwlen = 6; IReturn = Sendarp (DWIP, 0, (Pulong) & Bymac, & DWLEN); IF (IReturn! = no_ERROR) {Printf ("Error: Send ARP Request. / NBE Sure That Connectting TO% S Need No Gateway./N ", Argv [1]); __leave;} printf ("%. 2X -%. 2X -%. 2X -%. 2X -%. 2X-%. 2x / n ", bymac [0], bymac [1], bymac [2], bymac [3], bymac [4], bymac [5]);} __finally {wsacleanup ();} return 0;}