获取本机IP地址

uses
WinSock;

function LocalIP: String;
type
TaPInAddr = Array[0..10] of PInAddr;
PaPInAddr = ^TaPInAddr;
var
phe: PHostEnt;
pptr: PaPInAddr;
Buffer: Array[0..63] of AnsiChar;
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 := inet_ntoa(pptr^[i]^);
    Inc(i);
end;
WSACleanup;
end;

//测试:
procedure TForm1.Button1Click(Sender: TObject);
begin
ShowMessage(LocalIP); //
end;