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

📄 testmsgdlg.cpp

📁 自己编的一个基于WinCE的拨号程序
💻 CPP
字号:
// TestMsgDlg.cpp : implementation file
//

#include "stdafx.h"
#include "TestMsg.h"
#include "TestMsgDlg.h"
#include "ComExt.h"
#include "Ras.h"

__EXT_Init  mExt_Init;
__EXT_DeInit mExt_DeInit;

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

RASDIALPARAMS RasDialParams;
HRASCONN  hRasConn;

WCHAR szRasEntryName[40] = _T("ADSL");
WCHAR szUserName[40] = _T("cd87827259");
WCHAR szPassword[40] = _T("");
WCHAR szDomain[40] = _T("");


BOOL MakeAutoRasDial (HWND hDlgWnd);
BOOL MakeUserRasDial (HWND hDlgWnd);

/////////////////////////////////////////////////////////////////////////////
// CTestMsgDlg dialog

CTestMsgDlg::CTestMsgDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CTestMsgDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CTestMsgDlg)
	m_domain = _T("");
	m_password = _T("302302");
	m_username = _T("cd87827259");
	m_entryname = _T("ADSL");
	m_rasconn = _T("");
	m_error = 0;
	m_devname = _T("");
	m_size = 0;
	m_constate = _T("");
	m_devtype = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CTestMsgDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CTestMsgDlg)
	DDX_Control(pDX, IDC_BUTTON_CLOSE, m_autodial);
	DDX_Control(pDX, IDC_BUTTON_OPEN, m_open);
	DDX_Control(pDX, IDC_BUTTON_HANGUP, m_hangup);
	DDX_Text(pDX, IDC_EDIT_DOMAIN, m_domain);
	DDX_Text(pDX, IDC_EDIT_PASSWORD, m_password);
	DDX_Text(pDX, IDC_EDIT_USERNAME, m_username);
	DDX_Text(pDX, IDC_EDIT_ENTRYNAME, m_entryname);
	DDX_Text(pDX, IDC_EDIT_RASCONN, m_rasconn);
	DDX_Text(pDX, IDC_EDIT_ST_ERROR, m_error);
	DDX_Text(pDX, IDC_EDIT_ST_NAME, m_devname);
	DDX_Text(pDX, IDC_EDIT_ST_SIZE, m_size);
	DDX_Text(pDX, IDC_EDIT_ST_STATE, m_constate);
	DDX_Text(pDX, IDC_EDIT_ST_TYPE, m_devtype);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CTestMsgDlg, CDialog)
	//{{AFX_MSG_MAP(CTestMsgDlg)
	ON_BN_CLICKED(IDC_BUTTON_OPEN, OnAutoDial)
	ON_BN_CLICKED(IDC_BUTTON_CLOSE, OnUserDial)
	ON_BN_CLICKED(IDC_BUTTON_HANGUP, OnButtonHangup)
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTestMsgDlg message handlers

BOOL CTestMsgDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	CenterWindow(GetDesktopWindow());	// center to the hpc screen

	// TODO: Add extra initialization here
	this->SetTimer(REFRESH_TIMER,1000,NULL);
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}



void CTestMsgDlg::OnAutoDial() 
{
	// TODO: Add your control notification handler code here
	if (0!=hRasConn)  return;

	if ( TRUE==MakeAutoRasDial(this->m_hWnd) ) {
		this->m_autodial.EnableWindow(FALSE);
		this->m_open.EnableWindow(FALSE);
		this->m_hangup.EnableWindow(TRUE);
	}

}

void CTestMsgDlg::OnUserDial() 
{
	// TODO: Add your control notification handler code here
//	USES_CONVERSION;
	this->UpdateData(TRUE);
	if (0!=hRasConn)  return;
	
	wcscpy( szRasEntryName, this->m_entryname );
	wcscpy( szUserName, this->m_username );
	wcscpy( szPassword, this->m_password );
	wcscpy( szDomain, this->m_domain );
	
	if ( TRUE==MakeUserRasDial(this->m_hWnd) ) {
		this->m_autodial.EnableWindow(FALSE);
		this->m_open.EnableWindow(FALSE);
		this->m_hangup.EnableWindow(TRUE);
	}

}

void CTestMsgDlg::DeInit(void) 
{
	// TODO: Add your control notification handler code here
	OnButtonHangup();
}

void CTestMsgDlg::OnButtonHangup() 
{
	// TODO: Add your control notification handler code here
	if (hRasConn==0)  return;
	
	DWORD ret = RasHangUp(hRasConn);
	hRasConn = 0;
	
	TCHAR szBuffer[100];
    if (0 != ret ) {
		wsprintf (szBuffer, TEXT("Hang Up Error...\r\nError Code = %X"), ret);
		MessageBox ( szBuffer, 
                  _T("Test Hangup..."), //szTitle, 
                  MB_OK);
	}

	this->m_autodial.EnableWindow(TRUE);
	this->m_open.EnableWindow(TRUE);
	this->m_hangup.EnableWindow(FALSE);

	UpdateData(TRUE);
	m_constate = _T("用户已挂断...");
	UpdateData(FALSE);

}

CString status_char_running[] = {
	_T("RASCS_OpenPort"),  // = 0 
    _T("端口已打开..."),//_T("RASCS_PortOpened"),
    _T("连接设备中..."),//_T("RASCS_ConnectDevice"),
    _T("连接设备成功..."),//_T("RASCS_DeviceConnected"),
    _T("RASCS_AllDevicesConnected"),
    _T("验证中..."),//_T("RASCS_Authenticate"),
    _T("RASCS_AuthNotify"),
    _T("RASCS_AuthRetry"),
    _T("RASCS_AuthCallback"),
    _T("RASCS_AuthChangePassword"),
    _T("RASCS_AuthProject"),
    _T("RASCS_AuthLinkSpeed"),
    _T("RASCS_AuthAck"),
    _T("再次验证中..."),//_T("RASCS_ReAuthenticate"),
    _T("通过验证..."),//_T("RASCS_Authenticated"),
    _T("RASCS_PrepareForCallback"),
    _T("RASCS_WaitForModemReset"),
    _T("RASCS_WaitForCallback"),
    _T("RASCS_Projected")
};

CString status_char_pause[] = {
    _T("RASCS_Interactive"), 			// = RASCS_PAUSED
    _T("再次验证.."),//_T("RASCS_RetryAuthentication"),
    _T("RASCS_CallbackSetByCaller"),
    _T("需要密码...")//_T("RASCS_PasswordExpired")
};

CString status_char_done[] = {
    _T("网络连接已完成..."),//_T("RASCS_Connected"), // = RASCS_DONE
    _T("网络连接已断开...") //_T("RASCS_Disconnected ")
};


void CTestMsgDlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	CHAR tmp_chr[20];

	switch(nIDEvent) {
	case REFRESH_TIMER:
		UpdateData(TRUE);
		tmp_chr[0] = 0x00;
		tmp_chr[1] = 0x00;
		sprintf(tmp_chr, "%X", (DWORD) hRasConn);
		this->m_rasconn = tmp_chr;
		RefreshConnectStatus();

		UpdateData(FALSE);
		break;
	default:
		break;
	}

	CDialog::OnTimer(nIDEvent);

}

void CTestMsgDlg::RefreshConnectStatus(void)
{
	RASCONNSTATUS tmp_constatus;
	DWORD tmp_dwstate;
	TCHAR szBuffer[100] = _T("");

	if (0 != hRasConn) {
		RasGetConnectStatus(hRasConn, &tmp_constatus);
		m_size = tmp_constatus.dwSize;
		m_error = tmp_constatus.dwError;
		if (0==m_error) {
			m_devtype = tmp_constatus.szDeviceType;
			m_devname = tmp_constatus.szDeviceName;
		}
		tmp_dwstate = tmp_constatus.rasconnstate;
		if ( (tmp_dwstate>=0) && (tmp_dwstate<=18) )  {
			wsprintf (szBuffer, TEXT("[%04X]RUNING: %s"), tmp_dwstate, status_char_running[tmp_dwstate]);
		}
		else if ( (tmp_dwstate>=RASCS_PAUSED) && (tmp_dwstate<=RASCS_PAUSED+3) )  {
			wsprintf (szBuffer, TEXT("[%04X]PAUSED: %s"), tmp_dwstate, status_char_pause[tmp_dwstate-RASCS_PAUSED]);
		}
		else if ( (tmp_dwstate>=RASCS_DONE) && (tmp_dwstate<=RASCS_DONE+1) )  {
			wsprintf (szBuffer, TEXT("[%04X]DONE: %s"), tmp_dwstate, status_char_done[tmp_dwstate-RASCS_DONE]);
			if (tmp_dwstate==RASCS_DONE) {  //Connected
				this->m_autodial.EnableWindow(FALSE);
				this->m_open.EnableWindow(FALSE);
				this->m_hangup.EnableWindow(TRUE);
			}
			else {	//DisConnected
				this->m_autodial.EnableWindow(TRUE);
				this->m_open.EnableWindow(TRUE);
				this->m_hangup.EnableWindow(FALSE);
				RasHangUp(hRasConn);
				hRasConn = 0;
			}
		}
		else  {
			//wsprintf (szBuffer, TEXT("[%04X]ERROR State Number!"), tmp_dwstate);
		}
		m_constate = szBuffer;
	}

}

BOOL MakeAutoRasDial (HWND hDlgWnd)
{
  BOOL bPassword;
  TCHAR szBuffer[100];

	SetWindowText (hDlgWnd, _T("AutoDial Start"));

    // Get the last configuration parameters used for this connection. 
    // If the password was saved, then the logon dialog box will not be
    // displayed.
    if (RasGetEntryDialParams (NULL, &RasDialParams, &bPassword) != 0)
    {
      MessageBox (hDlgWnd, 
                  TEXT("Could not get parameter details"), 
                  _T("Test Auto Dial"), //szTitle, 
                  MB_OK);
      return FALSE;
    }

    // Set hRasConn to NULL before attempting to connect.
    hRasConn = NULL;
 
  // Try to establish RAS connection.
  if (RasDial (NULL,            // Extension not supported
               NULL,            // Phone book is in registry
               &RasDialParams,  // RAS configuration for connection
               0xFFFFFFFF,      // Notifier type is a window handle
               hDlgWnd,         // Window receives notification message
               &hRasConn) != 0) // Connection handle
  {
    MessageBox (hDlgWnd, 
                TEXT("Could not connect using RAS"), 
                RasDialParams.szEntryName, //szTitle, 
                MB_OK);
    return FALSE;
  }

  wsprintf (szBuffer, TEXT("AutoDialing %s..."), RasDialParams.szEntryName);

  // Set the Dialing dialog box window name to szBuffer.
  SetWindowText (hDlgWnd, szBuffer);

  return TRUE;
}


BOOL MakeUserRasDial (HWND hDlgWnd)
{
  TCHAR szBuffer[100];

	SetWindowText (hDlgWnd, _T("UserDial Start"));

	// Set hRasConn to NULL before attempting to connect.
    hRasConn = NULL;

    // Initialize the structure.
    memset (&RasDialParams, 0, sizeof (RASDIALPARAMS));

    // Configure the RASDIALPARAMS structure. 
    RasDialParams.dwSize = sizeof (RASDIALPARAMS);
    RasDialParams.szPhoneNumber[0] = TEXT('\0');
    RasDialParams.szCallbackNumber[0] = TEXT('\0');
	wcscpy (RasDialParams.szEntryName, szRasEntryName);
    wcscpy (RasDialParams.szUserName, szUserName); //This is optional    wcscpy (RasDialParams.szPassword, szPassword); //This is optional
    wcscpy (RasDialParams.szPassword, szPassword);
    wcscpy (RasDialParams.szDomain, szDomain); //This is optional
 
  // Try to establish RAS connection.
  if (RasDial (NULL,            // Extension not supported
               NULL,            // Phone book is in registry
               &RasDialParams,  // RAS configuration for connection
               0xFFFFFFFF,      // Notifier type is a window handle
               hDlgWnd,         // Window receives notification message
               &hRasConn) != 0) // Connection handle
  {
    MessageBox (hDlgWnd, 
                TEXT("Could not connect using RAS"), 
                szRasEntryName, //szTitle, 
                MB_OK);
    return FALSE;
  }

  wsprintf (szBuffer, TEXT("UserDialing %s..."), szRasEntryName);

  // Set the Dialing dialog box window name to szBuffer.
  SetWindowText (hDlgWnd, szBuffer);

  return TRUE;
}



⌨️ 快捷键说明

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