怡.梦 (2004-03-13 15:39:41)
由计算机名称得到IP地
procedure TForm1.Button1Click(Sender: TObject);
var
WSAData: TWSAData;
HostEnt: PHostEnt;
sComputerName, sIP: string;
begin
sComputername:=edit1.text;
WSAStartup(2, WSAData);
HostEnt := gethostbyname(PChar(sComputerName));
if HostEnt <> nil then
begin
with HostEnt^ do
sIP := Format('%d.%d.%d.%d', [Byte(h_addr^[0]), Byte(h_addr^[1]), Byte(h_addr^[2]), Byte(h_addr^[3])]);
end;
WSACleanup;
edit2.text:=sIP;
end;
end.
sfwany (2004-03-13 15:40:02)
function tform1.ipaddrtoname(ipaddr:string):string;
var
sockaddrin:tsockaddrin;
hostent
hostent;
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
begin
result:=strpas(hostent^.h_name);
end
else
begin
result:='';
end;
end;
///
function tform1.getipfromname(name:string):string;
var
wsadata:twsadata;
hostent
hostent;
begin
wsastartup(2,wsadata);
hostent:=gethostbyname(pchar(name));
with hostent^ do
result:=format('%d.%d.%d.%d',[byte(h_addr^[0]),byte(h_addr^[1]),
byte(h_addr^[2]),byte(h_addr^[3])]);
wsacleanup;
end;