Under WIN9X, if dial-up networking is installed, there will be two dial-up network management libraries, RasApi32.DLL and RasApi16.DLL, under the system directory System of the WINDOWS system. We can use the RAS series functions in them to obtain and set up dial-up connections. Network information. Of course, you can obtain the dynamic IP address of the dial-up connection. In the Delphi help file, there are detailed descriptions of related RAS functions.
---- 1. First explain some data constants and data structures to be used.
constRAS_MaxDeviceType = 16; //Device type name length RAS_MaxEntryName = 256; //Maximum connection name length RAS_MaxDeviceName = 128; //Maximum device name length RAS_MaxIpAddress = 15; //Maximum length of IP address Rasp_PppIp = $8021; //Dial-up connection Protocol type, this value indicates PPP connection typeHRASCONN = DWord;//Type of dial-up connection handle RASCONN = record//Handle and setting information of active dial-up connection dwSize: DWORD;//The size of the 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 device type used for active connections szDeviceName : array[0..RAS_MaxDeviceName] of char;//The device name used for active connections end;TRASPPPIP = record//Active dial-up Connected dynamic IP address information dwSize: DWORD;//The size of the memory occupied by this structure (Bytes), generally set to SizeOf(TRASPPPIP) dwError: DWORD;//Error type identifier szIpAddress: array[ 0..RAS_MaxIpAddress] of char;//IP address of active dial-up connection end;
---- 2. Next, we will 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 a pointer to the buffer of active connections var lpcb: DWORD; // Buffer size var lpcConnections : DWORD/ /actual number of active connections): DWORD; stdcall;function RasEnumConnections;external 'Rasapi32.dll' name 'RasEnumConnectionsA';//Get the dynamic IP information of the specified active dial-up connection function RasGetProjectionInfo(hrasconn: HRasConn;//Specify the handle of the active connection rasprojection: DWORD;//RAS connection type var lpprojection: TRASPPPIP;//Receive dynamic IP information Buffer var lpcb: DWord//receive buffer size): DWORD;stdcall;function RasGetProjectionInfo;external 'Rasapi32.dll' name 'RasGetProjectionInfoA';
When the return value of these two functions is 0, it indicates successful execution, and non-zero indicates an error code.
---- 3. Next, we will discuss how to use the two RAS functions above to obtain the dynamic IP address of the dial-up connection.
---- First, use the function RasEnumConnections to list the information of the established active dial-up connections, 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 based on the connection handle. , which includes a member attribute szIpAddress, which is the dynamic IP address. For details, please see the following program fragments and annotation information.
procedure TForm1.Button1Click(Sender: TObject);const MaxConnections = 10;//Assume that there are up to 10 active dial-up connections var connections: array[0..MaxConnections-1] of RASCONN; //Dial-up connection array longSize: dword; intAvailabelConnections : 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 connections begin connections[ 0 ].dwSize := sizeof(RASCONN); longSize := MaxConnections * connections[ 0 ].dwSize;/ /Buffer size for receiving active connections intAvailabelConnections := 0; //Get information about all active dial-up connections (connection handle and setting information) dwResult := RasEnumConnections( connections[ 0 ], longSize,intAvailabelConnections ); if 0 < > dwResult then memo1.lines.add( 'Error:' + inttostr ( dwResult ) ) else begin memo1.lines.add( 'There are existing active connections' + IntToStr( intAvailabelConnections ) + 'one');//Display the information of all active dial-up connections (setting information and dynamic IP address) for intIndex := 0 to intAvailabelConnections - 1 do begin//Display the setting information of 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 ); //Display 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 < > dwResult then memo1.lines.add('Error:' + inttostr( dwResult )) else memo1.lines.add( 'Dynamic address: ' + StrPas(RASpppIP.szIPAddress)); end; end;end;