Rede - pegar nome maquina pelo ip |
Top Previous Next |
------------------------- MANEIRA 1 ------------------------------------
/// esta funciona no Delphi 7
uses WinSock;
function IPAddrToName(IPAddr: string): string; var SockAddrIn: TSockAddrIn; HostEnt: PHostEnt; WSAData: TWSAData; begin WSAStartup($101, WSAData); SockAddrIn.sin_addr.s_addr := Inet_Addr(PChar(IPAddr)); HostEnt:= GetHostByAddr(@SockAddrIn.Sin_addr.S_addr, 4, AF_INET); if HostEnt <> nil then Result := StrPas(Hostent^.h_name) else Result:=''; end;
procedure TForm1.Button1Click(Sender: TObject); begin Caption := IPAddrToName(Edit1.Text); end;
------------------------- MANEIRA 2 ------------------------------------
// esta não funciona no Delphi 7 (?)
uses winsock;
// usada por Nome_Maquina_Deste_IP function Lookup_Hostname(const hostname:string):longint; const INVALID_IP_ADDRESS: Int64 = $ffffffff; var RemoteHost : PHostEnt; ip_address : Int64; s : string; begin try S := HostName + #0; Ip_address := Winsock.Inet_Addr(PChar(@s[1])); if Ip_address = SOCKET_ERROR then begin RemoteHost := Winsock.GetHostByName(PChar(@s[1]));
if (RemoteHost=NIL) or (RemoteHost^.h_length<=0) then begin Result := ip_address; Exit; end else Ip_address:=longint(pointer(RemoteHost^.h_addr_list^)^); end; except Ip_address := INVALID_IP_ADDRESS; end; Result := Ip_address; end;
// usada para retornar o nome da maquina sofredora function Nome_Maquina_Deste_IP(const IP: string): string; var RemoteHost : PHostEnt; ip_address : Longint; begin ip_address:= Lookup_Hostname(IP); RemoteHost:=Winsock.GetHostByAddr(@ip_address,4,pf_inet); if RemoteHost <> nil then Result := StrPas(Pchar(RemoteHost^.h_name)) else Result := ''; end; |