VC + + to achieve dial-up program Xiangjie


We know that in Netants, DownLoad Expert and other software are with regular dial-up Internet access to download software. General dial-up users, using the Windows, Remote Access Service (RAS, Remote Access Service). The following brief in Visual C + + under implementation.

Visual C + + provides us with a statement containing the RAS API's "ras.h" header file. To achieve dial-in program functions, its general procedure is as follows:

1. Using dial-up Modem to connect, you should use RasDial function.

The statement is as follows:

DWORD Ras Dial (LPRASDIALEXTENSIONS lpRas DialExtensions, LPCTSTR lpszPhonebook, LPRASDIALPARAMS lp Ras DialParams, DWORD dw Notifier Type, LPVOID lpv Notifier,
LPHRASCONN lph Ras Conn)

Parameters:

lpRasDialExtensions and lpszPhonebook: only valid under Windows NT, the Windows 95, the two parameters are ignored.

lpRasDialParams: This parameter is important, it points to a RASDIALPARAMS structure that contains the following members:

dwSize: should be set to sizeof (RASDIALPARAMS);

szEntryName and szPhoneNumber: These two parameters are linked, szEntryName can specify the connection to be established, for example, "my connection" and so on, which is handling the user has been in the "Dial-Up Networking" in the establishment of the connection. Then, Modem will call you "my connection" to set the ISP number, this time members set to empty string szPhoneNumber "" then; If you want to specify in the program itself, then dial the ISP number, szEntryName can be set to empty string "", at this time should be set szPhoneNumber for your ISP number (169,663, etc.), particularly for the use of 201 phone cards to access the situation, can be set to "201,,, account, password #,, ISP number # "(where", "said pause period of time (to wait for the confirmation ID, password, etc.), you can state your location in the line to adjust itself.

SzCallBackNumber, szDomain: Make the empty string "" can be.

SzUserName, szPassword: login user name and password. Such as the 169 Public Account guest, guest.

Other members do not set.

DwNotifierType: specified by the window or the callback function to handle the confirmation message. We can get through the confirmation message RasDial the current state of the process. Such as the "opening paragraph I," "Verifying user name and password" and so on. Can also be set to NULL.

dwNotifier: designated to handle the confirmation message window or callback function. Can also be set to NULL.

LphRasConn: point to a variable of type HRASCONN. RasDial before the call is designated as NULL, RasDial return if successful, will RAS connection handle stored in the variable it points to. We can also connect through this handle to disconnect.

As long as the appropriate place in the program can call RasDial function connect.

2. Reasonable confirmation message in order to get the current status of dial-up process.

We deal with the confirmation message window to specify an example on how to get the current status of dial-up process.

In dealing with the confirmation message of the dialog class (or view class, etc.) to add the implementation code:

const UINT WM_RASEVENT =:: RegisterWindowMessageA (RASDIALEVENT);
In the Message Map in hand by adding a message map :(**** your dialog class definition name)
BEGIN_MESSAGE_MAP (****, CDialog)
file / / AFX_MSG_MAP (****)
... ...
ON_REGISTERED_MESSAGE (WM_RASEVENT, OnRasDialEvent) (<- add these words)
file / / AFX_MSG_MAP
END_MESSAGE_MAP ()
Adding member functions processing messages:
LRESULT CDialInfo:: OnRasDialEvent (WPARAM wp, LPARAM lp)
(
RASCONNSTATE rasstate = (RASCONNSTATE) wp;
CListBox * info = (CListBox *) GetDlgItem (IDC_INFOLIST);
file / / use the ListBox control (ID for the IDC-INFOLIST) to display the state)
switch (rasstate)
(
case RASCS_OpenPort:
info → AddString (_T ("open port ... ..."));




[Next]


break;
case RASCS_PortOpened:
info → AddString (_T ("Port already open ."));
break;
case RASCS_ConnectDevice:
info → AddString (_T ("connect 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 ("through"));
break;
case RASCS_Connected:
info-> AddString (_T ("Connected"));
reak;
case RASCS_Disconnected:
info-> AddString (_T ("Connection has been disconnected"));
m_hRasConn = NULL;
file / / define the member variable of type HRASCONN m_hRasConn to save the RAS connection handle.
file / / when calling RasDial m_hRasConn with a pointer pointing to a lphRasConn parameters.
file / / since we use the m_hRasConn to save the connection handle, the connection should be reset after disconnecting 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 have noticed the above code in the Sleep function, here is a must. Take time to disconnect. If you do not wait for a period of time, the computer may not shut down the port. Lead to the next can not dial-up, only to re-start Windows to resolve. To prevent this problem can also call RasGetConnectStatus function as follows:

RASCONNSTATUS rStatus;
while (RasGetConnectStatus (m_hRasConn, & rStatus)! = ERROR_INVALID_HANDLE)
(
:: Sleep (0);
)

4. In the following cases:

① browsing sometimes appear to stop responding after restart explorer the connection status taskbar icon may disappear.
② hope that the connection is successful, exit the program, perform this procedure again, choose Disconnect.
RasEnumConnection function can call to get the current connection handle.

For example:

HRASCONN hRasConnect; DWORD dwBuffferSize, dwNumofConnections; / / buffer size, number of connections
LPRASCONN lpRasConn;
lpRasConn = new RASCONN [3】; / / connection handle up to 3 available, the client program does not have to set 3, due to the limited number of connections, the majority of only one connection.
lpRasConn [0】. dwSize = sizeof (RASCONN);
dwBuffferSize = 32 * sizeof (RASCONN); / / calculate the structure constituted by the three RASCONN buffer size
RasEnumConnections (lpRas Conn, & dw Buffer Size, & dw Numof Connections); / / This function returns zero if successful.
for (DWORD i = 0; i (
hRasConnect = lpRasConn [i】. hrasconn; / / RASCONN structure hrasconn members of the RAS connection handle
RasHangUp (hRasConnect);
:: Sleep (2000);
)
delete [】 lpRasConn;
In Windows 98, Visual C + + 6.0 to debug through.

Thus, a basic dial-up Internet access to achieve the procedure is complete. If you want to learn more about the situation or the server-side programming, you can refer to MSDN → Platform SDK → Networking and Distributed Services → Remote Access Service of the content. (Csdn)