局域网中无法直接获得外网IP,除非你访问一下外网,然后再返回局域网
下面利用www.ip138.com获得本机的外网地址
uses WinInet;
{$R *.dfm}
function GetWebPage(const Url: string):string;
var
Session,
HttpFile:HINTERNET;
szSizeBuffer
ointer;
dwLengthSizeBuffer
Word;
dwReserved
Word;
dwFileSize
Word;
dwBytesRead
Word;
Contents
Char;
begin
Session:=InternetOpen('',0,niL,niL,0);
HttpFile:=InternetOpenUrl(Session,PChar(Url),niL,0,0,0);
dwLengthSizeBuffer:=1024;
HttpQueryInfo(HttpFile,5,szSizeBuffer,dwLengthSizeBuffer,dwReserved);
GetMem(Contents,dwFileSize);
InternetReadFile(HttpFile,Contents,dwFileSize,dwBytesRead);
InternetCloseHandle(HttpFile);
InternetCloseHandle(Session);
Result:=Contents;
FreeMem(Contents);
end;
procedure TForm1.btn1Click(Sender: TObject);
var str:string;
ip:string;
str2,str3:string;
p1,p2,p3,p4:Integer;
begin
ip:=edt1.Text;
str:= GetWebPage('http://www.ip138.com/ );
//
p3:= Pos('您的IP地址是:[',str);
p4:=pos(']',str);
if P3>0 then
begin
str3:=Copy(str,p3,p4-p3);
ip_edit.Text :=str3;
end;
end;