如何得知拨号上网后的ip地址(100分)

  • 主题发起人 主题发起人 jony123
  • 开始时间 开始时间
J

jony123

Unregistered / Unconfirmed
GUEST, unregistred user!
请问如何得知拨号上网后的ip地址而不是本机的ip地址,如果是通过局域网上网的呢
 
不好意思,这是摘自《葵花宝典》的。
获取拨号连接的动态IP地址


----   在WIN9X下,如果安装了拨号网络,则在WINDOWS系统的系统目录System下将有两个拨号网络管理程序库RasApi32.DLL和RasApi16.DLL,我们可利用其中的RAS系列函数来获取和设置拨号连接网络的信息。当然是可以获取拨号连接的动态IP地址了。在Delphi帮助文件中,有相关RAS函数的详细说明。

---- 一、先解释一些要用到的数据常量和数据结构。

const
RAS_MaxDeviceType = 16;//设备类型名称长度
RAS_MaxEntryName = 256;//连接名称最大长度
RAS_MaxDeviceName = 128;//设备名称最大长度
RAS_MaxIpAddress = 15;//IP地址的最大长度
RASP_PppIp = $8021;//拨号连接的协议类型,该数值表示PPP连接

type
HRASCONN = DWORD;//拨号连接句柄的类型
RASCONN = record//活动的拨号连接的句柄和设置信息
dwSize : DWORD;//该结构所占内存的大小(Bytes),
一般设置为SizeOf(RASCONN)
hrasconn : HRASCONN;//活动连接的句柄
szEntryName : array[0..RAS_MaxEntryName] of char;
//活动连接的名称
szDeviceType : array[0..RAS_MaxDeviceType] of char;
//活动连接的所用的设备类型
szDeviceName : array[0..RAS_MaxDeviceName] of char;
//活动连接的所用的设备名称
end;
TRASPPPIP = record//活动的拨号连接的动态IP地址信息
dwSize : DWORD;//该结构所占内存的大小(Bytes),
一般设置为SizeOf(TRASPPPIP)
dwError : DWORD;//错误类型标识符
szIpAddress : array[ 0..RAS_MaxIpAddress ] of char;
//活动的拨号连接的IP地址
end;
---- 二、接着要解释要用到的两个RAS函数。

//获取所有活动的拨号连接的信息(连接句柄和设置信息)
function RasEnumConnections( var lprasconn : RASCONN ;
//接收活动连接的缓冲区的指针
var lpcb: DWORD;//缓冲区大小
var lpcConnections : DWORD//实际的活动连接数
) : DWORD; stdcall;
function RasEnumConnections;external 'Rasapi32.dll'
name 'RasEnumConnectionsA';
//获取指定活动的拨号连接的动态IP信息
function RasGetProjectionInfo(
hrasconn : HRasConn;//指定活动连接的句柄
rasprojection : DWORD;//RAS连接类型
var lpprojection : TRASPPPIP;//接收动态IP信息的缓冲区
var lpcb : DWord//接收缓冲区的大小
) : DWORD;stdcall;
function RasGetProjectionInfo;external
'Rasapi32.dll' name 'RasGetProjectionInfoA';
  这两个函数的返回值为0时表示执行成功,非0表示错误代码。

---- 三、下面要讨论如何用上面的两个RAS函数获取拨号连接的动态IP地址

----   首先,要用函数RasEnumConnections列出已建立的活动拨号连接的信息,其中包括连接名称、连接句柄、连接设备类型和设备名称;然后根据连接句柄用函数RasGetProjectionInfo获取连接对应的一个TRASPPPIP结构,其中包括一个成员属性szIpAddress即为动态IP地址。具体请参见以下程序片段和注释信息。

procedure TForm1.Button1Click(Sender: TObject);
const
MaxConnections = 10;//假设最多有10个活动的拨号连接
var
connections : array[0..MaxConnections-1] of RASCONN;
//拨号连接数组
longSize : dword;
intAvailabelConnections : dword;
//活动的拨号连接的实际数目
intIndex : integer;
strTemp : string;
dwResult : DWORD;
dwSize : DWORD;
RASpppIP : TRASPPPIP;
//活动的拨号连接的动态IP地址信息
begin
connections[ 0 ].dwSize := sizeof(RASCONN);
longSize := MaxConnections * connections[ 0 ].dwSize;
//接收活动连接的缓冲区大小
intAvailabelConnections := 0;
//获取所有活动的拨号连接的信息(连接句柄和设置信息)
dwResult := RasEnumConnections( connections[ 0 ],
longSize,intAvailabelConnections );
if 0 < > dwResult then
memo1.lines.add( '错误:' + inttostr( dwResult ) )
else
begin
memo1.lines.add( '现有的活动连接有' +
IntToStr( intAvailabelConnections ) + '个');
//显示所有活动的拨号连接的信息(设置信息和动态IP地址)
for intIndex := 0 to intAvailabelConnections - 1 do
begin
//显示一个活动的拨号连接的设置信息
strTemp := '连接名称:'
+ StrPAS( connections[ intIndex ].szEntryName )
+ ',设备类型:'
+ StrPAS( connections[ intIndex ].szDeviceType )
+ ',设备名称:'
+ StrPAS( connections[ intIndex ].szDeviceName );
memo1.lines.add( strTemp );
//显示一个活动的拨号连接的动态IP地址
dwSize := SizeOf(RASpppIP);
RASpppIP.dwSize := dwSize;
dwResult := RASGetProjectionInfo
( connections[ intIndex ].hRasConn,
RASP_PppIp,RasPPPIP,dwSize);//获取动态IP地址
if 0 < > dwResult then
memo1.lines.add(
'错误:' + inttostr( dwResult ))
else
memo1.lines.add(
'动态地址:' + StrPas(RASpppIP.szIPAddress));
end;
end;
end;

 
--非编程方式:在MS-DOS窗口下输入IPCONFIG即可
 
引用RAS单元,使用RasGetIPAddress函数得知拨号上网后的ip地址
function RasGetIPAddress: string;
var
RASConns : TRasConn;
dwSize : DWORD;
dwCount : DWORD;
RASpppIP : TRASPPPIP;
begin
Result := '';
RASConns.dwSize := SizeOf(TRASConn);
RASpppIP.dwSize := SizeOf(RASpppIP);
dwSize := SizeOf(RASConns);
if RASEnumConnectionsA(@RASConns, @dwSize, @dwCount) = 0 then begin
if dwCount > 0 then begin
dwSize := SizeOf(RASpppIP);
RASpppIP.dwSize := SizeOf(RASpppIP);
if RASGetProjectionInfoA(RASConns.hRasConn,
RASP_PppIp,
@RasPPPIP,
@dwSize) = 0 then
Result := StrPas(RASpppIP.szIPAddress);
end;
end;
end;

//---------------------------------------------------------------------------

unit Ras;

interface

uses
Windows, SysUtils;

{$DEFINE WINVER400}
const
RasUnitVersion = 110;
CopyRight : String = 'lenye';
rasapi32 = 'rasapi32.dll';

UNLEN = 256; // Maximum user name length
PWLEN = 256; // Maximum password length
CNLEN = 15; // Computer name length
DNLEN = CNLEN; // Maximum domain name length

RAS_MaxDeviceType = 16;
RAS_MaxPhoneNumber = 128;
RAS_MaxIpAddress = 15;
RAS_MaxIpxAddress = 21;

{$IFDEF WINVER400}
RAS_MaxEntryName = 256;
RAS_MaxDeviceName = 128;
RAS_MaxCallbackNumber = RAS_MaxPhoneNumber;
{$ELSE}
RAS_MaxEntryName = 20;
RAS_MaxDeviceName = 32;
RAS_MaxCallbackNumber = 48;
{$ENDIF}

RAS_MaxAreaCode = 10;
RAS_MaxPadType = 32;
RAS_MaxX25Address = 200;
RAS_MaxFacilities = 200;
RAS_MaxUserData = 200;

RASCS_OpenPort = 0;
RASCS_PortOpened = 1;
RASCS_ConnectDevice = 2;
RASCS_DeviceConnected = 3;
RASCS_AllDevicesConnected = 4;
RASCS_Authenticate = 5;
RASCS_AuthNotify = 6;
RASCS_AuthRetry = 7;
RASCS_AuthCallback = 8;
RASCS_AuthChangePassword = 9;
RASCS_AuthProject = 10;
RASCS_AuthLinkSpeed = 11;
RASCS_AuthAck = 12;
RASCS_ReAuthenticate = 13;
RASCS_Authenticated = 14;
RASCS_PrepareForCallback = 15;
RASCS_WaitForModemReset = 16;
RASCS_WaitForCallback = 17;
RASCS_Projected = 18;

{$IFDEF WINVER400}
RASCS_StartAuthentication = 19;
RASCS_CallbackComplete = 20;
RASCS_LogonNetwork = 21;
{$ENDIF}
RASCS_SubEntryConnected = 22;
RASCS_SubEntryDisconnected= 23;

RASCS_PAUSED = $1000;
RASCS_Interactive = RASCS_PAUSED;
RASCS_RetryAuthentication = (RASCS_PAUSED + 1);
RASCS_CallbackSetByCaller = (RASCS_PAUSED + 2);
RASCS_PasswordExpired = (RASCS_PAUSED + 3);

RASCS_DONE = $2000;
RASCS_Connected = RASCS_DONE;
RASCS_Disconnected = (RASCS_DONE + 1);

// If using RasDial message notifications, get the notification message code
// by passing this string to the RegisterWindowMessageA() API.
// WM_RASDIALEVENT is used only if a unique message cannot be registered.
RASDIALEVENT = 'RasDialEvent';
WM_RASDIALEVENT = $CCCD;

// TRASPROJECTION
RASP_Amb = $10000;
RASP_PppNbf = $0803F;
RASP_PppIpx = $0802B;
RASP_PppIp = $08021;
RASP_Slip = $20000;

type
THRASCONN = THandle;
PHRASCONN = ^THRASCONN;
TRASCONNSTATE = DWORD;
PDWORD = ^DWORD;
PBOOL = ^BOOL;

TRASDIALPARAMS = packed record
dwSize : DWORD;
szEntryName : array [0..RAS_MaxEntryName] of Char;
szPhoneNumber : array [0..RAS_MaxPhoneNumber] of Char;
szCallbackNumber : array [0..RAS_MaxCallbackNumber] of Char;
szUserName : array [0..UNLEN] of Char;
szPassword : array [0..PWLEN] of Char;
szDomain : array [0..DNLEN] of Char;
{$IFDEF WINVER401}
dwSubEntry : DWORD;
dwCallbackId : DWORD;
{$ENDIF}
szPadding : array [0..2] of Char;
end;
PRASDIALPARAMS = ^TRASDIALPARAMS;

TRASDIALEXTENSIONS = packed record
dwSize : DWORD;
dwfOptions : DWORD;
hwndParent : HWND;
reserved : DWORD;
end;
PRASDIALEXTENSIONS = ^TRASDIALEXTENSIONS;

TRASCONNSTATUS = packed record
dwSize : DWORD;
RasConnState : TRASCONNSTATE;
dwError : DWORD;
szDeviceType : array [0..RAS_MaxDeviceType] of char;
szDeviceName : array [0..RAS_MaxDeviceName] of char;
szPadding : array [0..1] of Char;
end;
PRASCONNSTATUS = ^TRASCONNSTATUS;

TRASCONN = packed record
dwSize : DWORD;
hRasConn : THRASCONN;
szEntryName : array [0..RAS_MaxEntryName] of char;
{$IFDEF WINVER400}
szDeviceType : array [0..RAS_MaxDeviceType] of char;
szDeviceName : array [0..RAS_MaxDeviceName] of char;
{$ENDIF}
szPadding : array [0..0] of Char;
end;
PRASCONN = ^TRASCONN;

TRASENTRYNAME = packed record
dwSize : DWORD;
szEntryName : array [0..RAS_MaxEntryName] of char;
szPadding : array [0..2] of Char;
end;
PRASENTRYNAME = ^TRASENTRYNAME;

TRASENTRYDLG = packed record
dwSize : DWORD;
hWndOwner : HWND;
dwFlags : DWORD;
xDlg : LongInt;
yDlg : LongInt;
szEntry : array [0..RAS_MaxEntryName] of char;
dwError : DWORD;
Reserved : DWORD;
Reserved2 : DWORD;
szPadding : array [0..2] of Char;
end;
PRASENTRYDLG = ^TRASENTRYDLG;

TRASPROJECTION = integer;
TRASPPPIP = record
dwSize : DWORD;
dwError : DWORD;
szIpAddress : array [0..RAS_MaxIpAddress] of char;
end;


function RasDialA(RasDialExtensions: PRASDIALEXTENSIONS;
PhoneBook : PChar;
RasDialParams : PRASDIALPARAMS;
NotifierType : DWORD;
Notifier : Pointer;
RasConn : PHRASCONN
): DWORD; stdcall;
function RasGetErrorStringA(
uErrorValue : DWORD; // error to get string for
szErrorString : PChar; // buffer to hold error string
cBufSize : DWORD // size, in characters, of buffer
): DWORD; stdcall;
function RasHangupA(RasConn: THRASCONN): DWORD; stdcall;
function RasConnectionStateToString(nState : Integer) : String;
function RasGetConnectStatusA(
hRasConn: THRASCONN; // handle to RAS connection of interest
lpRasConnStatus : PRASCONNSTATUS // buffer to receive status data
): DWORD; stdcall;
function RasEnumConnectionsA(
pRasConn : PRASCONN; // buffer to receive connections data
pCB : PDWORD; // size in bytes of buffer
pcConnections : PDWORD // number of connections written to buffer
) : DWORD; stdcall
function RasEnumEntriesA(
Reserved : Pointer; // reserved, must be NIL
szPhonebook : PChar; // full path and filename of phonebook file
lpRasEntryName : PRASENTRYNAME; // buffer to receive entries
lpcb : PDWORD; // size in bytes of buffer
lpcEntries : PDWORD // number of entries written to buffer
) : DWORD; stdcall;
function RasGetEntryDialParamsA(
lpszPhonebook : PChar; // pointer to the full path and filename of the phonebook file
lprasdialparams : PRASDIALPARAMS; // pointer to a structure that receives the connection parameters
lpfPassword : PBOOL // indicates whether the user's password was retrieved
) : DWORD; stdcall;
function RasEditPhonebookEntryA(
hWndParent : HWND; // handle to the parent window of the dialog box
lpszPhonebook : PChar; // pointer to the full path and filename of the phonebook file
lpszEntryName : PChar // pointer to the phonebook entry name
) : DWORD; stdcall;
//function RasEntryDlgA(
// lpszPhonebook : PChar; // pointer to the full path and filename of the phone-book file
// lpszEntry : PChar; // pointer to the name of the phone-book entry to edit, copy, or create
// lpInfo : PRASENTRYDLG // pointer to a structure that contains additional parameters
// ) : DWORD; stdcall;
function RasCreatePhonebookEntryA(
hWndParent : HWND; // handle to the parent window of the dialog box
lpszPhonebook : PChar // pointer to the full path and filename of the phonebook file
) : DWORD; stdcall;

function RasGetProjectionInfoA(
hRasConn : THRASCONN; // handle that specifies remote access connection of interest
RasProjection : TRASPROJECTION; // specifies type of projection information to obtain
lpProjection : Pointer; // points to buffer that receives projection information
lpcb : PDWORD // points to variable that specifies buffer size
) : DWORD; stdcall;
function RasGetIPAddress: string;

implementation


{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
function RasConnectionStateToString(nState : Integer) : String;
begin
case nState of
RASCS_OpenPort: Result := 'Opening Port';
RASCS_PortOpened: Result := 'Port Opened';
RASCS_ConnectDevice: Result := 'Connecting Device';
RASCS_DeviceConnected: Result := 'Device Connected';
RASCS_AllDevicesConnected: Result := 'All Devices Connected';
RASCS_Authenticate: Result := 'Starting Authentication';
RASCS_AuthNotify: Result := 'Authentication Notify';
RASCS_AuthRetry: Result := 'Authentication Retry';
RASCS_AuthCallback: Result := 'Callback Requested';
RASCS_AuthChangePassword: Result := 'Change Password Requested';
RASCS_AuthProject: Result := 'Projection Phase Started';
RASCS_AuthLinkSpeed: Result := 'Link Speed Calculation';
RASCS_AuthAck: Result := 'Authentication Acknowledged';
RASCS_ReAuthenticate: Result := 'Reauthentication Started';
RASCS_Authenticated: Result := 'Authenticated';
RASCS_PrepareForCallback: Result := 'Preparation For Callback';
RASCS_WaitForModemReset: Result := 'Waiting For Modem Reset';
RASCS_WaitForCallback: Result := 'Waiting For Callback';
RASCS_Projected: Result := 'Projected';
{$IFDEF WINVER400}
RASCS_StartAuthentication: Result := 'Start Authentication';
RASCS_CallbackComplete: Result := 'Callback Complete';
RASCS_LogonNetwork: Result := 'Logon Network';
{$ENDIF}
RASCS_SubEntryConnected: Result := '';
RASCS_SubEntryDisconnected: Result := '';
RASCS_Interactive: Result := 'Interactive';
RASCS_RetryAuthentication: Result := 'Retry Authentication';
RASCS_CallbackSetByCaller: Result := 'Callback Set By Caller';
RASCS_PasswordExpired: Result := 'Password Expired';
RASCS_Connected: Result := 'Connected';
RASCS_Disconnected: Result := 'Disconnected';
else
Result := 'Connection state #' + IntToStr(nState);
end;
end;


{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
function RasGetIPAddress: string;
var
RASConns : TRasConn;
dwSize : DWORD;
dwCount : DWORD;
RASpppIP : TRASPPPIP;
begin
Result := '';
RASConns.dwSize := SizeOf(TRASConn);
RASpppIP.dwSize := SizeOf(RASpppIP);
dwSize := SizeOf(RASConns);
if RASEnumConnectionsA(@RASConns, @dwSize, @dwCount) = 0 then begin
if dwCount > 0 then begin
dwSize := SizeOf(RASpppIP);
RASpppIP.dwSize := SizeOf(RASpppIP);
if RASGetProjectionInfoA(RASConns.hRasConn,
RASP_PppIp,
@RasPPPIP,
@dwSize) = 0 then
Result := StrPas(RASpppIP.szIPAddress);
end;
end;
end;



{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
function RasDialA; external rasapi32 name 'RasDialA';
function RasGetErrorStringA; external rasapi32 name 'RasGetErrorStringA';
function RasHangUpA; external rasapi32 name 'RasHangUpA';
function RasGetConnectStatusA; external rasapi32 name 'RasGetConnectStatusA';
function RasEnumConnectionsA; external rasapi32 name 'RasEnumConnectionsA';
function RasEnumEntriesA; external rasapi32 name 'RasEnumEntriesA';
function RasGetEntryDialParamsA; external rasapi32 name 'RasGetEntryDialParamsA';
function RasEditPhonebookEntryA; external rasapi32 name 'RasEditPhonebookEntryA';
//function RasEntryDlgA; external rasapi32 name 'RasEntryDlgA';
function RasCreatePhonebookEntryA; external rasapi32 name 'RasCreatePhonebookEntryA';
function RasGetProjectionInfoA; external rasapi32 name 'RasGetProjectionInfoA';

{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}

end.


 
如果你是通过路由器拔号上网的话,其实你最终得到的IP地址可能是你的出网的上层再上层的地址,换句话说你可能得到ISP网关出网的IP。
 
后退
顶部