我用SendARP这个函数获取远程主机的MAC地址,因为delphi中未提供相应的封装单元,所以加载了iphlpapi.dll,加载成功后调用SendARP,代码如下:
Handle := LoadLibrary('iphlpapi.dll');
@MySendARP := GetProcAddress(Handle,'SendARP');//区分大小写???
MySendARP(RemoteAddr,0,PMacAddr,MacAddrLen);
showmessage(inttostr(PMacAddr^)); //此处有问题,PMacAddr^始终为0,
// 也就是此处无法获取MAC地址希望大家赐教!
Handle := LoadLibrary('iphlpapi.dll');
@MySendARP := GetProcAddress(Handle,'SendARP');//区分大小写???
MySendARP(RemoteAddr,0,PMacAddr,MacAddrLen);
showmessage(inttostr(PMacAddr^)); //此处有问题,PMacAddr^始终为0,
// 也就是此处无法获取MAC地址希望大家赐教!
function SendARP( const DestIP : DWord;
const SrcIP : DWord;
const pMacAddr : Pointer;
const PhyAddrLen : PULONG) : DWord; stdcall; external 'IPHLPAPI.DLL' name 'SendARP';function inet_addr(const cp : PChar) : DWord; stdcall; external 'WS2_32.DLL' name 'inet_addr';var
dwResult : DWord;
ulIPAddr : DWord;
ulMACAddr : Array [0..5] of Byte;
ulAddrLen : ULONG;begin
ulIPAddr := inet_addr(PChar('192.168.1.1'));
ShowMessage(IntToHex(ulIPAddr,8)); // Ist mal interessant zu sehen
if ulIPAddr = INADDR_NONE exit;
ulAddrLen := 6;
dwResult := SendARP(ulIPAddr,0,@ulMACAddr,@ulAddrLen);
ShowMessage('dwResult: '+IntToStr(dwResult)); // if 0, then ok
ShowMessage(IntToHex(ulMACAddr[0],2)+':'+
IntToHex(ulMACAddr[1],2)+':'+
IntToHex(ulMACAddr[2],2)+':'+
IntToHex(ulMACAddr[3],2)+':'+
IntToHex(ulMACAddr[4],2)+':'+
IntToHex(ulMACAddr[5],2));
end;