/通过IP获取机器名
uses
WinSock;
function GetNameByIP(IP: String; var Name: String): Boolean;
var
SockAddrIn: TSockAddrIn;
HostEnt: PHostEnt;
WSAData: TWSAData;
begin
Result := False;
if inet_addr(PChar(IP))=INADDR_NONE then //IP地址无效
Exit;
try
if WSAStartup(2,WSAData)<>ERROR_SUCCESS then
Exit;
SockAddrIn.sin_addr.s_addr:=inet_addr(PChar(IP));
HostEnt:=gethostbyaddr(@SockAddrIn.sin_addr.S_addr,4,AF_INET);
if HostEnt<>nil then
Name:=StrPas(Hostent^.h_name);
Result:=True;
finally
WSACleanup;
end;
end;
//通过IP获取MAC地址
uses
WinSock;
function GetMacByIP(const IPAddress: String): String;
const
IPHLPAPI = 'IPHLPAPI.DLL';
type
ipAddr = LongInt;
puLong = ^u_Long;
var
DestIP: Integer;
pMacAddr: puLong;
AddrLen: u_Long;
MacAddr: array [1..6] of Byte;
p: PByte;
s: String;
i: Integer;
IPStr: String;
hMod: hModule;
SendARP: function(DestIP: ipAddr; SrcIP: ipAddr; pMacAddr: puLong;
PhyAddrLen: puLong): DWORD; stdcall;
begin
Result:='';
hMod:=LoadLibrary(IPHLPAPI);
if hMod=0 then
Exit;
@SendARP:=GetProcAddress(hMod,'SendARP');
if not Assigned(SendARP) then
Exit;
try
IPStr:=IPAddress;
DestIP:=iNet_Addr(PChar(IPStr));
pMacAddr:=puLong(@MacAddr);
AddrLen:=SizeOf(MacAddr);
if SendARP(DestIP,0,pMacAddr,@AddrLen)=0 then
begin
s:='';
p:=PByte(pMacAddr);
if (p<>nil) and (AddrLen>0) then
begin
for i:=1 to AddrLen do
begin
s:=s+IntToHex(p^,2);
p:=Pointer(Integer(p)+SizeOf(Byte));
end;
SetLength(s,12);
end;
end;
Result:=s;
finally
FreeLibrary(hMod);
end;
end;