Ras问题:请看下如下代码,为什么拨号上网不成功?
[复制链接]
DWORD NetDial(HWND hWnd, LPHRASCONN pRasConn, LPWSTR ConnectionName,LPWSTR DialNumber,LPWSTR lpszAPN)
{
DWORD dwRet = 0;
RASENTRY rasEntry;
RASDIALPARAMS RasDialParams;
CELLDEVCONFIG* cellcfg = NULL;
dwRet = RasValidateEntryName(NULL, ConnectionName);//判断连接是否存在
if(ERROR_INVALID_NAME == dwRet)
{
return dwRet;
}
else// if(0 == dwRet)//连接不存在
{
//新建连接
//设置rasEntry
memset(&rasEntry,0,sizeof(rasEntry));
rasEntry.dwSize = sizeof(RASENTRY);
rasEntry.dwfOptions = RASEO_ProhibitEAP|RASEO_RemoteDefaultGateway;
rasEntry.dwCountryCode = 86;
wcscpy(rasEntry.szAreaCode,L"10");
wcscpy(rasEntry.szLocalPhoneNumber,DialNumber);
rasEntry.dwfNetProtocols = RASNP_Ip;
rasEntry.dwFramingProtocol = RASFP_Ppp;
wcscpy(rasEntry.szDeviceType,RASDT_Modem);
wcscpy(rasEntry.szDeviceName,CELLTSP_LINENAME_STRING);//蜂窝电话线路(GPRS)
LPVARSTRING pInfo = NULL;
SetLogHwnd(hWnd);
if(GetDeviceInfo(rasEntry.szDeviceName,(BYTE **)(&pInfo)))
{
cellcfg =(CELLDEVCONFIG*)((BYTE*)pInfo + pInfo->dwStringOffset);
}
else
return 1;
//开始配置GPRS数据
cellcfg->bGPRSConnectionInfoValid = TRUE;
memset(&cellcfg->cgciGPRSConnectionInfo, 0x00,sizeof(cellcfg->cgciGPRSConnectionInfo));
wcscpy(cellcfg->cgciGPRSConnectionInfo.wszAccessPointName, lpszAPN);
cellcfg->cgciGPRSConnectionInfo.dwProtocolType = CELLDEVCONFIG_GPRSPROTOCOL_IP; //CELLDEVCONFIG_GPRSPROTOCOL_PPP;//
cellcfg->cgciGPRSConnectionInfo.dwL2ProtocolType = CELLDEVCONFIG_GPRSL2PROTOCOL_PPP;
cellcfg->cgciGPRSConnectionInfo.dwDataCompression = CELLDEVCONFIG_GPRSCOMPRESSION_OFF;
cellcfg->cgciGPRSConnectionInfo.dwHeaderCompression = CELLDEVCONFIG_GPRSCOMPRESSION_OFF;
dwRet = RasSetEntryProperties(NULL,ConnectionName,&rasEntry,rasEntry.dwSize,(BYTE*)(cellcfg),sizeof(CELLDEVCONFIG));
if(0 != dwRet)
{
return dwRet;
}
}
RasSetDialingPatterns();
//取参数
memset(&RasDialParams,0,sizeof(RASDIALPARAMS));
RasDialParams.dwSize = sizeof(RASDIALPARAMS);
wcscpy(RasDialParams.szEntryName, ConnectionName);
wcscpy(RasDialParams.szDomain,_T("*"));
//拨号
*pRasConn = NULL;
dwRet = RasDial(NULL,NULL,&RasDialParams,0xFFFFFFFF,hWnd,pRasConn);
return dwRet;
}
DWORD FindDeviceID(TCHAR * szDeviceName, BOOL * pfUnimodem)
{
DWORD dwDeviceID;
DWORD dwNumDevices;
DWORD rc;
HLINEAPP hLineApp;
LPLINEDEVCAPS lpDevCaps;
DWORD dwCapsLen;
LPTSTR lpszTSP;
CString szError = _T("");
rc = lineInitialize(
&hLineApp,
(HINSTANCE)1, // hInstance is unused
TapiCallback,
TEXT("RASENTRY"),
&dwNumDevices
);
if (rc) {
return -1;
}
dwCapsLen = sizeof(LINEDEVCAPS);
for (dwDeviceID = 0; dwDeviceID < dwNumDevices; dwDeviceID++) {
lpDevCaps = (LPLINEDEVCAPS)LocalAlloc(LPTR, dwCapsLen);
if (NULL == lpDevCaps) {
goto fdi_fail;
}
lpDevCaps->dwTotalSize = dwCapsLen;
rc = lineGetDevCaps(
hLineApp,
dwDeviceID,
TAPI_CURRENT_VERSION,
0,
lpDevCaps
);
if (rc) {
LocalFree(lpDevCaps);
continue;
}
if (lpDevCaps->dwTotalSize < lpDevCaps->dwNeededSize) {
dwCapsLen = lpDevCaps->dwNeededSize;
LocalFree(lpDevCaps);
lpDevCaps = (LPLINEDEVCAPS)LocalAlloc(LPTR, dwCapsLen);
if (NULL == lpDevCaps) {
goto fdi_fail;
}
lpDevCaps->dwTotalSize = dwCapsLen;
rc = lineGetDevCaps(
hLineApp,
dwDeviceID,
TAPI_CURRENT_VERSION,
0,
lpDevCaps
);
if ((rc) || (lpDevCaps->dwTotalSize < lpDevCaps->dwNeededSize)) {
LocalFree(lpDevCaps);
continue;
}
}
if (!_tcscmp((LPTSTR) ((char *)lpDevCaps + lpDevCaps->dwLineNameOffset), szDeviceName)) {
if (pfUnimodem) {
*pfUnimodem = FALSE;
if (lpDevCaps->dwProviderInfoSize) {
lpszTSP = (LPTSTR)((DWORD)lpDevCaps + lpDevCaps->dwProviderInfoOffset);
if (!_tcscmp(lpszTSP, TEXT("UNIMODEM"))) {
*pfUnimodem = TRUE;
}
}
}
lineShutdown(hLineApp);
LocalFree(lpDevCaps);
return dwDeviceID;
}
} // for dwDeviceID
fdi_fail:
lineShutdown(hLineApp);
return -1;
}
BOOL GetDeviceInfo(TCHAR * szDeviceName,BYTE ** pstDeviceInfo)
{
BOOL fUnimodem;
DWORD dwDeviceID;
DWORD dwNumDevices;
DWORD rc;
HLINEAPP hLineApp;
HLINE hLine;
LPVARSTRING lpDevConfig;
DWORD dwLen;
UNIMDM_CHG_DEVCFG UCD;
CString szError = _T("");
//
// Get device ID of the TAPI device whose line name matches the RASENTRY szDeviceName
//
dwDeviceID = FindDeviceID(szDeviceName, &fUnimodem);
if (dwDeviceID == -1) {
return FALSE;
}
//
// This interface is only supported by unimodem
//
//if (fUnimodem == FALSE) {
// return FALSE;
// }
rc = lineInitialize(
&hLineApp,
(HINSTANCE)1, // hInstance is unused
TapiCallback,
TEXT("RASENTRY"),
&dwNumDevices
);
if (rc) {
return FALSE;
}
lpDevConfig = NULL;
//
// Open the TAPI device
//
rc = lineOpen(
hLineApp,
dwDeviceID,
&hLine,
TAPI_CURRENT_VERSION,
0,
0,
LINECALLPRIVILEGE_MONITOR,
LINEMEDIAMODE_DATAMODEM,
NULL
);
if (rc) {
goto gbr_fail;
}
//
// Find out how big its DevConfig is and get it.
//
dwLen = sizeof(VARSTRING);
lpDevConfig = (LPVARSTRING)LocalAlloc(LPTR, dwLen);
if (NULL == lpDevConfig) {
goto gbr_fail;
}
lpDevConfig->dwTotalSize = dwLen;
rc = lineGetDevConfig(dwDeviceID,lpDevConfig,TEXT("comm/datamodem"));
if (rc) {
goto gbr_fail;
}
if (lpDevConfig->dwTotalSize < lpDevConfig->dwNeededSize) {
dwLen = lpDevConfig->dwNeededSize;
LocalFree(lpDevConfig);
lpDevConfig = (LPVARSTRING)LocalAlloc(LPTR, dwLen);
if (NULL == lpDevConfig) {
goto gbr_fail;
}
lpDevConfig->dwTotalSize = dwLen;
rc = lineGetDevConfig(dwDeviceID,lpDevConfig,TEXT("comm/datamodem"));
if (rc) {
goto gbr_fail;
}
}
*pstDeviceInfo = (BYTE *)lpDevConfig;
lineShutdown(hLineApp);
return TRUE;
gbr_fail:
if (lpDevConfig) {
LocalFree(lpDevConfig);
}
lineShutdown(hLineApp);
return FALSE;
}
BOOL RasSetDialingPatterns(void)
{
HKEY hKey;
DWORD dataType = REG_MULTI_SZ;
TCHAR szKeyName[] = TEXT("0");
LONG retVal = 0;
LONG ConfigurationStringSize = 512;
TCHAR ConfigurationString[512];
memset(ConfigurationString, 0, 512);
// Build the configuration string.
wcscat(ConfigurationString, TEXT("Work")); // dialing from
wcscat(ConfigurationString, TEXT("$"));
wcscat(ConfigurationString, TEXT("G")); // local calls
wcscat(ConfigurationString, TEXT("$"));
wcscat(ConfigurationString, TEXT("G")); // long distance calls
wcscat(ConfigurationString, TEXT("$"));
wcscat(ConfigurationString, TEXT("G")); // international calls
wcscat(ConfigurationString, TEXT("$"));
wcscat(ConfigurationString, TEXT("010")); // local area code
wcscat(ConfigurationString, TEXT("$"));
wcscat(ConfigurationString, TEXT("")); // disable call waiting by dialing
wcscat(ConfigurationString, TEXT("$"));
wcscat(ConfigurationString, TEXT("86")); // country/region code
wcscat(ConfigurationString, TEXT("$"));
wcscat(ConfigurationString, TEXT("0")); // tone or pulse
wcscat(ConfigurationString, TEXT("$"));
// Format the string correctly, take care please
int ConfigStringLen = wcslen(ConfigurationString);
for (int x = 0; x < ConfigStringLen; x++)
{
if (ConfigurationString[x] == '$')
{
ConfigurationString[x] = '\0';
}
}
// Open the registry key.
retVal = RegOpenKeyEx(HKEY_CURRENT_USER , TEXT("ControlPanel\\Dial\\Locations"), 0, KEY_READ, &hKey);
if (retVal != ERROR_SUCCESS)
{
return FALSE;
}
// Set the value of the key.
retVal = RegSetValueEx(hKey, szKeyName, NULL, dataType, (PBYTE)ConfigurationString, ConfigurationStringSize);
RegCloseKey(hKey);
if (retVal != ERROR_SUCCESS)
{
return FALSE;
}
else
{
return TRUE;
}
}