在国际上,通常只有一种GPRS接入方式,在中国有CMWAP和CMNET两个接入点,前者是为手机WAP上网而设立的,后者则主要是为PC、笔记本电脑、PDA等利用GPRS上网服务。它们在实现方式上并没有任何差别,但因为定位不同,所以和CMNET相比,CMWAP便有了部分限制,资费上也存在差别。
下面这段代码连接CMNET.
//连接GPRS
DWORD dwStatus;// = 0;
HANDLE hConnection=NULL;
CONNMGR_CONNECTIONINFO sConInfo;
memset(&sConInfo, 0 ,sizeof(CONNMGR_CONNECTIONINFO));
sConInfo.cbSize = sizeof(CONNMGR_CONNECTIONINFO);
sConInfo.dwParams = CONNMGR_PARAM_GUIDDESTNET;
sConInfo.dwPriority = CONNMGR_PRIORITY_USERINTERACTIVE;
sConInfo.dwFlags = CONNMGR_FLAG_PROXY_HTTP|CONNMGR_FLAG_PROXY_WAP|CONNMGR_FLAG_PROXY_SOCKS4|CONNMGR_FLAG_PROXY_SOCKS5;
sConInfo.bExclusive = FALSE;
sConInfo.bDisabled = FALSE;
sConInfo.guidDestNet = IID_DestNetInternet; //IID_DestNetWAP
sConInfo.hWnd = 0;
sConInfo.uMsg = 0;
if (FAILED(ConnMgrEstablishConnectionSync(&sConInfo, &hConnection, 60000, &dwStatus))) ;
目前,移动的WAP网关对外只提供HTTP代理协议(80和8080端口)和WAP网关协议(9201端口),彩信还有单独配置接入点是因为彩信服务需要连接专用的服务器。所以把上面那段代码中的IID_DestNetInternet换成IID_DestNetWAP,实际中是不能连通GPRS的。
if (m_hConnection)
{
ConnMgrReleaseConnection(m_hConnection, FALSE);
m_hConnection = NULL;
}
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 );
}
int Index =0;
//查找“WAP”的索引
for (int i = 0; i < StrAry.GetCount(); ++i)
{
if (StrAry[i] == L"WAP")
{
Index = i;
break;
}
}
// 得到正确的连接信息
CONNMGR_DESTINATION_INFO DestInfo = {0};
HRESULT hResult = ConnMgrEnumDestinations(Index, &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;
ConnInfo.dwPriority = CONNMGR_PRIORITY_HIPRIBKGND;//CONNMGR_PRIORITY_USERINTERACTIVE;
ConnInfo.guidDestNet = DestInfo.guid;
ConnInfo.bExclusive = FALSE;
ConnInfo.bDisabled = FALSE;
DWORD dwStatus = 0;
hResult = ConnMgrEstablishConnectionSync(&ConnInfo, &m_hConnection, 100*1000, &dwStatus );
// if(FAILED(hResult))
// {
// MessageBox(NULL,L"连接失败",L"错误信息",MB_OK);
// m_hConnection = NULL;
// }
// else
// {
// MessageBox(NULL,L"连接成功",L"错误信息",MB_OK);
// return TRUE;
// }
}
上面两段代码仅限于连接移动的GPRS。注意:m_hConnection最好设成全局变量,这样程序退出时关闭GPRS
所需的头文件和lib库
#include <connmgr.h>
#include <connmgr_proxy.h>
#pragma comment( lib, "cellcore.lib" )