var
WSData: TWSAData;
Buffer: array[0..63] of Char;
HostEnt: PHostEnt;
PPInAddr: ^PInAddr;
LocalIP: DWord;
IPString: string;
procedure GetIP;
begin
LocalIP := 0;
IPString := '';
try
WSAStartUp($101, WSData);
GetHostName(Buffer, SizeOf(Buffer));
HostEnt := GetHostByName(Buffer);
if Assigned(HostEnt) then
begin
PPInAddr := @(PInAddr(HostEnt.H_Addr_List^));
while Assigned(PPInAddr^) do
begin
IPString := StrPas(INet_NToA(PPInAddr^^));
LocalIP := PPInAddr^^.S_Addr;
Inc(PPInAddr);
end;
end;
finally
try
WSACleanUp;
except
end;
end;
end;
//取本机IP地址,返回4字节格式
function GetIPAddress: DWORD;
begin
GetIP;
Result := LocalIP;
end;
//取本机IP地址,返回点分隔字符串格式
function GetLocalIP: string;
begin
GetIP;
Result := IPString;
end;