极点博客 网络软件、编程技术博客

获取本机IP地址的delphi函数

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); //本机内网Ip
end;

标签: delphi基础

作者:xtpyeihc 分类:先前文章 浏览:199 评论:0
留言列表
发表评论
来宾的头像