如何通過IP得到對方的電腦名稱和網卡的物理地址?(80分)

  • 主题发起人 主题发起人 joshua_jia
  • 开始时间 开始时间
J

joshua_jia

Unregistered / Unconfirmed
GUEST, unregistred user!
給定一個IP,怎樣獲取它的名稱和MAC地址呢?
請各位幫幫小弟,謝了.
 
/通过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;
 
接受答案了.
 
后退
顶部