procedure TForm1.Button1Click(Sender: TObject);
var
Ip:String;
Ipstr:String;
Buffer:Array[1..32] of Char;
i:Integer;
WSData:TWSAData;
Host:PHostEnt;
begin
if WSAstartup(2,WSData)<>0 then
begin
showmessage('WS2_32.DLL初始化失败!');
halt;
end;
try
if GetHostName(@Buffer[1],32)<>0 then
begin
showmessage('没有得到主机名');
halt;
end;
except
showmessage('没有成功返回主机名!');
halt;
end;
Host:=GetHostName (@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;
WSACleanup;