VC dial-up network program

zhaozj2021-02-08  223

Everyone knows that in Netants, Download Expert and other software, there is a function of timing dial-up network download software. and

General users' dial-up Internet access, using Windows Remote Access Service (RAS, Remote Access Service).

Here is the implementation of its implementation under Visual C .

Visual C provides us with a "ras.h" header file containing the RAS API declaration. To implement dialing in the program

Internet function, its rough process is as follows:

1. Use the MODEM dial-up to connect to use the Rasdial function.

Its 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: It is valid only under Windows NT, under Windows 95, this

The two parameters are ignored.

LPRASDIALPARAMS: This parameter is important, it points to an RasdialParams structure, which contains the following

Several members:

DWSIZE: It should be set to sizeof (rasdialparams);

SzenTryName and SzphonEnumber: These two parameters are connected, and SzenTryName can specify the connection to be established.

For example, "My Connection", etc., this is the connection to the user has been established in the "Dial Network". At this time, MO

Dem will call your ISP number in "My Connection". At this time, the szphonenumber member is set to empty string ""

Can; if you want to specify the ISP number to be dial in the program, SzenTryName can be set to empty string "

", At this time, SzphonEnumber should be set for your ISP number (169, 663, etc.), especially, for the 201 call card

The situation of the Internet, can be set to "201,, account, password #,, ISP number #" (where "," means a pause

Embrase (take the account, password, etc.), you can adjust according to the line status of your location.

SzcallbackNumber, SZDomain: Set to an empty string ""

SzuserName, Szpassword: Log in to the username and password. Such as 169 utility accounts GUEST, GUEST.

Other members do not have to be set.

DWNotifiertype: Specifies whether to handle confirmation messages by the window or by the callback function. Through confirmation, we can

To get the current state of the Rasdial process. Such as "Open Duplete", "is verifying username and password", etc.. also may

Set to NULL.

DWNotifier: Specifies a window or callback function that handles a confirmation message. It can also be set to NULL.

LPHRASCONN: Points a variable of a type HRASCONN. Before calling Rasdial, you must be specified as NULL, RAS

If Dial is successfully returned, the Handle of the RAS connection is stored in the variable it points to. We can also be interrupted by this handle.

Open.

The connection can be established as long as the RasDial function is called in the appropriate location in the program. 2. Licensing the message to get the current state of the dialing process.

We handle confirmation messages with a specified window to explain how to get the current state of the dialing process.

Add: in the implementation code of the dialog class (or view class, etc.) for the confirmation message:

Const uint wm_rasevent = :: registerWindowMessagea (rasdialevent);

Manually add message mapping in Message Map: (**** is your defined dialog class name)

Begin_MESSAGE_MAP (****, cdialog)

// AFX_MSG_MAP (****)

......

ON_REGISTERED_MESSAGE (WM_RASEVENT, ONRASDIALEVENT) (<- Join this sentence)

// AFX_MSG_MAP

END_MESSAGE_MAP ()

Add member function to process the message:

LResult CDialInfo :: OnrasdiaLevent (WPARAM WP, LPARAM LP)

{

RasconnState Rasstate = (rasconnstate) WP;

Clistbox * info = (clistbox *) getdlgitem (IDC_INFOLIST);

// Display status with the ListBox control (IDC-INFOLIST))

Switch (rasstate)

{

Case rascs_openport:

INFO → addstring (_t ("Open Port ..."))));

Break;

Case rascs_portople:

INFO → AddString (_t ("port is opened.")));

Break;

Case Rascs_ConnectDevice:

INFO → AddString (_T ("Connection Device ..."));

Break;

Case rascs_deviceconnected:

INFO → addstring (_t ("device is 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 ("Connection has been disconnected");

m_hrasconn = null;

// The type of member variable m_hrasconn that can be defined for HRASCONN is defined to save the Handle of the RAS connection.

// Use the pointer to M_hrasconn as the LPHRASCONN parameter when calling Rasdial.

// Since the connection handle is saved in M_Hrasconn, the connection is disconnected to be reset to NULL.

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 notice that the Sleep function in the above code is required here. It takes a certain time to disconnect the connection. Such as

If you don't wait for a while, the computer may not be able to close the port normally. The next time you can't dial, only restarting Windows can be resolved. To prevent this issue, you can also call the RasgetConnectStatus function, the method is as follows:

RasconnStatus Rstatus;

While (RasgetConnectStatus (m_hrasconn, & rstatus)! = error_INvalid_handle)

{

:: Sleep (0);

}

4. In the following cases:

1 When browsing the web, stop responding, restart the connection status icon on the taskbar after Explorer, may be eliminated

Loss.

2 I hope that after the connection is successful, the program is exited, and then the program can be executed again to select disconnect.

You can call the RaseNumConnection function to get the currently connected handle.

For example, as follows:

Hrasconn Hrasconnect; DWORD DWBUFFFFERSIZE, DWNUMOFCONNECTIONS; / / Buffer size,

Digit number

Lprasconn LPrasconn;

LPrasconn = new rasconn [3]; // You can get 3 connection handles, and the client program does not necessarily

3, because the number of connections is limited, only one connection is mostly connected.

LPRASCONN [0] .dwsize = sizeof (rasconn);

DWBuffFersize = 32 * sizeof (rasconn); // Since the size of the buffer consisting of three Rasconn structures

RasenumConnections (LPRAS CONN, & DW Buffer Size, & DW Numof Connections); //

This function returns zero if successful.

For (DWORD I = 0; i {

Hrasconnect = lprasconn [i]. HRASCONN member of the // rasconn structure is the RAS connection sentence

handle

Rashangup (Hrasconnect);

:: Sleep (2000);

}

Lprasconn;

Under Windows 98, Visual C 6.0 is debugged.

In this way, a program that implements a basic dial-up function is complete. If you have to learn more about conditions or services

Device design, you can refer to MSDN → Platform SDK → Networking and distributed services → r

Emote Access Service

转载请注明原文地址:https://www.9cbs.com/read-3095.html

New Post(0)