VC dial-up surfing program

Source: Internet
Author: User

As you know, NetAnts, download expert, and other software all have the function of downloading the software through timed dialing. While
Generally, the Remote Access Service (RAS) of Windows is used for dial-up Internet access ).
The following describes the implementation in Visual C ++.
Visual c ++ provides us with the "RAS. H" header file containing the ras api declaration. Implement dialing in the program
The process of accessing the Internet is as follows:
1. The rasdial function should be used for connections using modem dialing.
The statement is as follows:
DWORD Ras dial (lprasdialextensions lpras dialextensions, lpctstr lpszphoneboo
K, lprasdialparams LP Ras dialparams, dword dw notifier type, lpvoid LPV notifier,
Lphrasconn lph Ras conn)
Parameter description:
Lprasdialextensions and lpszphonebook: valid only in Windows NT. in Windows 95
The two parameters are ignored.
Lprasdialparams: this parameter is very important and points to a rasdialparams structure, which contains the following
Several members:
Dwsize: it should be set to sizeof (rasdialparams );
Szentryname and szphonenumber: these two parameters are related. szentryname can specify the connection to be established.
For example, "my connections" and so on are used to process the connections that the user has established in the "Dial-Up Network. At this time, Mo
DEM will call the ISP number you set in "my connections". At this time, the szphonenumber member is set to an empty string "", that is
Yes. If you want to specify the ISP number in the program, szentryname can be set to an empty string"
", Set szphonenumber to your ISP number (169,663, etc.). In particular, if you use a 201 phone card
When surfing the Internet, you can set it to "201, account, password #, and ISP number #" (where "," indicates a pause
Room (to wait for confirmation of account, password, etc.), you can adjust the line according to your location.
Szcallbacknumber, szdomain: Set it to an empty string.
Szusername and szpassword: logon username and password. For example, 169 Public Account guest and guest.
You do not need to set other members.
Dwnotifiertype: Specifies whether the confirmation message is processed by the window or the callback function. By confirming the message, we can
Obtain the current status of the rasdial process. For example, "opening the segment" and "verifying the user name and password. Or
Set to null.
Dwnotifier: Specifies the window or callback function for processing the confirmation message. It can also be set to null.
Lphrasconn: point to a variable of the hrasconn type. Before calling rasdial, you must specify NULL, Ras
If dial is returned successfully, the RAS connection handle is stored in the variable to which it points. We can also use this handle to disconnect
Open a connection.
You only need to call the rasdial function in a proper place in the program to establish a connection.
2. Confirm the message to get the current status of the dialing process.
The following example uses a specified window to process the confirmation message to describe how to obtain the current status of the dialing process.
Add the following to the implementation code of the dialog box class (or view class) for processing the confirmation message:
Const uint wm_rasevent =: registerwindowmessagea (rasdialevent );
Manually add message ing to message map: (*** is the name of the dialog box class you defined)
Begin_message_map (***, cdialog)
// Afx_msg_map (****)
......
On_registered_message (wm_rasevent, onrasdialevent) (<-add this sentence)
// Afx_msg_map
End_message_map ()
Add a member function to process messages:
Lresult cdialinfo: onrasdialevent (wparam WP, lparam LP)
{
Rasconnstate rasstate = (rasconnstate) WP;
Clistbox * info = (clistbox *) getdlgitem (idc_infolist );
// Use The ListBox control (ID is the IDC-INFOLIST) to display the status)
Switch (rasstate)
{
Case rascs_openport:
Info → addstring (_ T ("open the port ...... ″));
Break;
Case rascs_portopened:
Info → addstring (_ T ("port opened .″));
Break;
Case rascs_connectdevice:
Info → addstring (_ T ("Connect device ...... ″));
Break;
Case rascs_deviceconnected:
Info → addstring (_ T ("device connected .″));
Break;
Case rascs_authenticate:
Info → addstring (_ T ("verify user and password ″));
Break;
Case rascs_authenticated:
Info → addstring (_ T ("pass ″));
Break;
Case rascs_connected:
Info-> addstring (_ T ("connected ″));
Reak;
Case rascs_disconnected:
Info-> addstring (_ T ("disconnected ″));
M_hrasconn = NULL;
// You can define the member variable m_hrasconn of the hrasconn type to save the handle of the RAS connection.
// When calling rasdial, use the pointer pointing to m_hrasconn as the lphrasconn parameter.
// Since m_hrasconn is used to save the connection handle, it should be reset to null after the connection is closed.
Break;
Default:
Return (lresult) 0;
}
Return (lresult) 0;
}
3. Disconnect:
If (m_hrasconn! = NULL)
{
Rashangup (m_hrasconn );
M_hrasconn = NULL;
M_ondial = true;
: Sleep (2000 );
}
Note:
You may have noticed the sleep function in the above Code, which is required here. It takes some time to disconnect. For example
If you do not wait for a while, the computer may fail to close the port. As a result, the next time cannot be called, only restart
Windows. To prevent this problem, call the rasgetconnectstatus function as follows:
Rasconnstatus rstatus;
While (rasgetconnectstatus (m_hrasconn, & rstatus )! = Error_invalid_handle)

{
: Sleep (0 );
}
4. In the following cases:
① When browsing the Web page, the response is sometimes stopped. After the explorer is restarted, the connection status icon on the taskbar may disappear.
Loss.
② If You Want To exit the program and execute the program again after the connection is successful, you can choose to disconnect the program.
You can call the rasenumconnection function to obtain the handle of the current connection.
Example:
Hrasconn hrasconnect; DWORD dwbufffersize, dwnumofconnections; // buffer size, connection
Number of connections
Lprasconn;
Lprasconn = new rasconn [3]; // a maximum of three connection handles can be obtained. The client program does not have to be set
3. Because the number of connections is limited, most connections are only one.
Lprasconn [0]. dwsize = sizeof (rasconn );
Dwbufffersize = 32 * sizeof (rasconn); // obtain the buffer size composed of three rasconn structures.
Rasenumconnections (lpras Conn, & DW buffer size, & DW numof connections );//
If this function is successful, zero is returned.
For (DWORD I = 0; I {
Hrasconnect = lprasconn [I]. hrasconn; // hrasconn IN THE rasconn structure contains the RAS join sentence.
Handle
Rashangup (hrasconnect );
: Sleep (2000 );
}
Delete [] lprasconn;
Windows 98 and Visual C ++ 6.0 are successfully debugged.
In this way, a program that implements the basic dial-up Internet function is complete. If you want to know more about the situation or service
For program design, refer to msdn → Platform SDK → networking and distributed Services → R
Emote Access Service

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.