⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 autodial.cpp

📁 网络自动拨号程序
💻 CPP
字号:
// AutoDial.cpp : implementation file
//

#include "stdafx.h"
#include "AutoDialer.h"
#include "AutoDial.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAutoDial dialog

char m_szStatus[500];
char m_szLastError[500];

CAutoDial::CAutoDial(CWnd* pParent /*=NULL*/)
	: CDialog(CAutoDial::IDD, pParent)
{
	//{{AFX_DATA_INIT(CAutoDial)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT

	m_szStatus[0] = 0;
	m_szLastError[0] = 0;
	m_bErrorFlag = FALSE;
	m_hRasConn = NULL;
	m_nErrorSeconds = 0;

}


void CAutoDial::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAutoDial)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CAutoDial, CDialog)
	//{{AFX_MSG_MAP(CAutoDial)
	ON_WM_CREATE()
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAutoDial message handlers

int CAutoDial::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CDialog::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	// Kick off the timer.
	SetTimer( 1, 1000, NULL );
	
	return 0;
}

void CAutoDial::OnTimer(UINT nIDEvent) 
{

	// See if we have an error.
	if( m_szLastError[0] != 0 ){

		// Set the dialog text and increment
		// the counter.
		SetDlgItemText( IDC_STATUS, m_szLastError );
		m_nErrorSeconds++;

		// If we've shown the error for
		// five seconds, quit the dialog.
		if( m_nErrorSeconds > 5 )
			CDialog::OnOK();
		}
	else{

		// Make sure we zero the error counter.
		m_nErrorSeconds = 0;

		// Set the dialog to show the current status.
		SetDlgItemText( IDC_STATUS, m_szStatus );

		// If the string says "Connected to remote
		// network" we're connected.
		if( !stricmp( m_szStatus,
			"Connected to remote network" ) )
			CDialog::OnOK();
		}
	
	CDialog::OnTimer(nIDEvent);
}

VOID WINAPI RasDialFunc( UINT unMsg,
	RASCONNSTATE rasconnstate, DWORD dwError )
{

	// The following states are reported by
	// the RAS API and we copy a text string
	// so that the timer can update the dialog.
	switch( rasconnstate ){
		case RASCS_OpenPort:
			strcpy( m_szStatus,
				"Opening the communication port" );
			break;
		case RASCS_PortOpened:
			strcpy( m_szStatus,
				"The communication port has been opened" );
			break;
		case RASCS_ConnectDevice:
			strcpy( m_szStatus,
				"A connection is being attempted" );
			break;
		case RASCS_DeviceConnected:
		case RASCS_AllDevicesConnected:
			strcpy( m_szStatus,
				"A connection has been made" );
			break;
		case RASCS_Authenticate:
			strcpy( m_szStatus,
				"Authenticating user name and password" );
			break;
		case RASCS_AuthNotify:
			strcpy( m_szStatus,
				"Notifying authorization" );
			break;
		case RASCS_AuthRetry:
			strcpy( m_szStatus,
				"Authorization retry" );
			break;
		case RASCS_AuthCallback:
			strcpy( m_szStatus,
				"Authorization callback" );
			break;
		case RASCS_AuthChangePassword:
			strcpy( m_szStatus,
				"Authorization change password" );
			break;
		case RASCS_AuthProject:
			strcpy( m_szStatus,
				"Authorization project" );
			break;
		case RASCS_AuthLinkSpeed:
			strcpy( m_szStatus,
				"Authorization link speed" );
			break;
		case RASCS_AuthAck:
			strcpy( m_szStatus,
				"Authorization acknowledgment" );
			break;
		case RASCS_ReAuthenticate:
			strcpy( m_szStatus,
				"Re-authentication begun" );
			break;
		case RASCS_Authenticated:
			strcpy( m_szStatus,
				"User has been authenticated" );
			break;
		case RASCS_PrepareForCallback:
			strcpy( m_szStatus,
				"Preparing for callback" );
			break;
		case RASCS_WaitForModemReset:
			strcpy( m_szStatus,
				"Waiting for modem reset" );
			break;
		case RASCS_WaitForCallback:
			strcpy( m_szStatus,
				"Waiting for callback" );
			break;
		case RASCS_Projected:
			strcpy( m_szStatus,
				"Projected" );
			break;
		case RASCS_StartAuthentication:
			strcpy( m_szStatus,
				"Starting authentication" );
			break;
		case RASCS_CallbackComplete:
			strcpy( m_szStatus,
				"Callback complete" );
			break;
		case RASCS_LogonNetwork:
			strcpy( m_szStatus,
				"Logging on to network" );
			break;
		case RASCS_SubEntryConnected:
			strcpy( m_szStatus,
				"Sub entry connected" );
			break;
		case RASCS_SubEntryDisconnected:
			strcpy( m_szStatus,
				"Sub entry disconnected" );
			break;
		case RASCS_Interactive:
			strcpy( m_szStatus,
				"Interactive" );
			break;
		case RASCS_RetryAuthentication:
			strcpy( m_szStatus,
				"Authentication retry" );
			break;
		case RASCS_CallbackSetByCaller:
			strcpy( m_szStatus,
				"Callback set by caller" );
			break;
		case RASCS_PasswordExpired:
			strcpy( m_szStatus,
				"Password expired" );
			break;
		case RASCS_Connected:
			strcpy( m_szStatus,
				"Connected to remote network" );
			break;
		case RASCS_Disconnected:
			strcpy( m_szStatus,
				"Disconnected from remote network" );
			break;
		}

	// If we have a non-zero error code, get the
	// error code and place it into the m_szLastError
	// character buffer.
	if( dwError != 0 )
		RasGetErrorString( (UINT) dwError,
			(LPSTR) m_szLastError,
			sizeof( m_szLastError ) );

}

BOOL CAutoDial::Connect( const char *lpszUser,
	const char *lpszPassword, const char *lpszDialupName )
{
	RASDIALPARAMS rdParams;
	DWORD dwRet;

	// First, try to disconnect.
	Disconnect();

	// Clear the RASDIALPARAMS structure, the assign
	// it's members with the appropriate values.
	memset( &rdParams, 0, sizeof( RASDIALPARAMS ) );
	rdParams.dwSize = sizeof( RASDIALPARAMS );
	lstrcpy( rdParams.szEntryName, lpszDialupName );
	rdParams.szPhoneNumber[0] = '\0';
	rdParams.szCallbackNumber[0] = '*';
	rdParams.szCallbackNumber[0] = '\0';
	strcpy( rdParams.szUserName, lpszUser );
	strcpy( rdParams.szPassword, lpszPassword );
	strcpy( m_szDialupName, lpszDialupName );
	
	// Call the RasDial() function to get the
	// dialing sequence started.
	dwRet = RasDial( NULL, NULL, &rdParams, 0L,
		(RASDIALFUNC) RasDialFunc, &m_hRasConn );

	// If dwRet is non-zero, we had an error. Retrieve
	// the error into the m_szLastError char buffer.
	if( dwRet ){
		if( RasGetErrorString( (UINT) dwRet,
			(LPSTR) m_szLastError,
			sizeof( m_szLastError ) ) != 0 )
				wsprintf( (LPSTR) m_szLastError,
					"Undefined RAS Dial Error (%ld).",
					dwRet );
		return( FALSE );
		}

	return( TRUE );

}

void CAutoDial::Disconnect( void )
{

	// If the RAS handle is NULL,
	// return without doing anything.
	if( m_hRasConn == NULL )
		return;

	// Hang up and set the handle to NULL.
	RasHangUp( m_hRasConn );
	m_hRasConn = NULL;

}

void CAutoDial::GetLastError( char *lpszErrorBuffer,
	int nLimit )
{

	// Copy the error into a buffer that's passed
	// in and limit the bytes we send back to nLimit.
	for( int i=0; i<nLimit-1; i++ ){
		lpszErrorBuffer[i] = m_szLastError[i];
		if( lpszErrorBuffer[i] == 0 )
			break;
		}

	// NULL terminate the string.
	lpszErrorBuffer[nLimit-1] = 0;

}

void CAutoDial::GetLastError( CString& strError )
{

	// Set the strError CString object
	// to the last error that's in the
	// m_szLastError character buffer.
	strError = m_szLastError;

}

void CAutoDial::GetStatus( char *lpszStatusBuffer, int nLimit )
{

	// Copy the status into a buffer that's passed
	// in and limit the bytes we send back to nLimit.
	for( int i=0; i<nLimit-1; i++ ){
		lpszStatusBuffer[i] = m_szStatus[i];
		if( lpszStatusBuffer[i] == 0 )
			break;
		}

	// NULL terminate the string.
	lpszStatusBuffer[nLimit-1] = 0;

}

void CAutoDial::GetStatus( CString& strStatus )
{

	// Set the strStatus CString object
	// to the last error that's in the
	// m_szStatus character buffer.
	strStatus = m_szStatus;

}

BOOL CAutoDial::HangupByDialupName( const char *lpszDialupName )
{

	// Get a connection handle based on
	// the name that's passed in.
	HRASCONN hRasConn =
		GetConnectionHandle( lpszDialupName );

	// If the handle is NULL, we didn't
	// find the connection and return.
	if( hRasConn == NULL )
		return( FALSE );

	// Hang up.
	RasHangUp( hRasConn );

	return( TRUE );

}

HRASCONN CAutoDial::GetConnectionHandle( const char *lpszDialupName )
{

	// Declare an array of RASCONN structures
	// and clear them out.
	RASCONN RasConn[20];
	memset( RasConn, 0, sizeof( RASCONN ) * 20 );

	// The size is the size of the structure
	// times 20. Zero the connections.
	DWORD dwSize = sizeof( RASCONN ) * 20,
		dwConnections = 0;

	// Set the structure size to sizeof( RASCONN )
	// and get a list of the connections.
	RasConn[0].dwSize = sizeof( RASCONN );
	RasEnumConnections( RasConn, &dwSize, &dwConnections );

	// Loop through each connection and compare
	// the connection name to the name that was
	// pass in. If we find it, return the handle.
	for( DWORD i=0; i<dwConnections; i++ ){
		if( !strcmp( lpszDialupName,
			RasConn[i].szEntryName ) )
			return( RasConn[i].hrasconn );
		}

	// Return NULL since we didn't find the
	// connection.
	return( NULL );

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -