Delphi dialing Internet access code

Source: Internet
Author: User
Tags call back

Resend
Using modem to dial up the Internet is still the method that most individual netizens choose to access the Internet.
Manage dialing in our application
Network (such as the dial-up function in Foxmail and Sygate) will undoubtedly facilitate our software
Users (no longer need to switch applications,
Run the dial-up network) to improve the friendliness of our software and thus improve the competition of software.
Force.
If a dial-up network is installed in Win9x
There will be two dial-up network management under the System
Library rasapi32.dll and rasapi16.dll. We can use the functions to obtain
Obtains and sets the information of the dial-up connection network. In
Descriptions of related functions are provided in the Delphi help file.

Here, we will discuss the following management projects:
1. Obtain the name of the dial-up connection available in the current system.
2. Create a dial-up connection and modify the attributes of the dial-up connection
3. Obtain and set the dialing parameters for the dial-up connection
4. Use the specified dial-up connection to dial up or stop the specified dial-up connection
5. Obtain the current active connection and connection status.

Zero. Get the error message of rasapi function execution failure
The Calling interfaces of rasapi are unified, but for different Windows versions, many
The definitions of constants and data structures are different.
If the data structure used does not match the Windows version, rasapi function execution will fail.
Loss; in addition, other causes, such as other programs
When using the same dial-up connection for dialing, it also causes rasapi function execution loss.
Failure. For this reason, we need to discuss
The function that obtains the error message of rasapi function execution failure.
When the execution of a rasapi function ends, a result ID is returned. If the value is 0, the table is
Indicates that the execution is successful. Otherwise, it is used as an error mark.
The identifier indicates the cause of execution failure. The rasapi function rasgeterrorstring can be
The error identifier returns its error description,
A Chinese error message can be provided in Win9x.
Rasgeterrorstring function prototype:
Function rasgeterrorstring (
Uerrorvalue: uint; // error ID
Lpszerrorstring: pchar; // buffer for error message
Cbufsize: DWORD // buffer size of error message
): DWORD; stdcall;
Function rasgeterrorstring; External rasapidll name
'Rasgeterrorstrencga ';
(Note: rasapidll = 'rasapi32. dll ';)

For convenience, we can compile a function to obtain the rasapi function.
Error message of failed execution. In the following example
Child, this function is often used. The function code is as follows:
{Get the error message of rasapi function execution failure based on the error identifier}
Function getraserror (errorid: uint): string;
VaR
Buffer: array [0 .. 255] of char;
Begin
If 0 = rasgeterrorstring (errorid, buffer, 256) then
Result: = strpas (buffer) // if the error message is returned correctly, convert
Returns a Pascal string.
Else // otherwise, an error code in hexadecimal format is returned.
Result: = 'getraserror failure: errorid = '+ format (
'% X', [errorid]);
End;

1. Obtain the available dialing connection names in the current system
Obtain the name of the dial-up connection established in the system.
Which dialing connection is used for dialing.
Two methods can be used for implementation. One is to use rasapi functions, and the other is not
Use the rasapi function to directly query data in the registry.

1. Directly query in the Registry without using the rasapi Function
In the Registry
In HKEY_USERS/. Default/RemoteAccess/addresses
Set up in Dial-Up Network
The name of the dial-up connection and its attribute settings. The name of each project is
The name of the dial-up connection.
Set the attributes of each dial-up connection, but it is a binary string.
You can see that we only need to read the name of each project.
To obtain the name of the dial-up connection available in the current system.
VaR
Registrytemp: Tregistry;
Stringstemp: tstringlist;
Begin
Registrytemp: = Tregistry. Create;
Stringstemp: = tstringlist. Create;
With registrytemp do
Begin
Rootkey: = HKEY_USERS; // set the root key to HKEY_USERS.
// If the sub-key. Default/RemoteAccess/addresses exists
If openkey ('. Default/RemoteAccess/SES SSEs', false) then
Getvaluenames (stringstemp); // read the name of each project, that is, a dial-up connection.
Name
End;
Combobox1.items. Assign (stringstemp); // display, for selection
End;

2. Use rasapi Functions
The rasapi function rasenumentries can be used to obtain the available dialing connection names in the current system.
It is called. Its function prototype is
Function rasenumentries (
Reserved: pchar; // reserved field, which must be nil
Lpszphonebook: pchar; // name of the phone book. It is useless in Win9x and can be empty characters.
String
Lprasentryname: lprasentryname; // The buffer that receives the name of the dial-up connection,
Is a pointer to a rasentryname array.
VaR lpcb: DWORD; // the buffer size (bytes) of the name of the received dial-up connection)
VaR lpcentries: DWORD // The number of actually obtained dial-up connection names
): DWORD; stdcall;
Function rasenumentries; External rasapidll name
'Rasenumentriesa ';

The lprasentryname parameter provides a pointer to the rasentryname array
Buffer for receiving a dial-up connection name
The rasentryname and lprasentryname types are described as follows:

Lprasentryname = ^ rasentryname;
Rasentryname = record
Dwsize: DWORD; // The memory size occupied by the structure (bytes), which is generally set
Sizeof (rasentryname)
Szentryname: array [0 .. ras_maxentryname] of char; // dial
Connection name
End;
Lpcb is the buffer size. Generally, it is set to a multiple of dwsize, with a multiple
The number of possible connections.
Number of actual connections of the lpcentries instance.

The following is an application example listing the available dial-up connections in the current system.
Name.
Note: You should set it in the first rasentryname structure of the rasentryname buffer.
Dwsize.
Const
Maxphoneentries = 10; // maximum number of dial-up connections
VaR
Intindex: integer;
Phoneentries: array [0 .. maxphoneentries-1]
Rasentryname;
Dwsize, dwentries, dwresult: DWORD;
Begin
// Set dwsize In the first rasentryname structure of the rasentryname Buffer
Phoneentries [0]. dwsize: = sizeof (rasentryname );

Dwsize: = maxphoneentries * sizeof (rasentryname); // Buffer
Area size
// Call the rasapi function to obtain the name of the dial-up connection available in the current system
Dwresult: = rasenumentries (nil, '', @ phoneentries [0
], Dwsize, dwentries );

If dwresult <> 0 then
Begin // rasapi function, execution error
Memo1.lines. Add ('rasenumentries error: '+ getraserror (
Dwresult ));
Exit;
End;

// Display the available dial-up connection names in the current system
Memo1.lines. Add ('Total' + inttostr (dwentries) + 'Ras connections
, As shown below ');
For intindex: = 0 to dwentries-1 do
Memo1.lines. Add (strpas (phoneentries [intindex
]. Szentryname ));
End;

3. Get the name of the default dial-up connection
The default dial-up connection is the dial-up connection set by the user in the browser.
Connections can be considered the most commonly used by users.
.
In the Registry's HKEY_USERS/. Default/RemoteAccess location
A string name profile, which corresponds
The character value is HKEY_USERS/. Default/RemoteAccess/addresses.

Ii. Create a dial-up connection and modify the attributes of a dial-up connection
Rasapi functions rascreatephonebookentry,
Raseditphonebookentry
Allows you to create a new dial-up connection.
Modify the dial-up connection.
The setting operation is also completed by the system.
1. Create a dial-up connection
The rasapi function for creating a dial-up connection is rascreatephonebookentry,
Its function prototype is:
Function rascreatephonebookentry (
Hwnd: thandle; // handle of the parent window of the new dial-up connection window, which is nil
Desktop)
Lpszphonebook: pchar // The name of the phone book. It does not work in Win9x and can be a null character.
String
): DWORD; stdcall;
Function rascreatephonebookentry; External rasapidll name
'Rascreatephonebookentrya ';

If the return value of the function is 0, the execution is successful. Otherwise, the returned value is the error code.

The following is an example of an application that allows you to create a dial-up connection.
VaR
Dwresult: DWORD;
Begin
// Create a dial-up connection in the current window
Dwresult: = rascreatephonebookentry (handle ,'');
If dwresult = 0 then
Memo1.lines. Add ('new dial-up connection successful! ')
Else
Memo1.lines. Add ('new dial-up connection failed :! '+ Getraserror (
Dwresult ))
End;

2. Modify the attributes of a dial-up connection
The rasapi function for modifying the attributes of a dial-up connection is
Raseditphonebookentry, whose function prototype is:
Function raseditphonebookentry (
Hwnd: thandle; // handle of the parent window of the new dial-up connection window, which is nil
Desktop)
Lpszphonebook: pchar; // name of the phone book. It is useless in Win9x and can be empty characters.
String
Lpszentryname: pchar // name of the dial-up connection, for example, '123456' or '1234568'
): DWORD; stdcall;
Function raseditphonebookentry; External rasapidll name
'Raseditphonebookentrya ';

If the return value of the function is 0, the execution is successful. Otherwise, the returned value is the error code.
The following is an example of an application that allows users to modify the attributes of a specified dial-up connection.
.
VaR
Dwresult: DWORD;
Begin
// Modify the attributes of the dial-up connection in the current window
Dwresult: = raseditphonebookentry (handle, '', '123 ');
If dwresult = 0 then
Memo1.lines. Add ('dial-up connection modified successfully! ')
Else
Memo1.lines. Add ('failed to modify the dial-up connection :! '+ Getraserror (
Dwresult ))
End;

3. Obtain and set the dialing parameters for a dial-up connection
Use the rasapi function rasgetentrydialparams,
Rassetentrydialparams can directly obtain and set the specified dialing
The connection dialing parameters, including the user name and user password!
1. Obtain the dialing parameters of the dial-up connection.
Obtain the dialing parameter of the dial-up connection. The rasapi function is
Rasgetentrydialparams:
Function rasgetentrydialparams (
Lpszphonebook: pchar; // name of the phone book. It is useless in Win9x and can be empty characters.
String
Prasdialparams: lprasdialparams; // the dialing parameter, which is
Rasdialparams pointer
VaR lpfpassword: wordbool // whether the user password is required
): DWORD; stdcall;
Function rasgetentrydialparams; External rasapidll name
'Rasgetentrydialparamsa ';

The prasdialparams parameter is a rasdialparams pointer pointing to
Dialing parameter data connected by no.
Here, the rasdialparams and lprasdialparams types are described as follows:
Below:
Lprasdialparams = ^ rasdialparams;
Rasdialparams = record
Dwsize: DWORD; // The memory size occupied by the structure (bytes), which is generally set
Sizeof (rasdialparams)
Szentryname: array [0 .. ras_maxentryname] of char; // dial-up connection
Name
Szphonenumber: array [0 .. ras_maxphonenumber] of char; // dial
Number
Szcallbacknumber: array [0 .. ras_maxcallbacknumber]
Char; // call back number
Szusername: array [0 .. unlen] of char; // User Name
Szpassword: array [0 .. pwlen] of char; // User Password
Szdomain: array [0 .. dnlen] of char; // Domain Name
End;
If the return value of the function is 0, the execution is successful. Otherwise, the returned value is the error code.
The following is an application example to obtain the dialing parameters of a specified dial-up connection.
VaR
Dwresult: DWORD;
Rasdialparamsdata: rasdialparams;
Needpwd: wordbool;
Begin
// Specify the name of the dial-up connection
With rasdialparamsdata do
Begin
Dwsize: = sizeof (rasdialparams); // structure size
Szentryname: = '000000'; // specify the name of the dial-up connection.
Szusername: = ''; // initialize the other five parameters.
Szpassword: = '';
Szdomain: = '';
Szcallbacknumber: = '';
Szphonenumber: = '';
End;
Needpwd: = true; // The user password is required.

// Obtain the dialing parameters of the specified dial-up connection
Dwresult: = rasgetentrydialparams (
'', @ Rasdialparamsdata, needpwd );

If dwresult <> 0 then // failed to get the dialing parameter of the specified dial-up connection
Memo1.lines. Add ('Get
'+ Strpas (rasdialparamsdata. szentryname) + 'dialing parameter failed :'
+ Getraserror (dwresult ))
Else
Begin // display the dialing parameters of the specified dial-up connection
Memo1.lines. Add (strpas (rasdialparamsdata. szentryname) + 'dial
');
Memo1.lines. Add ('user name: '+
Strpas (rasdialparamsdata. szusername ));
Memo1.lines. Add ('user password: '+
Strpas (rasdialparamsdata. szpassword ));
Memo1.lines. Add ('domain name: '+
Strpas (rasdialparamsdata. szdomain ));
Memo1.lines. Add ('call back number: '+
Strpas (rasdialparamsdata. szcallbacknumber ));
Memo1.lines. Add ('dial-up number: '+
Strpas (rasdialparamsdata. szphonenumber ));
End;
End;
2. Set the dialing parameters for the dial-up connection
Set the parameter rasapi
Rassetentrydialparams:
Function rassetentrydialparams (
Lpszphonebook: pchar; // name of the phone book. It is useless in Win9x and can be empty characters.
String
Prasdialparams: lprasdialparams; // the dialing parameter, which is
Rasdialparams pointer
VaR lpfpassword: wordbool // whether to delete the User Password
): DWORD; stdcall;
Function rassetentrydialparams; External rasapidll name
'Rassetentrydialparamsa ';
The description of prasdialparams is the same as that of rasgetentrydialparams.
The meaning of the lpfpassword parameter is different from that of rasgetentrydialparams.
Whether to delete the user password, which is
If this parameter is set to true, the original user password is deleted.
    
If the return value of the function is 0, the execution is successful. Otherwise, the returned value is the error code.
The following is an example of setting the dialing parameters for a specified dial-up connection.
VaR
Dwresult: DWORD;
Rasdialparamsdata: rasdialparams;
Removepwd: wordbool;
Begin
// Specify the dialing parameters for the dial-up connection
With rasdialparamsdata do
Begin
Dwsize: = sizeof (rasdialparams); // structure size
Szentryname: = '000000'; // specify the name of the dial-up connection.
Szusername: = 'myname'; // specify the user name
Szpassword: = 'mypassword'; // specify the User Password
Szdomain: = 'mydomain'; // specifies the Domain Name
Szcallbacknumber: = ''; // specify the call back number
Szphonenumber: = ''; // specify the dialing number
End;
Removepwd: = false; // you do not need to delete the user password.

// Set the dialing parameters for the specified dial-up connection
Dwresult: = rassetentrydialparams (
'', @ Rasdialparamsdata, removepwd );

If dwresult <> 0 then // failed to set the dialing parameter for the specified dial-up connection
Memo1.lines. Add ('settings
'+ Strpas (rasdialparamsdata. szentryname) + 'dialing parameter failed :'
+ Getraserror (dwresult ))
Else
Begin // display the dialing parameters of the specified dial-up connection
Memo1.lines. Add (strpas (rasdialparamsdata. szentryname) + 'dial
Parameter ');
Memo1.lines. Add ('user name: '+
Strpas (rasdialparamsdata. szusername ));
Memo1.lines. Add ('user password: '+
Strpas (rasdialparamsdata. szpassword ));
Memo1.lines. Add ('domain name: '+
Strpas (rasdialparamsdata. szdomain ));
Memo1.lines. Add ('call back number: '+
Strpas (rasdialparamsdata. szcallbacknumber ));
Memo1.lines. Add ('dial-up number: '+
Strpas (rasdialparamsdata. szphonenumber ));
End;
End;

4. Use a specified dial-up connection to dial up or stop a specified dial-up connection
1. Use the specified dial-up connection
There are two dialing Methods: one is to call the dialing program in the dialing network
Double-click the name of the dial-up connection.
You can modify the dialing number, user name, and user password.
Number network to complete the dialing process; another method is
Is to call the rasapi function.
(1) dialup program in the dial-up Network
The following code can be used in the program:
Winexec('rundll32.exe rnaui. dll, rnadial 169 ',
Sw_shownormal );
The last parameter "169" in the string is
Name.
(2) dialing using the rasapi Function
The rasapi function for dialing connections is rasdial. Its prototype is as follows:
Function rasdial (
Prasdialextensions: lprasdialextensions; // useless in Win9x.
Set to nil
Lpszphonebook: pchar; // name of the phone book. It is not valid in Win9x and can be empty.
String
Lprasdialparams: lprasdialparams; // dialing Parameters
Dwnotifiertype: DWORD; // Message notification method
Handle: trasdialfunc; // Message Processing Event
VaR lphrasconn: DWORD // return the connection handle of the successful connection.
): DWORD; stdcall;
Function rasdial; External rasapidll name 'rasdiala ';

The description of prasdialparams is the same as that of rasgetentrydialparams.
The dwnotifiertype parameter indicates the Message notification method. During the dialing process,
The system sends a dialing event message to notify dialing
Progress. Therefore, you must provide a message receiving method and a function to process the message.
When the value is $ ffffff, the handle parameter is
It is treated as a window handle, and event messages are processed by this window; when its value is 0
The handle is treated as the trasdialfunc type.
When the value is 1, handle is considered as a trasdialfunc1 class.
Type function pointer.
The handle parameter indicates the pointer to the message processing function, which can be of the thandle,
Trasdialfunc, trasdialfunc1.
When the parameter is not null or nil, the pointer of the message processing function that it represents will receive dialing
Progress notification message; when it is nil, the dialing process is
Asynchronous mode changes to synchronous mode until the dialing process succeeds or fails.
Data is returned. For details, see the Delphi help file.
To help the rasdial function.
When the dial-up connection is successful, lphrasconn indicates the connection handle.

If the return value of the function is 0, the execution is successful. Otherwise, the returned value is the error code.
The following is an example of using the specified dialing parameter for dialing.

VaR
Rasdialparamsdata: rasdialparams;
Dwresult: DWORD;
Rascon: DWORD;
Begin
// Specify the dialing parameters for the dial-up connection
With rasdialparamsdata do
Begin
Dwsize: = sizeof (rasdialparams); // structure size
Szentryname: = '000000'; // specify the name of the dial-up connection.
Szusername: = 'myname'; // specify the user name
Szpassword: = 'mypassword'; // specify the User Password
Szdomain: = 'mydomain'; // specifies the Domain Name
Szcallbacknumber: = ''; // specify the call back number
Szphonenumber: = ''; // specify the dialing number
End;

// Use the specified dialing parameter and synchronous dialing
Dwresult: = rasdial (nil, '', @ rasdialparamsdata, 0, nil, Rascon
);

If dwresult <> 0 then // failed to set the dialing parameter for the specified dial-up connection
Memo1.lines. Add ('use' + strpas (rasdialparamsdata. szentryname
) + 'Dialing failed :'
+ Getraserror (dwresult ))
Else
Memo1.lines. Add ('use' + strpas (rasdialparamsdata. szentryname
) + 'Dial successful! ');
End;

2. Hang up the specified dial-up connection
The rasapi function for hanging up a dial-up connection is rashangup. Its prototype is:
Function rashangup (
Hrasconn: DWORD // handle of the dial-up connection to be hung up
): DWORD; stdcall;
Function rashangup; External rasapidll name 'rashangupa ';
If the return value of the function is 0, the execution is successful. Otherwise, the returned value is the error code.
The following is an example of hanging up a dial-up connection established by rasdial.

// Hangs up the dial-up connection established by rasdial.
Dwresult: = rashangup (Rascon );
If dwresult <> 0 then // An error occurred while hanging up.
Memo1.lines. Add ('hang up failed: '+ getraserror (dwresult ))
Else
Memo1.lines. Add ('hung up successfully! ');

5. Obtain the current active connection and connection status
1. Get the connection of the current activity
The rasapi function for retrieving the current active connection is
Rasenumconnections:
Function rasenumconnections (VAR lprasconn: rasconn; //
Pointer to 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 rasapidll name
'Rasenumconnectionsa ';
The lprasconn parameter provides a pointer to a rasconn array, pointing to a receiving
Activity connection Buffer
The rasconn type is described as follows:
Rasconn = record
Dwsize: DWORD; // The memory size occupied by the structure (bytes), which is generally set
Sizeof (rasconn)
Hrasconn: hrasconn; // handle of the active connection
Szentryname: array [0 .. ras_maxentryname] of char; // active connection
Name
Szdevicetype: array [0 .. ras_maxdevicetype] of char; // active connection
Device Type Used for connection
Szdevicename: array [0 .. ras_maxdevicename] of char; // active connection
Name of the device used for connection
End;
The parameter lpcb is the buffer size (bytes ).
The actual number of connections will be returned by the parameter lpcconnections.

If the return value of the function is 0, the execution is successful. Otherwise, the returned value is the error code.

2. Obtain the connection status of the specified connection
The rasapi function for retrieving the connection status of a specified connection is
Rasgetconnectstatus. Its function prototype is:
Function rasgetconnectstatus (
Hrasconn: hrasconn; // specify the handle of the active connection.
Lprasconnstatus: lprasconnstatus // connection status parameter
): DWORD; stdcall;
Function rasgetconnectstatus; External rasapidll name
'Rasgetconnectstatusa ';
The connection status parameter lprasconnstatus is of the rasconnstatus type.
Will return the connection status parameter.
The types of rasconnstatus and lprasconnstatus are described as follows:
Lprasconnstatus = ^ rasconnstatus;
Rasconnstatus = record
Dwsize: DWORD; // The memory size occupied by the structure (bytes), which is generally set
Sizeof (rasconnstatus)
Rasconnstate: rasconnstate; // connection status ID, number of DWORD types in a group
Value set.
Dwerror: DWORD; // error type identifier
Szdevicetype: array [0 .. ras_maxdevicetype] of char; // active connection
Device Type Used for connection
Szdevicename: array [0 .. ras_maxdevicename] of char; // active connection
Name of the device used for connection
End;
If the return value of the function is 0, the execution is successful. Otherwise, the returned value is the error code.

The following is an application example listing the names of active connections in the current system.
Name and connection status.
Note that dwsize should be set in the first rasconn structure of the rasconn buffer.

Const
Maxconnections = 10; // The maximum number of dial-up connections
VaR
Connections: array [0 .. MaxConnections-1] of rasconn;
Longsize: DWORD;
Intavailabelconnections: DWORD;
Intindex: integer;
Dwresult: DWORD;
Strtemp: string;
Rasconnstatusdata: rasconnstatus;
Begin
Connections [0]. dwsize: = sizeof (rasconn); // structure size
Longsize: = maxconnections * connections [0]. dwsize; // Buffer
Area size
Intavailabelconnections: = 0; // actual number of active connections
// Obtain the active connections in the current system
Dwresult: = rasenumconnections (connections [0], longsize,
Intavailabelconnections );
If dwresult <> 0 then // obtain the active connection in the current system
Memo1.lines. Add ('get the active connections in the current system: '+
Getraserror (dwresult ))
Else
Begin
Memo1.lines. Add ('Active connections in the current system '+ inttostr (
Intavailabelconnections)
+ ', As shown below ');
For intindex: = 0 to intavailabelconnections-1 do
Begin
Strtemp: = 'Connection name: '+ strpas (connections [intindex
]. Szentryname)
+ 'Device type: '+ strpas (connections [intindex
]. Szdevicetype)
+ 'Device name: '+ strpas (connections [intindex
]. Szdevicename );
// Obtain the connection status
Dwresult: = rasgetconnectstatus (connections [intindex
]. Hrasconn, @ rasconnstatusdata );
If 0 <> dwresult then
Strtemp: = strtemp + 'unknown connection status: '+ getraserror (
Dwresult)
Else if rasconnstatusdata. rasconnstate = rascs_connected
Then
Strtemp: = strtemp + 'Connection status: connected'
Else
Strtemp: = strtemp + 'Connection status :( '+
Inttostr (rasconnstatusdata. rasconnstate) + ')';
Memo1.lines. Add (strtemp );
End;
End;
End;

The above program is successfully debugged under pwin98 + Delphi3.0.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.