WinCe系统3G和RAS拨号 - 基于 .NET Compact Framework和C++

 

在.NET Compact Framework下开发3G应用可以有两个options,1:AT command, 2:RAS。其实RAS最终也是通过AT command来管理3G设备,RAS屏蔽了技术细节,很方便地建立TCP/IP based 的网络连接,所以推荐使用RAS,WinCE本身提供了native API可以直接操作RAS,管理3G设备及其连接。与此同时RAS也支持有线电话拨号,GPRS等应用。

RAS简介


RAS的详细介绍见 Overview of RAS

由于.NET Compact Framework 不直接提供RAS组件,因此只能通过native invoke来使用RAS。

RAS的native调用函数,事件及结构见 RAS Reference


关键的RAS native调用
 

        [DllImport("coredll.dll", CharSet = CharSet.Auto)]
        private static extern Int32 RasGetEntryDialParams(string lpszPhoneBook,
        byte[] lpRasDialParams,
        out UInt32 lpfPassword);

        [DllImport("coredll.dll", CharSet = CharSet.Auto)]
        private static extern int RasDial(IntPtr pDialExtensions,
                                                   string pPhonebook,
                          byte[] pRasDialParam,
                          uint pNotifierType,
                          IntPtr pHwnd,
                     ref int pRasConn);

        [DllImport("coredll.dll", CharSet = CharSet.Auto)]
        private static extern int RasHangUp(int pSession);

        [DllImport("coredll.dll", CharSet = CharSet.Auto)]
        private static extern uint RasGetConnectStatus(int pSession,
                                                                        byte[] lpRasConnStatus);

RasGetEntryDialParams 取得原先配置的连接项,可以在手工新建连接或者修改注册表建立连接项(Connection Entry)。这个函数的关键作用是从连接项取出配置好的拨号密码。
RasDial 拨号到电信运营商的网络上。
RasHangUp 挂断连接。
RasGetConnectStatus 取当前的连接状态。

 

RAS 事件
在调用RasDial 时,如果uint pNotifierType传递0xFFFFFFFF,那么IntPtr pHwnd可以注册一个事件接受的Handler。当连接状态发生改变时,windows会把相应的事件发送给这个handler,这样我们可以接收这些事件,把之变成delegate来通知clients。这样就实现了一个Observer模式,client可以通过订阅来实时得到连接状态的变更状况。

要把windows event变成C#的delegate,要继承MessageWindow class,然后override  WndProc(ref Message pMsg),同时定义C#的delegate和event把windows事件变成delegate提供给client订阅。

 

public class RasManager : MessageWindow
    {
        public delegate void NotifyEvent();
        public event NotifyEvent OnConnectedEvent;
        public event NotifyEvent OnDisconnectedEvent;

        protected override void WndProc(ref Message pMsg)
        {
            if (pMsg.Msg == WM_RASDIALEVENT)
            {
                switch ((int)pMsg.WParam)
                {
                    case (int)RASCONNSTATE.RASCS_OpenPort:
                        {
                            break;
                        }
                    case (int)RASCONNSTATE.RASCS_PortOpened:
                        {
                            break;
                        }
                    case (int)RASCONNSTATE.RASCS_ConnectDevice:
                        {
                            break;
                        }
                    case (int)RASCONNSTATE.RASCS_DeviceConnected:
                        {
                            break;
                        }
                    case (int)RASCONNSTATE.RASCS_AllDevicesConnected:
                        {
                            break;
                        }
                    case (int)RASCONNSTATE.RASCS_Authenticate:
                        {
                            break;
                        }
                    case (int)RASCONNSTATE.RASCS_AuthNotify:
                        {
                            break;
                        }
                    case (int)RASCONNSTATE.RASCS_AuthRetry:
                        {
                            break;
                        }
                    case (int)RASCONNSTATE.RASCS_AuthCallback:
                        {
                            break;
                        }
                    case (int)RASCONNSTATE.RASCS_AuthChangePassword:
                        {
                            break;
                        }
                    case (int)RASCONNSTATE.RASCS_AuthProject:
                        {
                            break;
                        }
                    case (int)RASCONNSTATE.RASCS_AuthLinkSpeed:
                        {
                            break;
                        }
                    case (int)RASCONNSTATE.RASCS_AuthAck:
                        {
                            break;
                        }
                    case (int)RASCONNSTATE.RASCS_ReAuthenticate:
                        {
                            break;
                        }
                    case (int)RASCONNSTATE.RASCS_Authenticated:
                        {
                            break;
                        }
                    case (int)RASCONNSTATE.RASCS_PrepareForCallback:
                        {
                            break;
                        }
                    case (int)RASCONNSTATE.RASCS_WaitForModemReset:
                        {
                            break;
                        }
                    case (int)RASCONNSTATE.RASCS_WaitForCallback:
                        {
                            break;
                        }
                    case (int)RASCONNSTATE.RASCS_Projected:
                        {
                            break;
                        }
                    case (int)RASCONNSTATE.RASCS_Interactive:
                        {
                            break;
                        }
                    case (int)RASCONNSTATE.RASCS_RetryAuthentication:
                        {
                            break;
                        }
                    case (int)RASCONNSTATE.RASCS_CallbackSetByCaller:
                        {
                            break;
                        }
                    case (int)RASCONNSTATE.RASCS_PasswordExpired:
                        {
                            break;
                        }
                    case (int)RASCONNSTATE.RASCS_Connected:
                        {
                            OnConnected();
                            break;
                        }
                    case (int)RASCONNSTATE.RASCS_Disconnected:
                        {
                            OnDisconnected();
                            break;
                        }
                    default:
                        {
                            break;
                        }
                }
            }
            base.WndProc(ref pMsg);
        }
RASCS_Disconnected windows 事件转换成OnConnected C# event, 而RASCS_Disconnected windows 事件转换成OnDisconnectedC# event。

 

Client 的调用
事件注册可以放在Client 的Constructor上执行
RasManager.Instance.OnConnectedEvent +=
new RasManager.NotifyEvent(Form1_OnConnectedEvent);
RasManager.Instance.OnDisconnectedEvent +=
new RasManager.NotifyEvent(Form1_OnDisconnectedEvent);
 

连接3G网络
RasManager.Instance.DialUp("3G Connection", null, null);
 

断开连接
RasManager.Instance.HangUp();
 

查询连接状态
 

RasManager.RASCONNSTATE rasConnState =
(RasManager.RASCONNSTATE) RasManager.Instance.GetStatus();

 

这个类同样可以使用于GPRS里面。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值