码迷,mamicode.com
首页 > Windows程序 > 详细

WinCE 下 RAS 拨号连接的建立和拨号过程

时间:2016-02-22 10:29:13      阅读:418      评论:0      收藏:0      [点我收藏+]

标签:

在 CE 下如果系统没有建立拨号网络,要实现上网功能时,就需要通过代码来实现建立拨号网络、拨号实现上网的过程。

以下代码中的参数和拨号所用数值,都是电信 EVDO 的配置。如果换 GSM 或 WCDMA 时,请相应修改。

  1 HRASCONN ghRasConn = NULL;  
  2 /* 
  3  * 设置串口波特率和数据位等 
  4 */  
  5 BOOL CTCPClientDlg::SetDevCfg(TCHAR *ptcRasEntryName)  
  6 {  
  7     HKEY hk;  
  8     unsigned char cDevCfg[684];  
  9     TCHAR tcRegAddr[512 + 1];  
 10     DWORD *pdwEapConnData = NULL;  
 11   
 12     ZeroMemory(tcRegAddr,sizeof(TCHAR) * (512 + 1));  
 13     ZeroMemory(cDevCfg,sizeof(char) * 683);  
 14     cDevCfg[0] = 0x30;  
 15     cDevCfg[4] = 0x05;  
 16     cDevCfg[9] = 0x01;  
 17     cDevCfg[13] = 0xC2;     // 波特率  
 18     cDevCfg[14] = 0x01;  
 19     cDevCfg[18] = 0x08;     // 数据位  
 20   
 21     wsprintf(tcRegAddr,L"Comm\\RasBook\\%s",ptcRasEntryName);  
 22   
 23     if(ERROR_SUCCESS == RegOpenKeyEx(HKEY_CURRENT_USER,tcRegAddr,0,0,&hk))  
 24     {  
 25         RegSetValueEx(hk,L"DevCfg",0,REG_BINARY,(LPBYTE)cDevCfg,sizeof(cDevCfg));  
 26         RegSetValueEx(hk,L"EapConnData",0,REG_BINARY,(LPBYTE)pdwEapConnData,0);  
 27         RegFlushKey(HKEY_CURRENT_USER);  
 28   
 29         RegCloseKey(hk);  
 30   
 31         return TRUE;  
 32     }  
 33     return FALSE;  
 34 }  
 35   
 36 BOOL CTCPClientDlg::CreateRASLink(TCHAR *ptcRasEntryName,TCHAR *ptcModemName)  
 37 {  
 38     LPRASENTRY lpRasEntry = NULL;  
 39     DWORD dwRasEntrySize = sizeof(RASENTRY);  
 40     DWORD dwBufferSize = 0;  
 41     DWORD dwRet = 0;  
 42     BOOL bRet = TRUE;  
 43   
 44     if(NULL == ptcRasEntryName || NULL == ptcModemName)  
 45         return FALSE;  
 46   
 47     RasGetEntryProperties(NULL,L"",NULL,&dwBufferSize,NULL,NULL);   
 48     if(0 == dwBufferSize)  
 49         return FALSE;  
 50   
 51     lpRasEntry = (LPRASENTRY)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,dwBufferSize);  
 52     if(NULL == lpRasEntry)  
 53         return FALSE;  
 54   
 55     ZeroMemory(lpRasEntry,sizeof(RASENTRY));  
 56     lpRasEntry->dwSize = dwBufferSize;  
 57     lpRasEntry->dwfOptions = RASEO_RemoteDefaultGateway;     // RASEO_PreviewUserPw 弹出用户名和密码输入框  
 58     // Leo lpRasEntry->dwType = RASET_Phone;  
 59     // lpRasEntry->dwCountryID = 86; // This member is currently ignored by Windows CE.  
 60     lpRasEntry->dwCountryCode = 86;  
 61     lstrcpy(lpRasEntry->szAreaCode,L"10");  
 62     lstrcpy(lpRasEntry->szLocalPhoneNumber,L"#777");  
 63   
 64     lstrcpy(lpRasEntry->szDeviceType,RASDT_Modem);  
 65     lstrcpy(lpRasEntry->szDeviceName,ptcModemName);      // 选用 Modem 的名字  
 66     lpRasEntry->dwfNetProtocols = RASNP_Ip;  
 67     lpRasEntry->dwFramingProtocol = RASFP_Ppp;  
 68   
 69     dwRet = RasSetEntryProperties(NULL,ptcRasEntryName,lpRasEntry,dwBufferSize,NULL,0);  //  创建连接   
 70     if(0 != dwRet)  
 71     {  
 72         bRet = FALSE;  
 73     }  
 74     HeapFree(GetProcessHeap(),0,(LPVOID)lpRasEntry);  
 75   
 76     return bRet;  
 77 }  
 78   
 79 BOOL CTCPClientDlg::NetworkDialAndParams(TCHAR *ptcRasEntryName)  
 80 {  
 81     DWORD dwRet = 0;  
 82     UINT i = 0;  
 83     RASDIALPARAMS rdParams;  
 84   
 85     ZeroMemory(&rdParams, sizeof(RASDIALPARAMS));    
 86   
 87     rdParams.dwSize = sizeof(RASDIALPARAMS);  
 88     wcscpy(rdParams.szEntryName,ptcRasEntryName);  
 89     // wcscpy(rdParams.szPhoneNumber,L"#777");  
 90     wcscpy(rdParams.szUserName,L"card");  
 91     wcscpy(rdParams.szPassword,L"card");  
 92   
 93     ghRasConn = NULL;  
 94     dwRet = RasDial(NULL,NULL,&rdParams,0xFFFFFFFF,NULL,&ghRasConn);  
 95     if(0 != dwRet)  
 96     {  
 97         RETAILMSG(1,(L"RasDial Error: %d.\r\n",dwRet));  
 98         return FALSE;  
 99     }  
100     return TRUE;  
101 }  

调用示例:

  1 void CNetworkDialDlg::OnBtnClickedOpenNet()  
  2 {  
  3     GetDlgItem(IDC_BUTTON1)->EnableWindow(FALSE);  
  4     m_NetName.EnableWindow(FALSE);  
  5   
  6     // TODO: Add your control notification handler code here  
  7     if(CreateRASLink(RASDIAL_NET_NAME,RASDIAL_NET_MODEM_NAME))  
  8     {  
  9         if(SetDevCfg(RASDIAL_NET_NAME))  
 10         {  
 11             if(NetworkDialAndParams(RASDIAL_NET_NAME,ghMainWnd))  
 12             {  
 13                 RETAILMSG(1,(L"[TCP Client]Dial new work success.\r\n"));  
 14                   
 15                 SetTimer(RASDIAL_NETSHOW_TIMER_ID,RASDIAL_NETSHOW_TIME_OUT,NULL);  
 16             }  
 17             else  
 18             {  
 19                 RETAILMSG(1,(L"[TCP Client]Dial new work failed!\r\n"));  
 20             }  
 21         }  
 22         else  
 23         {  
 24             RETAILMSG(1,(L"[TCP Client]Set RAS device config failed!\r\n"));  
 25         }  
 26     }  
 27     else  
 28     {  
 29         RETAILMSG(1,(L"[TCP Client]Create RAS link failed!\r\n"));  
 30     }  
 31 }  
 32   
 33 BOOL CNetworkDialDlg::PreTranslateMessage(MSG* pMsg)  
 34 {  
 35     switch(pMsg->message)  
 36     {  
 37     case WM_RASDIALEVENT:  
 38         OnRasDialEvent(pMsg->wParam,pMsg->lParam);  
 39         break;  
 40     case WM_USER + 0x1000:  
 41         if(1 == pMsg->wParam)  
 42         {  
 43             GetDlgItem(IDC_STATIC_PING_STATUS)->SetWindowText(L"PING 状态: 成功");  
 44         }  
 45         else  
 46         {  
 47             GetDlgItem(IDC_STATIC_PING_STATUS)->SetWindowText(L"PING 状态: 失败");  
 48         }  
 49         break;  
 50     default:  
 51         break;  
 52     }  
 53   
 54     return CDialog::PreTranslateMessage(pMsg);  
 55 }  
 56   
 57   
 58 /* 
 59 [TCP Ras Event]1.RasState: Open port 
 60 [TCP Ras Event]2.RasState: Port opened 
 61 [TCP Ras Event]3.RasState: Device Connected 
 62 [TCP Ras Event]4.RasState: All Device Connected 
 63 [TCP Ras Event]5.Authenticating 
 64 [TCP Ras Event]9.RAS Other RasState:12 
 65 [TCP Ras Event]6.Authenticated, continue login... 
 66 [TCP Ras Event]7.RASCS_Connected received 
 67 */  
 68 LRESULT CNetworkDialDlg::OnRasDialEvent(WPARAM wParam,LPARAM lParam)  
 69 {  
 70     RASCONNSTATE RasState = (RASCONNSTATE)wParam;  
 71     DWORD dwError = (DWORD)lParam;  
 72   
 73     gRasState = RasState;  
 74     switch(RasState)  
 75     {  
 76     case RASCS_OpenPort:  
 77         RETAILMSG(1,(L"[TCP Ras Event]1.RasState: Open port\r\n"));  
 78         break;  
 79   
 80     case RASCS_PortOpened:  // 端口已经打开  
 81         RETAILMSG(1,(L"[TCP Ras Event]2.RasState: Port opened\r\n"));  
 82         break;  
 83   
 84     case RASCS_DeviceConnected: // 正在连接  
 85         {  
 86             // dialDeviceConnected = TRUE;  
 87             RETAILMSG(1,(L"[TCP Ras Event]3.RasState: Device Connected\r\n"));  
 88         }  
 89         break;  
 90   
 91     case RASCS_AllDevicesConnected: //  
 92         // dialDeviceConnected = TRUE;  
 93         RETAILMSG(1,(L"[TCP Ras Event]4.RasState: All Device Connected\r\n"));  
 94         break;  
 95   
 96     case RASCS_Authenticate:  
 97         {  
 98             // dialDeviceConnected = TRUE;  
 99             RETAILMSG(1,(L"[TCP Ras Event]5.Authenticating\r\n"));  
100         }  
101         break;  
102   
103     case RASCS_Authenticated:  
104         {                 
105             // dialDeviceConnected = TRUE;  
106             RETAILMSG(1,(L"[TCP Ras Event]6.Authenticated, continue login...\r\n"));  
107         }  
108         break;  
109   
110     case RASCS_AuthNotify:  
111         {  
112             RETAILMSG(1,(L"[TCP Ras Event]RASCS_AuthNofify received,Error: %d\r\n",dwError));  
113             switch(dwError)  
114             {  
115             case ERROR_RESTRICTED_LOGON_HOURS:  
116             case ERROR_ACCT_DISABLED:  
117             case ERROR_PASSWD_EXPIRED:  
118             case ERROR_NO_DIALIN_PERMISSION:  
119             case ERROR_CHANGING_PASSWORD:  
120             default:  
121                 break;  
122             }  
123         }  
124         break;  
125   
126     case RASCS_Connected:  
127         {  
128             RETAILMSG(1,(L"[TCP Ras Event]7.RASCS_Connected received\r\n"));  
129             gbNetConnected = TRUE;  
130         }  
131         break;  
132   
133     case RASCS_Disconnected:  
134         {  
135             CString csError;  
136             RETAILMSG(1,(L"[TCP Ras Event]8.RASCS_Disconnected received. Error: %d\r\n",dwError));  
137               
138             csError.Format(L"建立网络出现错误:%d",dwError);  
139             GetDlgItem(IDC_BUTTON1)->EnableWindow(TRUE);  
140             m_NetName.EnableWindow(TRUE);  
141             giRasDialTimerCount = 0;  
142             GetDlgItem(IDC_STATIC_STATUS)->SetWindowText(csError);  
143             KillTimer(RASDIAL_NETSHOW_TIMER_ID);  
144         }  
145         break;  
146   
147     default:  
148         RETAILMSG(1,(L"[TCP Ras Event]9.RAS Other RasState:%d\r\n", RasState));  
149         break;  
150     }  
151   
152     return TRUE;  
153 }  

 

WinCE 下 RAS 拨号连接的建立和拨号过程

标签:

原文地址:http://www.cnblogs.com/91program/p/5206231.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!