procedure TForm1.Button4Click(Sender: TObject);{查找本机的ip地址}
var
ch: ARRAY[1..32] OF Char;
i: Integer;
WSData: TWSAData;
MyHost: PHostEnt;
a, b: string;
begin
if WSAstartup(2,wsdata)<>0 then
begin
Edit1.Text := 'can''t start Winsock: Error ' + IntToStr(WSAGetLastError);
Halt(2);
end;
try
IF getHostName(@ch[1],32)<>0 then
begin
Edit1.Text := 'getHostName failed';
Halt(3);
end;
except
Edit1.Text := 'getHostName failed';
halt(3);
end;
MyHost:=GetHostByName(@ch[1]);
if MyHost=NIL then
begin
Edit1.text := (' +GetHostName('+StrPas(@ch[1])+'), failed : Error'+IntToStr(WSAGetLastError));
Halt(4);
end
else
begin
a := '本机IP地址为 :';
for i:=1 to 4 do
begin
b := intToStr(Ord(MyHost.h_addr^[i-1]));
if i<4 then
b := b + '.';
a := a + b;
end;
end;
Edit1.Text := a;
end;