function LocalIP:string; type TaPInAddr = array [0..10] of PInAddr; PaPInAddr = ^TaPInAddr; var phe : PHostEnt; pptr : PaPInAddr; Buffer : array [0..63] of char; I : Integer; GInitData : TWSADATA;begin WSAStartup($101, GInitData); Result := ''; GetHostName(Buffer, SizeOf(Buffer)); phe :=GetHostByName(buffer); if phe = nil then Exit; pptr := PaPInAddr(Phe^.h_addr_list); I := 0; while pptr^[I] <> nil do begin result:=StrPas(inet_ntoa(pptr^[I]^)); Inc(I); end; WSACleanup; end;
uses {...,}Winsock;procedure ShowHostName; var wVersionRequested : WORD; wsaData : TWSAData; p : PHostEnt; s : array[0..128] of char; p2 : pchar; begin {启动 WinSock} wVersionRequested := MAKEWORD(1, 1); WSAStartup(wVersionRequested, wsaData);{计算机名} GetHostName(@s, 128); p := GetHostByName(@s); ShowMessage(Format('Computer name is ''%s''.', [p^.h_Name]));{IP地址} p2 := iNet_ntoa(PInAddr(p^.h_addr_list^)^); ShowMessage(Format('IP address is %s.',[p2]));WSACleanup; end;
type
TaPInAddr = array [0..10] of PInAddr;
PaPInAddr = ^TaPInAddr;
var
phe : PHostEnt;
pptr : PaPInAddr;
Buffer : array [0..63] of char;
I : Integer;
GInitData : TWSADATA;begin
WSAStartup($101, GInitData);
Result := '';
GetHostName(Buffer, SizeOf(Buffer));
phe :=GetHostByName(buffer);
if phe = nil then Exit;
pptr := PaPInAddr(Phe^.h_addr_list);
I := 0;
while pptr^[I] <> nil do begin
result:=StrPas(inet_ntoa(pptr^[I]^));
Inc(I);
end;
WSACleanup;
end;
var
wVersionRequested : WORD;
wsaData : TWSAData;
p : PHostEnt;
s : array[0..128] of char;
p2 : pchar;
begin
{启动 WinSock}
wVersionRequested := MAKEWORD(1, 1);
WSAStartup(wVersionRequested, wsaData);{计算机名}
GetHostName(@s, 128);
p := GetHostByName(@s);
ShowMessage(Format('Computer name is ''%s''.', [p^.h_Name]));{IP地址}
p2 := iNet_ntoa(PInAddr(p^.h_addr_list^)^);
ShowMessage(Format('IP address is %s.',[p2]));WSACleanup;
end;