双网卡,以下代码获取的IP为什么会是: 192.168.1.2 ??我想获取外网的IP(ADSL)
function GetLocalIP: String;
type
TPinAddr=Array[0..10] of PinAddr;
TMyPinAddr=^TPinAddr;
Var
MyPinAddr:TMyPinAddr;
Phe:PhostEnt;
Buffer:Array[0..63] of Char;
GInitData:TWSAData;
I : Integer;
//J:integer;
begin
WSAStartUp($101,GinitData); //初始化WinSock
try
result:='';
GetHostName(Buffer,Sizeof(Buffer));
Phe:=GetHostByName(Buffer); //得到IP地址存放出的地址
if Phe=nil then //如果机器里没有IP地址
exit;
MyPinAddr:=TMyPinAddr(Phe^.h_addr_list);
I:=0;
while MyPinAddr[i]<>nil do
begin
result:=StrPas(Inet_Ntoa(MyPinAddr^[i]^));
inc(i);
end;
finally
WSACleanUp;
end;end;
function GetLocalIP: String;
type
TPinAddr=Array[0..10] of PinAddr;
TMyPinAddr=^TPinAddr;
Var
MyPinAddr:TMyPinAddr;
Phe:PhostEnt;
Buffer:Array[0..63] of Char;
GInitData:TWSAData;
I : Integer;
//J:integer;
begin
WSAStartUp($101,GinitData); //初始化WinSock
try
result:='';
GetHostName(Buffer,Sizeof(Buffer));
Phe:=GetHostByName(Buffer); //得到IP地址存放出的地址
if Phe=nil then //如果机器里没有IP地址
exit;
MyPinAddr:=TMyPinAddr(Phe^.h_addr_list);
I:=0;
while MyPinAddr[i]<>nil do
begin
result:=StrPas(Inet_Ntoa(MyPinAddr^[i]^));
inc(i);
end;
finally
WSACleanUp;
end;end;
var
AIdStack:TIdStack;
begin
AIdStack:=TIdStack.CreateStack;
try
outdata.Text:=AIdStack.LocalAddresses.Text;//获取本地IP地址列表
finally
AIdStack.free;
end;
end;用这个试试