p> Under WIN9X, if dial-up network is installed, there will be two dial-up network governance program libraries RasApi32.DLL and RasApi16.DLL under the system directory System of the WINDOWS system. We can use the RAS series functions to obtain and set Information about dialing up to connect to the network. Of course, you can get the dynamic IP address of the dial-up connection. In the Delphi help file, there are specific instructions for related RAS functions. 1. First explain some data constants and data structures to be used. const
RAS_MaxDeviceType = 16;//Device type name length
RAS_MaxEntryName = 256;//Maximum length of connection name
RAS_MaxDeviceName = 128;//Maximum length of device name
RAS_MaxIpAddress = 15;//Maximum length of IP address
Rasp_PppIp = $8021;//The protocol type of dial-up connection, this value represents the PPP connection type
HRASCONN = DWord;//Type of dial-up connection handle
RASCONN = record//The handle and setting information of the active dial-up connection
dwSize : DWORD;//The size of memory occupied by this structure (Bytes),
Generally set to SizeOf(RASCONN)
hrasconn: HRASCONN;//The handle of the active connection
szEntryName : array[0..RAS_MaxEntryName] of char;
//The name of the active connection
szDeviceType : array[0..RAS_MaxDeviceType] of char;
//The type of equipment used for the active connection
szDeviceName : array[0..RAS_MaxDeviceName] of char;
//The device name used for the active connection
end;
TRASPPPIP = record//Dynamic IP address information for active dial-up connections
dwSize : DWORD;//The size of memory occupied by this structure (Bytes),
Generally set to SizeOf(TRASPPPIP)
dwError: DWORD;//Error type identifier
szIpAddress : array[ 0..RAS_MaxIpAddress ] of char;
//The IP address of the active dial-up connection
end;
2. Next, we need to explain the two RAS functions to be used. //Get information about all active dial-up connections (connection handle and setting information)
function RasEnumConnections( var lPRasconn : RASCONN ;
//Receive the pointer of the buffer of the active connection
var lpcb: DWORD;//Buffer size
var lpcConnections: DWORD//The actual number of active connections
) : DWORD; stdcall;
function RasEnumConnections; external Rasapi32.dll
name RasEnumConnectionsA;
//Get dynamic IP information for the specified active dial-up connection
function RasGetProjectionInfo(
hrasconn: HRasConn;//Specify the handle for the active connection
rasprojection: DWORD;//RAS connection type
var lpprojection: TRASPPPIP;//Buffer for receiving dynamic IP information
var lpcb: DWord//Receive buffer size
) : DWORD;stdcall;
ction RasGetProjectionInfo;external
Rasapi32.dll name RasGetProjectionInfoA;
When the return value of these two functions is 0, it means the execution is successful, and non-0 means the error code. 3. The following discussion is about how to use the above two RAS functions to obtain the dynamic IP address of a dial-up connection. First, use the function RasEnumConnections to list the information of the established active dial-up connection, including the connection name, connection handle, connection device type and device Name; then use the function RasGetProjectionInfo to obtain a TRASPPPIP structure corresponding to the connection, including a member attribute szIpAddress, which is the dynamic IP address. For details, please refer to the program snippets and comment information below. procedure TForm1.Button1Click(Sender: TObject);
const
MaxConnections = 10;//Suppose there are up to 10 active dial-up connections
var
connections : array[0..MaxConnections-1] of RASCONN;
//Dial-up connection array
longSize : dword;
intAvailableConnections : dword;
//The actual number of active dial-up connections
intIndex : integer;
strTemp : string;
dwResult : DWORD;
dwSize : DWORD;
RASpppIP: TRASPPPIP;
// Dynamic IP address information of active dial-up connection
Begin
connections[ 0 ].dwSize := sizeof(RASCONN);
longSize := MaxConnections * connections[ 0 ].dwSize;
//Receive the buffer size of the active connection
intAvailableConnections := 0;
//Get information about all active dial-up connections (connection handle and setting information)
dwResult := RasEnumConnections( connections[ 0 ],
longSize,intAvailableConnections);
if 0 $#@60; $#@62; dwResult then
memo1.lines.add(Error: + inttostr( dwResult ) )
else
Begin
memo1.lines.add (the existing active connection has +
IntToStr( intAvailableConnections ) + );
//Show information about all active dial-up connections (setting information and dynamic IP address)
for intIndex := 0 to intAvailableConnections - 1 do
Begin
//Show settings information for an active dial-up connection
strTemp := Connection name:
+ StrPAS( connections[ intIndex ].szEntryName )
+ , device type:
+ StrPAS( connections[ intIndex ].szDeviceType )
+ , device name:
+ StrPAS( connections[ intIndex ].szDeviceName );
memo1.lines.add( strTemp);
//Show the dynamic IP address of an active dial-up connection
dwSize := SizeOf(RASpppIP);
RASpppIP.dwSize := dwSize;
dwResult := RASGetProjectionInfo
( connections[ intIndex ].hRasConn,
RASP_PppIp,RasPPPIP,dwSize);//Get dynamic IP address
if 0 $#@60; $#@62; dwResult then
memo1.lines.add(
Error: + inttostr( dwResult ))
else
memo1.lines.add(
Dynamic address: + StrPas(RASpppIP.szIPAddress));
end;
end;
end;
The above program was debugged and passed under PWIN98+Delphi3.0.