中国开发网: 论坛: Delphi/BCB: 贴子 75797
pcplayer: Get local IP
The following code is not dependent on Windows 2000 or Active directory, will

list all IP addresses of the interfaces of the (IPV4) host it runs on, and

is portable to Linux as it uses standard socket calls:

uses WinSock;

....

{ lists all IP-address assigned to this host }

const

MAXHOSTNAMELEN = 256;

var

HostName: string;

pHE: PHostEnt;

pAddr: ^PChar;

begin

{ get hostname }

SetLength(HostName, MAXHOSTNAMELEN);

GetHostName(Pointer(HostName), MAXHOSTNAMELEN);

SetLength(HostName, StrLen(Pointer(HostName)));

{ host name }

ShowMessage(HostName);

{ get list of ip adresses }

pHE := GetHostByName(Pointer(HostName));

{ host name again}

ShowMessage(pHE.h_name);

{ lenght of an IP address, 4 = IPV4 }

ShowMessage(IntToStr(pHE.h_length));

{ list all adresses }

pAddr := Pointer(pHE.h_addr);

while PChar(pAddr^) <> Nil do

begin

ShowMessage(inet_ntoa(in_addr(pAddr^)));

inc(pAddr);

end;

end;

相关信息:


欢迎光临本社区,您还没有登录,不能发贴子。请在 这里登录