【转载】RAS使用拨号网络拨号的类

转载自:http://blog.csdn.net/91program/article/details/6311181

前一段时间使用了Socket与远程PC进行UDP通讯,使用了一个RAS拨号类,与大家分享一下。

在使用下面的代码建立 TCP/IP 链接前,请断开 ActiveSync,否则会导致失败。

头文件:RasCtrl.h 

#if !defined  _RAS_PPP_CTRL_H_
#define       _RAS_PPP_CTRL_H_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include "ras.h"
class CRasCtrl  
{
private:
	LPVOID m_MsgHandle; 
	HRASCONN m_hRasConn;
	//BOOL bConnect;
	//CString m_UserName;
	//CString m_PassWord;
	CString m_EntryName;
public:
	CRasCtrl();
	virtual ~CRasCtrl();
	//拨号
	bool DialUp(CString UserName,CString Password);
	//挂断
	bool HangUp();
	//设置处理方式
	void SetHandle(LPVOID _handle);
	bool IsConnect(void);
	//通过获取WM_RASDIALEVENT消息,判断拨号状态
	//static CString GetState(unsigned int message); 
};
#endif 

源文件:RasCtrl.cpp 

#include "stdafx.h"
#include "RasCtrl.h"
#include "ras.h"
#include "Raserror.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CRasCtrl::CRasCtrl()
{   
	m_hRasConn = NULL;
	//bConnect = false;
	m_EntryName = L"MyLink";	//建立拨号的名字,保持一致
}
CRasCtrl::~CRasCtrl()
{
	HangUp();
}
bool CRasCtrl::DialUp(CString UserName,CString Password)
{
	RASDIALPARAMS rdParams;    //拨号连接的信息
	ZeroMemory(&rdParams, sizeof(RASDIALPARAMS));
	rdParams.dwSize = sizeof(RASDIALPARAMS);
	//HangUp();
	//Sleep(100);
	//RasSetEntryDialParams(NULL,&rdParams,true);
	//RasGetEntryProperties();
	wcscpy(rdParams.szEntryName, m_EntryName);
	wcscpy(rdParams.szUserName, UserName);
	wcscpy(rdParams.szPassword, Password); 
	//lstrcpy(rdParams.szPhoneNumber, _T(""));
	//lstrcpy(rdParams.szDomain, _T(""));
	m_hRasConn = NULL;
	DWORD dwRet = ::RasDial(NULL, NULL, &rdParams, 0xFFFFFFFF, m_MsgHandle , &m_hRasConn);  //如果函数成功,则返回0
        //也可以RasHangUp(m_hRasConn);挂断,不过我曾经试过拨号成功m_hRasConn为NULL的情况,一时不清除怎么回事,所以把挂断函数改了。
	//RasDial函数的使用要小心一点跟windows平台有些差异。
	//DWORD iRet = GetLastError();
	if (dwRet) 
	{
		//bConnect = false;
		return false;
	}
	//bConnect = true;
	return true;
}
bool CRasCtrl::HangUp()
{
	int index;                 // An integer index
	DWORD dwError,             // Error code from a function call 
	dwRasConnSize,       // Size of RasConn in bytes
	dwNumConnections;    // Number of connections found 
	RASCONN RasConn[20];       // Buffer for connection state data 
	// Assume the maximum number of entries is 20. 
	// Assume no more than 20 connections.
	RasConn[0].dwSize = sizeof (RASCONN);
	dwRasConnSize = 20 * sizeof (RASCONN);
	// Find all connections.
	if (dwError = RasEnumConnections (RasConn, &dwRasConnSize, &dwNumConnections))
	{
		return false;
	}
	// If there are no connections, return zero.
	if (!dwNumConnections)
	{
		return false;
	}
	// Terminate all of the remote access connections.
	for (index = 0; index < (int)dwNumConnections; ++index)
	{
		//这样做主要是不想关掉usb连接,因为通过这种方法得到的连接中包括了USB同步的连接。
		if (wcsstr(RasConn[index].szEntryName,_T("MyLink"))!=NULL)
		{
			if (dwError = RasHangUp (RasConn[index].hrasconn))
			{
				return false;
			}
		}
	}
	return TRUE;
}
// 传递接收消息的窗体句柄进来,这样窗体才能接收到WM_RASDIALEVENT消息。
// wParam的值有RASCS_Connected,RASCS_Disconnected等,具体查看msdn
// wince不支持其他方式。      
void CRasCtrl::SetHandle(LPVOID _handle)
{  
	m_MsgHandle = _handle;
}
bool CRasCtrl::IsConnect(void)
{
	if(NULL != m_hRasConn)
	{
		RASCONNSTATUS rasConStatus;
		rasConStatus.dwSize = sizeof(RASCONNSTATUS);
		RasGetConnectStatus(m_hRasConn,&rasConStatus);
		if(RASCS_Connected == rasConStatus.rasconnstate)
		{
			return true;
		}
	}
	return false;
}
/*
这个用于解析PPPoE返回的消息的
CString CRasCtrl::GetState(unsigned int message)
{
 CString str;
 switch(message)
 {
 ... ... ...
 case RASCS_Connected:
  str= LoadStringEx(IDS_LINKED);
  break;
 case RASCS_Disconnected:
  str= LoadStringEx(IDS_UNLINKED);
  break;
 }
 return str;
}
*/

 

posted @ 2013-06-13 17:28  风雨雪夜  阅读(202)  评论(0编辑  收藏  举报