在 CE 下如果系统没有建立拨号网络,要实现上网功能时,就需要通过代码来实现建立拨号网络、拨号实现上网的过程。
以下代码中的参数和拨号所用数值,都是电信 EVDO 的配置。如果换 GSM 或 WCDMA 时,请相应修改。
HRASCONN ghRasConn = NULL;
/*
* 设置串口波特率和数据位等
*/
BOOL CTCPClientDlg::SetDevCfg(TCHAR *ptcRasEntryName)
{
HKEY hk;
unsigned char cDevCfg[684];
TCHAR tcRegAddr[512 + 1];
DWORD *pdwEapConnData = NULL;
ZeroMemory(tcRegAddr,sizeof(TCHAR) * (512 + 1));
ZeroMemory(cDevCfg,sizeof(char) * 683);
cDevCfg[0] = 0x30;
cDevCfg[4] = 0x05;
cDevCfg[9] = 0x01;
cDevCfg[13] = 0xC2; // 波特率
cDevCfg[14] = 0x01;
cDevCfg[18] = 0x08; // 数据位
wsprintf(tcRegAddr,L"Comm\\RasBook\\%s",ptcRasEntryName);
if(ERROR_SUCCESS == RegOpenKeyEx(HKEY_CURRENT_USER,tcRegAddr,0,0,&hk))
{
RegSetValueEx(hk,L"DevCfg",0,REG_BINARY,(LPBYTE)cDevCfg,sizeof(cDevCfg));
RegSetValueEx(hk,L"EapConnData",0,REG_BINARY,(LPBYTE)pdwEapConnData,0);
RegFlushKey(HKEY_CURRENT_USER);
RegCloseKey(hk);
return TRUE;
}
return FALSE;
}
BOOL CTCPClientDlg::CreateRASLink(TCHAR *ptcRasEntryName,TCHAR *ptcModemName)
{
LPRASENTRY lpRasEntry = NULL;
DWORD dwRasEntrySize = sizeof(RASENTRY);
DWORD dwBufferSize = 0;
DWORD dwRet = 0;
BOOL bRet = TRUE;
if(NULL == ptcRasEntryName || NULL == ptcModemName)
return FALSE;
RasGetEntryProperties(NULL,L"",NULL,&dwBufferSize,NULL,NULL);
if(0 == dwBufferSize)
return FALSE;
lpRasEntry = (LPRASENTRY)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,dwBufferSize);
if(NULL == lpRasEntry)
return FALSE;
ZeroMemory(lpRasEntry,sizeof(RASENTRY));
lpRasEntry->dwSize = dwBufferSize;
lpRasEntry->dwfOptions = RASEO_RemoteDefaultGateway; // RASEO_PreviewUserPw 弹出用户名和密码输入框
// Leo lpRasEntry->dwType = RASET_Phone;
// lpRasEntry->dwCountryID = 86; // This member is currently ignored by Windows CE.
lpRasEntry->dwCountryCode = 86;
lstrcpy(lpRasEntry->szAreaCode,L"10");
lstrcpy(lpRasEntry->szLocalPhoneNumber,L"#777");
lstrcpy(lpRasEntry->szDeviceType,RASDT_Modem);
lstrcpy(lpRasEntry->szDeviceName,ptcModemName); // 选用 Modem 的名字
lpRasEntry->dwfNetProtocols = RASNP_Ip;
lpRasEntry->dwFramingProtocol = RASFP_Ppp;
dwRet = RasSetEntryProperties(NULL,ptcRasEntryName,lpRasEntry,dwBufferSize,NULL,0); // 创建连接
if(0 != dwRet)
{
bRet = FALSE;
}
HeapFree(GetProcessHeap(),0,(LPVOID)lpRasEntry);
return bRet;
}
BOOL CTCPClientDlg::NetworkDialAndParams(TCHAR *ptcRasEntryName)
{
DWORD dwRet = 0;
UINT i = 0;
RASDIALPARAMS rdParams;
ZeroMemory(&rdParams, sizeof(RASDIALPARAMS));
rdParams.dwSize = sizeof(RASDIALPARAMS);
wcscpy(rdParams.szEntryName,ptcRasEntryName);
// wcscpy(rdParams.szPhoneNumber,L"#777");
wcscpy(rdParams.szUserName,L"card");
wcscpy(rdParams.szPassword,L"card");
ghRasConn = NULL;
dwRet = RasDial(NULL,NULL,&rdParams,0xFFFFFFFF,NULL,&ghRasConn);
if(0 != dwRet)
{
RETAILMSG(1,(L"RasDial Error: %d.\r\n",dwRet));
return FALSE;
}
return TRUE;
}
调用示例:
void CNetworkDialDlg::OnBtnClickedOpenNet()
{
GetDlgItem(IDC_BUTTON1)->EnableWindow(FALSE);
m_NetName.EnableWindow(FALSE);
// TODO: Add your control notification handler code here
if(CreateRASLink(RASDIAL_NET_NAME,RASDIAL_NET_MODEM_NAME))
{
if(SetDevCfg(RASDIAL_NET_NAME))
{
if(NetworkDialAndParams(RASDIAL_NET_NAME,ghMainWnd))
{
RETAILMSG(1,(L"[TCP Client]Dial new work success.\r\n"));
SetTimer(RASDIAL_NETSHOW_TIMER_ID,RASDIAL_NETSHOW_TIME_OUT,NULL);
}
else
{
RETAILMSG(1,(L"[TCP Client]Dial new work failed!\r\n"));
}
}
else
{
RETAILMSG(1,(L"[TCP Client]Set RAS device config failed!\r\n"));
}
}
else
{
RETAILMSG(1,(L"[TCP Client]Create RAS link failed!\r\n"));
}
}
BOOL CNetworkDialDlg::PreTranslateMessage(MSG* pMsg)
{
switch(pMsg->message)
{
case WM_RASDIALEVENT:
OnRasDialEvent(pMsg->wParam,pMsg->lParam);
break;
case WM_USER + 0x1000:
if(1 == pMsg->wParam)
{
GetDlgItem(IDC_STATIC_PING_STATUS)->SetWindowText(L"PING 状态: 成功");
}
else
{
GetDlgItem(IDC_STATIC_PING_STATUS)->SetWindowText(L"PING 状态: 失败");
}
break;
default:
break;
}
return CDialog::PreTranslateMessage(pMsg);
}
/*
[TCP Ras Event]1.RasState: Open port
[TCP Ras Event]2.RasState: Port opened
[TCP Ras Event]3.RasState: Device Connected
[TCP Ras Event]4.RasState: All Device Connected
[TCP Ras Event]5.Authenticating
[TCP Ras Event]9.RAS Other RasState:12
[TCP Ras Event]6.Authenticated, continue login...
[TCP Ras Event]7.RASCS_Connected received
*/
LRESULT CNetworkDialDlg::OnRasDialEvent(WPARAM wParam,LPARAM lParam)
{
RASCONNSTATE RasState = (RASCONNSTATE)wParam;
DWORD dwError = (DWORD)lParam;
gRasState = RasState;
switch(RasState)
{
case RASCS_OpenPort:
RETAILMSG(1,(L"[TCP Ras Event]1.RasState: Open port\r\n"));
break;
case RASCS_PortOpened: // 端口已经打开
RETAILMSG(1,(L"[TCP Ras Event]2.RasState: Port opened\r\n"));
break;
case RASCS_DeviceConnected: // 正在连接
{
// dialDeviceConnected = TRUE;
RETAILMSG(1,(L"[TCP Ras Event]3.RasState: Device Connected\r\n"));
}
break;
case RASCS_AllDevicesConnected: //
// dialDeviceConnected = TRUE;
RETAILMSG(1,(L"[TCP Ras Event]4.RasState: All Device Connected\r\n"));
break;
case RASCS_Authenticate:
{
// dialDeviceConnected = TRUE;
RETAILMSG(1,(L"[TCP Ras Event]5.Authenticating\r\n"));
}
break;
case RASCS_Authenticated:
{
// dialDeviceConnected = TRUE;
RETAILMSG(1,(L"[TCP Ras Event]6.Authenticated, continue login...\r\n"));
}
break;
case RASCS_AuthNotify:
{
RETAILMSG(1,(L"[TCP Ras Event]RASCS_AuthNofify received,Error: %d\r\n",dwError));
switch(dwError)
{
case ERROR_RESTRICTED_LOGON_HOURS:
case ERROR_ACCT_DISABLED:
case ERROR_PASSWD_EXPIRED:
case ERROR_NO_DIALIN_PERMISSION:
case ERROR_CHANGING_PASSWORD:
default:
break;
}
}
break;
case RASCS_Connected:
{
RETAILMSG(1,(L"[TCP Ras Event]7.RASCS_Connected received\r\n"));
gbNetConnected = TRUE;
}
break;
case RASCS_Disconnected:
{
CString csError;
RETAILMSG(1,(L"[TCP Ras Event]8.RASCS_Disconnected received. Error: %d\r\n",dwError));
csError.Format(L"建立网络出现错误:%d",dwError);
GetDlgItem(IDC_BUTTON1)->EnableWindow(TRUE);
m_NetName.EnableWindow(TRUE);
giRasDialTimerCount = 0;
GetDlgItem(IDC_STATIC_STATUS)->SetWindowText(csError);
KillTimer(RASDIAL_NETSHOW_TIMER_ID);
}
break;
default:
RETAILMSG(1,(L"[TCP Ras Event]9.RAS Other RasState:%d\r\n", RasState));
break;
}
return TRUE;
}