这个程序里有是否检测网络已断开,然后重连,大家可以根据自己的情况进行改变,一切为了分享,希望大家也都把这个精神发扬下去,在WM5.0、WM6.0 基于MFC的智能设备工程,测试通过 CDMA网络
*******************************************************************
//ConnectManager.h
#pragma once
class CConnectManager
{
public:
CConnectManager(void);
virtual ~CConnectManager(void);
void EnumNetIdentifier ( OUT CStringArray &StrAry );
BOOL GetConnMgrAvailable();
int MapURLAndGUID ( LPCTSTR lpszURL, GUID &guidNetworkObject, OUT CString *pcsDesc=NULL );
BOOL EstablishConnection ( DWORD dwIndex );
BOOL EstablishConnection ( GUID guid );//liu 4-29修改
BOOL WaitForConnected ( int nTimeoutSec, DWORD *pdwStatus=NULL );
private:
void ReleaseConnection ();
HANDLE m_hConnection;
};
//ConnectManager.cpp
#include "StdAfx.h"
#include "ConnectManager.h"
//#include "StdAfx.h"
//#include "ConnectManager.h"
// for connect manager function
#include <Connmgr.h>
#pragma comment ( lib, "Cellcore.lib" )
CConnectManager::CConnectManager(void)
{
}
CConnectManager::~CConnectManager(void)
{
ReleaseConnection ();
}
BOOL CConnectManager::GetConnMgrAvailable()
{
HANDLE hConnMgr = ConnMgrApiReadyEvent ();
BOOL bAvailbale = FALSE;
DWORD dwResult = ::WaitForSingleObject ( hConnMgr, 2000 );
if ( dwResult == WAIT_OBJECT_0 )
{
bAvailbale = TRUE;
}
// 关闭
if ( hConnMgr ) CloseHandle ( hConnMgr );
return bAvailbale;
}
int CConnectManager::MapURLAndGUID ( LPCTSTR lpszURL, OUT GUID &guidNetworkObject, OUT CString *pcsDesc/*=NULL*/ )
{
if ( !lpszURL || lstrlen(lpszURL) < 1 )
return FALSE;
memset ( &guidNetworkObject, 0, sizeof(GUID) );
int nIndex = 0;
HRESULT hResult = ConnMgrMapURL ( lpszURL, &guidNetworkObject, (DWORD*)&nIndex );
if ( FAILED(hResult) )
{
nIndex = -1;
DWORD dwLastError = GetLastError ();
AfxMessageBox ( _T("Could not map a request to a network identifier") );
}
else
{
if ( pcsDesc )
{
CONNMGR_DESTINATION_INFO DestInfo = {0};
if ( SUCCEEDED(ConnMgrEnumDestinations(nIndex, &DestInfo)) )
{
*pcsDesc = DestInfo.szDescription;
}
}
}
return nIndex;
}
void CConnectManager::EnumNetIdentifier ( OUT CStringArray &StrAry )
{
CONNMGR_DESTINATION_INFO networkDestInfo = {0};
// 得到网络列表
for ( DWORD dwEnumIndex=0; ; dwEnumIndex++ )
{
memset ( &networkDestInfo, 0, sizeof(CONNMGR_DESTINATION_INFO) );
if ( ConnMgrEnumDestinations ( dwEnumIndex, &networkDestInfo ) == E_FAIL )
{
break;
}
StrAry.Add ( networkDestInfo.szDescription );
//AfxMessageBox (dwEnumIndex);
CString tt;
tt.Format(L"%u",dwEnumIndex);
}
}
//指定拨号连接
BOOL CConnectManager::EstablishConnection ( DWORD dwIndex )
{
// 释放之前的连接
ReleaseConnection ();
// 得到正确的连接信息
CONNMGR_DESTINATION_INFO DestInfo = {0};
HRESULT hResult = ConnMgrEnumDestinations(dwIndex, &DestInfo);
BOOL bRet = FALSE;
if(SUCCEEDED(hResult))
{
// 初始化连接结构
CONNMGR_CONNECTIONINFO ConnInfo;
ZeroMemory(&ConnInfo, sizeof(ConnInfo));
ConnInfo.cbSize = sizeof(ConnInfo);
ConnInfo.dwParams = CONNMGR_PARAM_GUIDDESTNET;
ConnInfo.dwFlags = CONNMGR_FLAG_PROXY_HTTP | CONNMGR_FLAG_PROXY_WAP | CONNMGR_FLAG_PROXY_SOCKS4 | CONNMGR_FLAG_PROXY_SOCKS5;//参数dwFlags用于指定我们的接入点,比如我们常说的CMNER和CMWAP
ConnInfo.dwPriority = CONNMGR_PRIORITY_USERINTERACTIVE;
ConnInfo.guidDestNet = DestInfo.guid;
ConnInfo.bExclusive = FALSE;
ConnInfo.bDisabled = FALSE;
DWORD dwStatus = 0;
hResult = ConnMgrEstablishConnectionSync(&ConnInfo, &m_hConnection, 10*1000, &dwStatus );
if(FAILED(hResult))
{
m_hConnection = NULL;
}
else bRet = TRUE;
}
return bRet;
}
//指定拨号连接liu 4-29 添加
BOOL CConnectManager::EstablishConnection ( GUID guid )
{
// 释放之前的连接
ReleaseConnection ();
// 得到正确的连接信息
HRESULT hResult ;
BOOL bRet = FALSE;
// 初始化连接结构
CONNMGR_CONNECTIONINFO ConnInfo;
ZeroMemory(&ConnInfo, sizeof(ConnInfo));
ConnInfo.cbSize = sizeof(ConnInfo);
ConnInfo.dwParams = CONNMGR_PARAM_GUIDDESTNET;
ConnInfo.dwFlags = CONNMGR_FLAG_PROXY_HTTP | CONNMGR_FLAG_PROXY_WAP | CONNMGR_FLAG_PROXY_SOCKS4 | CONNMGR_FLAG_PROXY_SOCKS5 | CONNMGR_FLAG_SUSPEND_AWARE;//参数dwFlags用于指定我们的接入点,比如我们常说的CMNER和CMWAP
ConnInfo.dwPriority = CONNMGR_PRIORITY_USERINTERACTIVE;
ConnInfo.guidDestNet = guid;
ConnInfo.bExclusive = FALSE;
ConnInfo.bDisabled = FALSE;
DWORD dwStatus = 0;
hResult = ConnMgrEstablishConnectionSync(&ConnInfo, &m_hConnection, 10*1000, &dwStatus );
if(FAILED(hResult))
{
m_hConnection = NULL;
}
else
bRet = TRUE;
return bRet;
}
BOOL CConnectManager::WaitForConnected ( int nTimeoutSec, DWORD *pdwStatus/*=NULL*/ )
{
DWORD dwStartTime = GetTickCount ();//获取开机以来的毫秒数
BOOL bRet = FALSE;
while ( GetTickCount ()-dwStartTime < (DWORD)nTimeoutSec * 1000 )//定时为2秒间隔
{
if ( m_hConnection )
{
DWORD dwStatus = 0;
HRESULT hr = ConnMgrConnectionStatus ( m_hConnection, &dwStatus );//利用此函数的返回值dwStatus,我们可以得到很多的关于连接的信息,例如如果我们连接成功将返回CONNMGR_STATUS_CONNECTED,断开连接将返回CONNMGR_STATUS_DISCONNECTED,他的返回状态信息非常丰富
if ( pdwStatus ) *pdwStatus = dwStatus;
if ( SUCCEEDED(hr) )
{
if ( dwStatus == CONNMGR_STATUS_CONNECTED )
{
bRet = TRUE;
break;
}
}
}
Sleep ( 100 );
}
return bRet;
}
void CConnectManager::ReleaseConnection ()
{
if ( m_hConnection )
{
ConnMgrReleaseConnection(m_hConnection, FALSE);
m_hConnection = NULL;
}
}
*********************************************************************************
测试用例:
BOOL YourClass::ConnectManagerTest(void)
{
BOOL bRet=false;
DWORD dwStatus = 0;
CString url;
CString strDialup;
GUID guid;
CArray<CString, CString> ss;//第一个参数就是CArray类数组元素的变量类型,后一个是函数调用时的参数类型
if (!m_ConnectManager.WaitForConnected ( 2, &dwStatus ) )
{
m_ConnectManager.MapURLAndGUID(url,guid);//URL是指定的一个公网地址URL,随便哪个都可以的,如www.baidu.com
if ( m_ConnectManager.EstablishConnection (guid) )
{
bRet=true;
}
else
{
bRet=false;
}
}
else
bRet=true;
return bRet;
}