var
Ip:string;
Ipstr:string;
buffer:array[1..32] of char;
i:integer;
WSData:TWSAData;
Host:PHostEnt;
begin
if WSAstartup(2,WSData)<>0 then //为程序使用WS2_32.DLL初始化
begin
showmessage('WS2_32.DLL初始化失败!');
halt;
end;
try
if gethostname(@buffer[1],32)<>0 then
begin
showmessage('没有得到主机名!');
halt;
end;
except
showmessage('没有成功返回主机名');
halt;
end;
Host:=gethostbyname(@buffer[1]);
if Host=nil then
begin
showmessage('IP地址为空!');
halt;
end
else
begin
edit2.text:=host.h_name;
edit3.text:=chr(host.h_addrtype+64);
for i:=1 to 4 do
begin
Ip:=inttostr(Ord(Host.h_addr^[i-1]));
showmessage('分段Ip地址为:'+Ip);
Ipstr:=Ipstr+Ip;
if i<4 then
Ipstr:=Ipstr+'.'
else
edit1.text:=Ipstr;
end;
end;
end;