Windows Mobile 下使用C#進行GPRS、CDMA開發)

來源:互聯網
上載者:User

有關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。運行介面如所示:                         

                                        

在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.開啟“開始->設定”,點擊“串連”。

                                     

      2.如果您的手機已經有Internet設定,則在圖2中會有“管理現有串連”的連結,點擊它可以進行修改設定,在這裡點擊“添加新數據機串連”,增加一個Internet 設定。

                                     

    3.在彈出的3所示的對話方塊中,選擇數據機為“電話線路(GPRS)”。

                                     

     4.在彈出4所示的對話方塊中,“訪問點名稱”輸入“cmnet”。

                                     

     5.在彈出的5所示的對話方塊中,直接點擊“完成”。對於專用網路則要輸入移動提供的使用者名稱和密碼。

                                     

     6.點擊“完成”後在“Internet 設定”裡多了一個“GRPS串連互連網”的設定,如果此處有幾個設定,請確保您要使用的設定處於選中狀態。點擊“OK”退出設定。此時您可以使用本問提供的執行個體程式來嘗試串連,在串連的時候手機不要和電腦處於同步狀態,否則手機可能不會進行撥號。

                                     

      從上面的使用我們可以知道,建立GPRS串連和建立CDMA串連的差別僅僅是串連設定的不同而已,也就是撥號的問題。但是網路上很多人問GPRS開發和CDMA開發問題,這裡通過這個Demo,希望可以給學習移動開發的朋友們提供一些協助。

源碼下載:http://download.csdn.net/source/1047113

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.