Windows Mobile 下使用C#进行GPRS、CDMA开发(转)

有关GPRS、CDMA开发的文章网上已经有不少,但是由于Windows Mobile SDK提供的GPRS、CDMA连接操作的库只有C++版本的(即Connection Manager API),网上的文章大多数都是C++版本的,尽管也有C#编写的但是大多封装的有些不对并且没有经过很好的测试,本文在网络已有的资料上整理出如何用C#进行GPRS、CDMA开发。

参考文献:

http://www.vckbase.com/document/viewdoc/?id=1803

http://www.cnblogs.com/jsjkandy/archive/2008/08/06/1262445.html

http://blogs.msdn.com/anthonywong/archive/2006/03/13/550686.aspx

开发环境

Visual Studio 2005

Windows Mobile 6 SDK Professional

头文件封装

我们要用到Windows Mobile SDK的一个C++的头文件Connmgr.h,它的路径是:c:\program files\windows mobile 6 sdk\pocketpc\include\armv4i\connmgr.h,首先要用C#重新定义要用到的这个头文件的常量、结构和外部函数声明。

有关C#平台封送的更多说明可以参考MSDN:

http://msdn.microsoft.com/zh-cn/library/fzhhdwae(VS.80).aspx

常量声明:

[code=C#]
        const int S_OK = 0;
        const uint CONNMGR_PARAM_GUIDDESTNET = 0x1;
        const uint CONNMGR_PRIORITY_USERINTERACTIVE = 0x08000;
        const uint INFINITE = 0xffffffff;
        const uint CONNMGR_STATUS_CONNECTED = 0x10;
 
        const int CONNMGR_MAX_DESC = 128;           // @constdefine Max size of a network description
        const int CONNMGR_FLAG_PROXY_HTTP = 0x1;    // @constdefine HTTP Proxy supported
        const int CONNMGR_FLAG_PROXY_WAP = 0x2;     // @constdefine WAP Proxy (gateway) supported
        const int CONNMGR_FLAG_PROXY_SOCKS4 = 0x4;  // @constdefine SOCKS4 Proxy supported
        const int CONNMGR_FLAG_PROXY_SOCKS5 = 0x8;  // @constdefine SOCKS5 Proxy supported

        const UInt16 IDC_WAIT = 32514;
        const UInt16 IDC_ARROW = 32512;
[/code]

结构封送声明

C++原型:

[code=C++]
        typedef struct _CONNMGR_CONNECTIONINFO
        {
            DWORD cbSize;       // @field Size of this structure
            DWORD dwParams;     // @field Valid parms, set of CONNMGR_PARAM_*
            DWORD dwFlags;      // @field Connection flags, set of CONNMGR_FLAG_*
            DWORD dwPriority;   // @field Priority, one of CONNMGR_PRIORITY_*
            BOOL bExclusive;    // @field Connection is exclusive, see comments
            BOOL bDisabled;     // @field Don't actually connect
            GUID guidDestNet;   // @field GUID of network to connect to
            HWND hWnd;          // @field hWnd to post status change messages to
            UINT uMsg;          // @field Msg to use when posting status changes
            LPARAM lParam;      // @field lParam to use when posting status changes
            ULONG ulMaxCost;    // @field Max acceptable cost of connection
            ULONG ulMinRcvBw;   // @field Min acceptable receive bandwidth of connection
            ULONG ulMaxConnLatency; // @field Max acceptable connect latency
        } CONNMGR_CONNECTIONINFO;

        typedef struct _CONNMGR_DESTINATION_INFO
        {
            GUID guid;  // @field GUID associated with network
            TCHAR szDescription[CONNMGR_MAX_DESC];  // @field Description of network
            BOOL fSecure; // @field Is it OK to allow multi-homing on the network
        } CONNMGR_DESTINATION_INFO;

        typedef struct _GUID {          // size is 16
            DWORD Data1;
            WORD   Data2;
            WORD   Data3;
            BYTE  Data4[8];
        } GUID;
[/code]

C#声明

[code=C#]
        [StructLayout(LayoutKind.Sequential)]
        public struct CONNMGR_CONNECTIONINFO
        {
            public uint cbSize;
            public uint dwParams;
            public uint dwFlags;
            public uint dwPriority;
            public int bExclusive;
            public int bDisabled;
            public GUID guidDestNet;
            public IntPtr hWnd;
            public uint uMsg;
            public uint lParam;
            public uint ulMaxCost;
            public uint ulMinRcvBw;
            public uint ulMaxConnLatency;
        }

        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
        public struct CONNMGR_DESTINATION_INFO
        {
            public GUID guid;  // @field GUID associated with network
            [MarshalAs(UnmanagedType.ByValTStr,SizeConst = CONNMGR_MAX_DESC)]
            public string szDescription;  // @field Description of network
            public int fSecure; // @field Is it OK to allow multi-homing on the network
        }
 
        public struct GUID
        {          // size is 16
            public uint Data1;
            public UInt16 Data2;
            public UInt16 Data3;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
            public byte[] Data4;
        }
[/code]

 

函数封送声明

C++原型:

请参考connmgr.h

C#声明:

[code=C#]
        [DllImport("coredll.dll")]
        public static extern uint GetTickCount();

        [DllImport("coredll.dll")]
        public static extern uint WaitForSingleObject(IntPtr hHandle,uint dwMilliseconds);

        [DllImport("cellcore.dll")]
        public static extern int ConnMgrMapURL(string pwszURL, ref GUID pguid, ref uint pdwIndex);

        [DllImport("cellcore.dll")]
        public static extern int ConnMgrEstablishConnectionSync(ref CONNMGR_CONNECTIONINFO ci, ref IntPtr phConnection, uint dwTimeout, ref uint pdwStatus);

        [DllImport("cellcore.dll")]
        private static extern IntPtr ConnMgrApiReadyEvent();

        [DllImport("cellcore.dll")]
        public static extern int ConnMgrReleaseConnection(IntPtr hConnection, int bCache);

        [DllImport("cellcore.dll")]
        public static extern int ConnMgrEnumDestinations(int nIndex,ref CONNMGR_DESTINATION_INFO pDestInfo);

        [DllImport("cellcore.dll")]
        public static extern int ConnMgrConnectionStatus(IntPtr hConnection, ref uint pdwStatus );  

        [DllImport("coredll.dll")]
        private static extern int CloseHandle(IntPtr hObject);
[/code]  

这里参考http://www.vckbase.com/document/viewdoc/?id=1803例子用C#封装一个类库,并实现类似的Demo。运行界面如下图所示:                         

                                         7

在Demo中,增加一个建立GPRS和建立CDMA连接的演示,代码:

[code=C#]
        /// <summary>
        /// 连接到移动网络
        /// </summary>
        /// <returns></returns>
        private static bool ConnectMobileNetwork(string csDesc)
        {
            ConnectManager connectManager = new ConnectManager();
            List<ConnectManager.CONNMGR_DESTINATION_INFO> lst = connectManager.EnumNetIdentifier();
            int nIndex = 0;
        
            //选择连接
            for (; nIndex < lst.Count; nIndex++)
            {
                if (string.Compare(lst[nIndex].szDescription, csDesc, true) == 0)
                {
                    break;
                }
            }
        
            //建立连接
            if (nIndex >= 0 && nIndex < lst.Count)
            {
               return connectManager.EstablishConnection((uint)nIndex);
            }
            return false;
         }

         /// <summary>
         /// 建立GPRS连接
         /// </summary>
         /// <param name="sender"></param>
         /// <param name="e"></param>
         private void btnConnectGPRS_Click(object sender, EventArgs e)
         {
            if (!ConnectMobileNetwork("Internet 设置"))
            {
                MessageBox.Show("未能建立GPRS连接!");
                return;
            }
            else
            {
                MessageBox.Show("成功建立GPRS连接!");
            }
         }        /// <summary>
         
         /// 建立CDMA连接
         /// </summary>
         /// <param name="sender"></param>
         /// <param name="e"></param>
         private void btnConnectCDMA_Click(object sender, EventArgs e)
         {
            if (!ConnectMobileNetwork("联通wap"))
            {
                MessageBox.Show("未能建立CDMA连接!");
                return;
            }
            else
            {
                MessageBox.Show("成功建立CDMA连接!");
            }
         } 
[/code]

 手机设置

      在这里给出GPRS连接的设置说明,大多数的手机采用默认设置就可以直接使用了,并不用自己设置,但是如果你发现上面代码运行后不能建立GPRS连接,测可以尝试下面的设置。

     在上面的GPRS连接代码中,我们使用了“Internet 设置”这个设置,如果手机还没有建立这个连接设置或者已经建立设置但是连接总是失败,则可以按下面的方式建立或修改连接设置(这里使用Windows Mobile 5操作系统,动感地带SIM卡,用模拟器截图):

     1.打开“开始->设置”,点击“连接”如图所示。

                                      1

      2.如果您的手机已经有Internet设置,则在图2中会有“管理现有连接”的链接,点击它可以进行修改设置,在这里点击“添加新调制解调器连接”,增加一个Internet 设置。

                                      2

    3.在弹出的如图3所示的对话框中,选择调制解调器为“电话线路(GPRS)”。

                                      3

     4.在弹出如图4所示的对话框中,“访问点名称”输入“cmnet”。

                                      4

     5.在弹出的如图5所示的对话框中,直接点击“完成”。对于专用网络则要输入移动提供的用户名和密码。

                                      5

     6.点击“完成”后在“Internet 设置”里多了一个“GRPS连接互联网”的设置,如果此处有几个设置,请确保您要使用的设置处于选中状态。点击“OK”退出设置。此时您可以使用本问提供的实例程序来尝试连接,在连接的时候手机不要和电脑处于同步状态,否则手机可能不会进行拨号。

                                      6

      从上面的使用我们可以知道,建立GPRS连接和建立CDMA连接的差别仅仅是连接设置的不同而已,也就是拨号的问题。但是网络上很多人问GPRS开发和CDMA开发问题,这里通过这个Demo,希望可以给学习移动开发的朋友们提供一些帮助。

源码下载:http://download.csdn.net/source/1047113

posted @ 2009-04-11 09:05  木瓜脑袋  阅读(7352)  评论(8编辑  收藏  举报