如何在程序中取得ADSL的动态IP。(10分)

  • 主题发起人 主题发起人 peterzhou20
  • 开始时间 开始时间
P

peterzhou20

Unregistered / Unconfirmed
GUEST, unregistred user!
因为需要在程序中实时的知道当前ADSL的动态IP,请教高手如何实现。
 
关注。
部分ADSL可以使用Tapi获取动态的ip地址
 
给出代码吧!!
 
function IsConnecting : boolean;
var
BufSize : LongInt;
NumEntries : Integer;
Entries : Array[1..2] of TRasConn;
begin
// 初始化设置
Entries[1].dwSize := SizeOf(TRasConn);
Bufsize:=SizeOf(Entries);
// 调用函数获取连接个数
if (RasEnumConnections(@Entries[1], BufSize, NumEntries) = 0) then
begin
// 如果连接个数大于0,表示已经拨号上网了
if NumEntries > 0 then
Result := true
else //没有拨号上网
Result := false;
end;

Result := false;
exit;
end;
 
测试中....
 
type
TaPInAddr = Array[0..10] of PInAddr;
PaPInAddr = ^TaPInAddr;
var
ip:string;
phe: PHostEnt;
pptr: PaPInAddr;
Buffer: Array[0..63] of Char;
I: Integer;
GInitData: TWSAData;
begin
image1.Picture.loadfromfile(extractfiledir(application.exename)+'/img/开始.bmp');

Screen.Cursor := crHourGlass;
try
WSAStartup($101, GInitData);
IP:='0.0.0.0';
GetHostName(Buffer, SizeOf(Buffer));
phe := GetHostByName(buffer);
if phe = nil then
begin
statusbar1.Panels[3].Text :=ip;
Exit;
end;
pPtr := PaPInAddr(phe^.h_addr_list);
I := 0;
while pPtr^ <> nil do
begin
IP := inet_ntoa(pptr^^);
Inc(I);
end;
WSACleanup;
statusbar1.Panels[3].text:=ip;//如果上网则为上网ip否则是网卡ip
finally
Screen.Cursor := crDefault;
end;
end;
 
TRasConn这个类在哪里啊?
 
这个类是TAPI里面的结构定义,里面的含义可参考msdn里面的RASCONN结构,pascal中定义如下:
(摘自Ras.pas)
LPHRasConn = ^THRasConn;
THRasConn = Longint;


{* Identifies an active RAS connection. (See RasEnumConnections)
*}
LPRasConnW = ^TRasConnW;
TRasConnW = record
dwSize: Longint;
hrasconn: THRasConn;
szEntryName: Array[0..RAS_MaxEntryName] of WideChar;
{$IFNDEF WINVER31}
szDeviceType: Array[0..RAS_MaxDeviceType] of WideChar;
szDeviceName: Array[0..RAS_MaxDeviceName] of WideChar;
{$ENDIF}
{$IFDEF WINVER41}
szPhonebook: Array[0..MAX_PATH - 1] of WideChar;
dwSubEntry: Longint;
{$ENDIF}
end;

LPRasConnA = ^TRasConnA;
TRasConnA = record
dwSize: Longint;
hrasconn: THRasConn;
szEntryName: Array[0..RAS_MaxEntryName] of AnsiChar;
{$IFNDEF WINVER31}
szDeviceType: Array[0..RAS_MaxDeviceType] of AnsiChar;
szDeviceName: Array[0..RAS_MaxDeviceName] of AnsiChar;
{$ENDIF}
{$IFDEF WINVER41}
szPhonebook: Array[0..MAX_PATH - 1] of AnsiChar;
dwSubEntry: Longint;
{$ENDIF}
end;

LPRasConn = ^TRasConn;
TRasConn = TRasConnA;

 
接受答案了.
 
后退
顶部