一个朋友研究了在smartphone上建立GPRS拨号连接的情况,贴出一些代码供需要的朋友参考:
http://cpp.sourceforge.net/?show=39205
怕这个链接以后会失效,我还是先copy一份过来。
#include "stdafx.h"
#include <wchar.h>
#include <tapi.h>
#include <tsp.h>
#include <ras.h>
#include <raserror.h>
#include <vector>
DWORD FindCellLine()
{
LONG lRet = 0;
DWORD dwDeviceNum = 0;
DWORD dwDeviceID = -1, dwTapiVersion = TAPI_CURRENT_VERSION;
HLINEAPP hLineApp = NULL;
LINEINITIALIZEEXPARAMS initParams = {0};
initParams.dwTotalSize = sizeof(initParams);
initParams.dwOptions = LINEINITIALIZEEXOPTION_USEEVENT;
std::vector<char> buffer(1024, '\0');
LPLINEDEVCAPS devcaps =(LINEDEVCAPS*) &buffer[0];
devcaps->dwTotalSize = buffer.size();
lRet = lineInitializeEx(&hLineApp, GetModuleHandle(NULL), NULL,
L"GPRS Device Finder", &dwDeviceNum, &dwTapiVersion, &initParams);
if (lRet != 0)
return -1;
for(DWORD dwDeviceIndex = 0; dwDeviceIndex != dwDeviceNum; dwDeviceIndex++)
{
lineGetDevCaps(hLineApp, dwDeviceIndex, dwTapiVersion, 0, devcaps);
if (devcaps->dwNeededSize > devcaps->dwTotalSize)
{
buffer.resize(devcaps->dwNeededSize);
devcaps =(LINEDEVCAPS*) &buffer[0];
devcaps->dwTotalSize = buffer.size();
dwDeviceIndex--;
continue;
}
if (wcscmp((LPCTSTR) &buffer[devcaps->dwLineNameOffset],
CELLTSP_LINENAME_STRING) == 0)
{
dwDeviceID = dwDeviceIndex;
break;
}
}
lineShutdown(hLineApp);
return dwDeviceID;
}
BOOL SetupGprsRasEntry(LPWSTR szEntryName, LPCWSTR szAccessPointName)
{
DWORD dwLineID = 0;
std::vector<char> buffer(sizeof(VARSTRING) + sizeof(CELLDEVCONFIG), '\0');
LPVARSTRING varbuffer =(LPVARSTRING) &buffer[0];
varbuffer->dwTotalSize = buffer.size();
LPCELLDEVCONFIG cellcfg = NULL;
{//查找Cell Line的设备ID
if ((dwLineID = FindCellLine()) == -1)
return FALSE;
}
{//获得默认数据配置
if (lineGetDevConfig(dwLineID, varbuffer, L"comm/datamodem") != 0)
return FALSE;
cellcfg =(LPCELLDEVCONFIG) &buffer[varbuffer->dwStringOffset];
}
{//开始配置GPRS数据
cellcfg->bGPRSConnectionInfoValid = TRUE;
memset(&cellcfg->cgciGPRSConnectionInfo, 0x00,
sizeof(cellcfg->cgciGPRSConnectionInfo));
wcscpy(cellcfg->cgciGPRSConnectionInfo.wszAccessPointName, szAccessPointName);
cellcfg->cgciGPRSConnectionInfo.dwProtocolType = CELLDEVCONFIG_GPRSPROTOCOL_IP;
cellcfg->cgciGPRSConnectionInfo.dwL2ProtocolType = CELLDEVCONFIG_GPRSL2PROTOCOL_PPP;
cellcfg->cgciGPRSConnectionInfo.dwDataCompression = CELLDEVCONFIG_GPRSCOMPRESSION_OFF;
cellcfg->cgciGPRSConnectionInfo.dwHeaderCompression = CELLDEVCONFIG_GPRSCOMPRESSION_OFF;
}
{//安装GPRS拨号项
RASENTRY rsaentry = {0};
rsaentry.dwSize = sizeof(rsaentry);
rsaentry.dwfOptions = RASEO_ProhibitEAP|RASEO_RemoteDefaultGateway;
rsaentry.dwfNetProtocols = RASNP_Ip;
rsaentry.dwFramingProtocol = RASFP_Ppp;
wcscpy(rsaentry.szDeviceName, CELLTSP_LINENAME_STRING);
wcscpy(rsaentry.szDeviceType, RASDT_Modem);
wcscpy(rsaentry.szLocalPhoneNumber, L"~GPRS!");
if (RasSetEntryProperties(NULL, szEntryName, &rsaentry, sizeof(rsaentry),
(LPBYTE) cellcfg, sizeof(*cellcfg)) != 0)
return FALSE;
}
return TRUE;
}