delphi编程获取本地IP地址
参考测试了好多个网上的函数,都得不到正确的结果,最终找到一个能用的。环境:delphi5&win7
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
if i=0
then result:=StrPas(inet_ntoa(pptr^[I]^))
else result:=result+ ', '+StrPas(inet_ntoa(pptr^[I]^));
Inc(I);
end;
WSACleanup;
end;