Windows mobile 开发入门—网络程序的开发 [ZT]

这里讲的网络程序的开发,主要是在程序中连接网络,在CF中UDP与TCP与Windows 应用程序相差不多.

设置 

     mobile要联接网络,先觉条件是要有一个拔号设置,移动拔号设置分为cmwap,cmnet.这个设置非常的重要,cmwap只支持http而不支持tcp和udp,因为这个原因我曾经就花费过许多时间.

开始-->设置-->连接(选项卡)—>连接(按钮)—>高级-->选择网络 在这里你可以选择一个已经有的网络,或者新建一个网络.

mobile-rm-30001

 

新建连接

1点击新建

2在常规选项卡里输入连接的名字

3在调制解调器选项卡点击新建,弹出新建对话框

4输入新建连接名字,选择电话线路(gprs),点击下一步

5在访问点的名字输入cmwap,或者cmnet 选择下一步

6用户名密码不用填 ,完成连接

在使用时只要选择即可。

具体的设置可以打10086,或者其它运营商咨询。

 

程序拔号

如果webservice,程序会在没有连接网络时自动连接。但在开发UDP、TCP网络程序时,经常要考虑到是否连接到网络,如果没有则需要程序拔号。现在已经有开源代码实现这个功能。主要有两个类ConnectManager.cs,GPRSManage.cs 这两个人实现方式差不多,都是调用系统方法。

在使用的时候这样调用就可以了。

ContractedBlock.gif ExpandedBlockStart.gif Code
  1None.gifusing System;
  2None.gifusing System.Collections.Generic;
  3None.gifusing System.Text;
  4None.gifusing System.Runtime.InteropServices;
  5None.gifusing System.Threading;
  6None.gifusing System.Collections;
  7None.gif
  8None.gifnamespace Connection
  9ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 10InBlock.gif    public class ConnectManager
 11ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 12InBlock.gif        const int S_OK = 0;
 13InBlock.gif        const uint CONNMGR_PARAM_GUIDDESTNET = 0x1;
 14InBlock.gif        const uint CONNMGR_PRIORITY_USERINTERACTIVE = 0x08000;
 15InBlock.gif        const uint INFINITE = 0xffffffff;
 16InBlock.gif        const uint CONNMGR_STATUS_CONNECTED = 0x10;
 17InBlock.gif        const int CONNMGR_MAX_DESC = 128;    // @constdefine Max size of a network description
 18InBlock.gif
 19InBlock.gif        const int CONNMGR_FLAG_PROXY_HTTP = 0x1// @constdefine HTTP Proxy supported
 20InBlock.gif        const int CONNMGR_FLAG_PROXY_WAP = 0x2// @constdefine WAP Proxy (gateway) supported
 21InBlock.gif        const int CONNMGR_FLAG_PROXY_SOCKS4 = 0x4// @constdefine SOCKS4 Proxy supported
 22InBlock.gif        const int CONNMGR_FLAG_PROXY_SOCKS5 = 0x8// @constdefine SOCKS5 Proxy supported
 23InBlock.gif
 24InBlock.gif        const UInt16 IDC_WAIT = 32514;
 25InBlock.gif        const UInt16 IDC_ARROW = 32512;
 26InBlock.gif
 27InBlock.gif        private IntPtr m_hConnection = IntPtr.Zero;
 28InBlock.gif
 29InBlock.gif        public ConnectManager()
 30ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 31ExpandedSubBlockEnd.gif        }

 32InBlock.gif
 33InBlock.gif        ~ConnectManager()
 34ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 35InBlock.gif            ReleaseConnection();
 36ExpandedSubBlockEnd.gif        }

 37InBlock.gif
 38ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 39InBlock.gif        /// 查看连接是否可用
 40InBlock.gif        /// </summary>
 41ExpandedSubBlockEnd.gif        /// <returns></returns> 

 42InBlock.gif        public bool GetConnMgrAvailable()
 43ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 44InBlock.gif            IntPtr hConnMgr = ConnMgrApiReadyEvent();
 45InBlock.gif            bool bAvailbale = false;
 46InBlock.gif            uint dwResult = WaitForSingleObject ( hConnMgr, 2000 );
 47InBlock.gif
 48InBlock.gif            if (dwResult == 0)
 49ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 50InBlock.gif                bAvailbale = true;
 51ExpandedSubBlockEnd.gif            }

 52InBlock.gif
 53InBlock.gif            // 关闭
 54InBlock.gif            if (hConnMgr.ToInt32() != 0) CloseHandle(hConnMgr);
 55InBlock.gif
 56InBlock.gif            return bAvailbale;
 57ExpandedSubBlockEnd.gif        }

 58ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 59InBlock.gif        /// 映射URL
 60InBlock.gif        /// </summary>
 61InBlock.gif        /// <param name="lpszURL"></param>
 62InBlock.gif        /// <param name="guidNetworkObject"></param>
 63InBlock.gif        /// <param name="pcsDesc"></param>
 64ExpandedSubBlockEnd.gif        /// <returns></returns> 

 65InBlock.gif        public int MapURLAndGUID(string lpszURL, ref GUID guidNetworkObject, ref string pcsDesc)
 66ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 67InBlock.gif            if (lpszURL == null || lpszURL.Length < 1)
 68InBlock.gif                return 0;
 69InBlock.gif
 70InBlock.gif            uint nIndex = 0;
 71InBlock.gif            int hResult = ConnMgrMapURL(lpszURL,ref guidNetworkObject, ref nIndex);
 72InBlock.gif            if (hResult < 0)
 73ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 74InBlock.gif                throw new Exception("Could not map a request to a network identifier");
 75ExpandedSubBlockEnd.gif            }

 76InBlock.gif            else
 77ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 78InBlock.gif                if (pcsDesc != null)
 79ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 80InBlock.gif                    CONNMGR_DESTINATION_INFO DestInfo = new CONNMGR_DESTINATION_INFO();
 81InBlock.gif                    if (ConnMgrEnumDestinations((int)nIndex, ref DestInfo) >= 0)
 82ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
 83InBlock.gif                        pcsDesc = DestInfo.szDescription;
 84ExpandedSubBlockEnd.gif                    }

 85ExpandedSubBlockEnd.gif                }

 86ExpandedSubBlockEnd.gif            }

 87InBlock.gif
 88InBlock.gif            return (int)nIndex;
 89ExpandedSubBlockEnd.gif        }

 90ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 91InBlock.gif        /// 枚举网络标识符信息
 92InBlock.gif        /// </summary>
 93ExpandedSubBlockEnd.gif        /// <param name="lstNetIdentifiers"></param> 

 94InBlock.gif        public List<CONNMGR_DESTINATION_INFO> EnumNetIdentifier()
 95ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 96InBlock.gif            List<CONNMGR_DESTINATION_INFO> lstNetIdentifiers = new List<CONNMGR_DESTINATION_INFO>();
 97InBlock.gif            // 得到网络列表
 98InBlock.gif            for (uint dwEnumIndex = 0; ; dwEnumIndex++)
 99ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
100InBlock.gif                CONNMGR_DESTINATION_INFO networkDestInfo = new CONNMGR_DESTINATION_INFO();
101InBlock.gif                
102InBlock.gif                if (ConnMgrEnumDestinations((int)dwEnumIndex,ref networkDestInfo) != 0)
103ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
104InBlock.gif                    break;
105ExpandedSubBlockEnd.gif                }

106InBlock.gif                lstNetIdentifiers.Add(networkDestInfo);
107ExpandedSubBlockEnd.gif            }

108InBlock.gif
109InBlock.gif            return lstNetIdentifiers;
110ExpandedSubBlockEnd.gif        }

111InBlock.gif        
112ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
113InBlock.gif        /// 建立连接
114InBlock.gif        /// </summary>
115InBlock.gif        /// <param name="nIndex"></param>
116ExpandedSubBlockEnd.gif        /// <returns></returns> 

117InBlock.gif        public bool EstablishConnection(uint nIndex, uint dwStatus)
118ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
119InBlock.gif            ReleaseConnection();
120InBlock.gif            // 得到正确的连接信息
121InBlock.gif            CONNMGR_DESTINATION_INFO DestInfo = new CONNMGR_DESTINATION_INFO();
122InBlock.gif            int hResult = ConnMgrEnumDestinations((int)nIndex, ref DestInfo);
123InBlock.gif
124InBlock.gif            if (hResult >= 0)
125ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
126InBlock.gif                // 初始化连接结构
127InBlock.gif                CONNMGR_CONNECTIONINFO ConnInfo = new CONNMGR_CONNECTIONINFO();
128InBlock.gif
129InBlock.gif                ConnInfo.cbSize = (uint)Marshal.SizeOf(ConnInfo);
130InBlock.gif                ConnInfo.dwParams = CONNMGR_PARAM_GUIDDESTNET;
131InBlock.gif                ConnInfo.dwFlags = CONNMGR_FLAG_PROXY_HTTP | CONNMGR_FLAG_PROXY_WAP | CONNMGR_FLAG_PROXY_SOCKS4 | CONNMGR_FLAG_PROXY_SOCKS5;
132InBlock.gif                ConnInfo.dwPriority = CONNMGR_PRIORITY_USERINTERACTIVE;
133InBlock.gif                ConnInfo.guidDestNet = DestInfo.guid;
134InBlock.gif                ConnInfo.bExclusive = 0;
135InBlock.gif                ConnInfo.bDisabled = 0;
136InBlock.gif
137InBlock.gif                //uint dwStatus = 0;
138InBlock.gif                hResult = ConnMgrEstablishConnectionSync(ref ConnInfo, ref m_hConnection, 15 * 1000ref dwStatus);
139InBlock.gif                if (hResult < 0)
140ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
141InBlock.gif                    return false;
142ExpandedSubBlockEnd.gif                }

143InBlock.gif                else
144ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
145InBlock.gif                    return true;
146ExpandedSubBlockEnd.gif                }

147ExpandedSubBlockEnd.gif            }

148InBlock.gif
149InBlock.gif            return false;
150ExpandedSubBlockEnd.gif        }

151InBlock.gif
152ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
153InBlock.gif        /// 建立连接
154InBlock.gif        /// </summary>
155InBlock.gif        /// <param name="nIndex"></param>
156ExpandedSubBlockEnd.gif        /// <returns></returns>

157InBlock.gif        public bool EstablishConnection(GUID guid, uint dwStatus)
158ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
159InBlock.gif            ReleaseConnection();
160InBlock.gif            // 得到正确的连接信息
161InBlock.gif           
162InBlock.gif                // 初始化连接结构
163InBlock.gif                CONNMGR_CONNECTIONINFO ConnInfo = new CONNMGR_CONNECTIONINFO();
164InBlock.gif
165InBlock.gif                ConnInfo.cbSize = (uint)Marshal.SizeOf(ConnInfo);
166InBlock.gif                ConnInfo.dwParams = CONNMGR_PARAM_GUIDDESTNET;
167InBlock.gif                ConnInfo.dwFlags = CONNMGR_FLAG_PROXY_HTTP | CONNMGR_FLAG_PROXY_WAP | CONNMGR_FLAG_PROXY_SOCKS4 | CONNMGR_FLAG_PROXY_SOCKS5;
168InBlock.gif                ConnInfo.dwPriority = CONNMGR_PRIORITY_USERINTERACTIVE;
169InBlock.gif                ConnInfo.guidDestNet = guid;
170InBlock.gif                ConnInfo.bExclusive = 0;
171InBlock.gif                ConnInfo.bDisabled = 0;
172InBlock.gif
173InBlock.gif                //uint dwStatus = 0;
174InBlock.gif                int hResult = ConnMgrEstablishConnectionSync(ref ConnInfo, ref m_hConnection, 15 * 1000ref dwStatus);
175InBlock.gif                if (hResult < 0)
176ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
177InBlock.gif                    return false;
178ExpandedSubBlockEnd.gif                }

179InBlock.gif                else
180ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
181InBlock.gif                    return true;
182ExpandedSubBlockEnd.gif                }
         
183InBlock.gif
184InBlock.gif           
185ExpandedSubBlockEnd.gif        }

186InBlock.gif
187ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
188InBlock.gif        /// 连接状态
189InBlock.gif        /// </summary>
190InBlock.gif        /// <param name="nTimeoutSec"></param>
191InBlock.gif        /// <param name="pdwStatus"></param>
192ExpandedSubBlockEnd.gif        /// <returns></returns>

193InBlock.gif        public bool WaitForConnected(int nTimeoutSec, ref uint pdwStatus)
194ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
195InBlock.gif            uint dwStartTime = GetTickCount();
196InBlock.gif            bool bRet = false;
197InBlock.gif
198InBlock.gif            while (GetTickCount() - dwStartTime < (uint)nTimeoutSec * 1000)
199ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
200InBlock.gif                if (m_hConnection.ToInt32() != 0)
201ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
202InBlock.gif                    uint dwStatus = 0;
203InBlock.gif                    int hr = ConnMgrConnectionStatus(m_hConnection, ref dwStatus);
204InBlock.gif                    if (dwStatus != 0) pdwStatus = dwStatus;
205InBlock.gif                    if (hr >= 0)
206ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
207InBlock.gif                        if (dwStatus == CONNMGR_STATUS_CONNECTED)
208ExpandedSubBlockStart.gifContractedSubBlock.gif                        dot.gif{
209InBlock.gif                            bRet = true;
210InBlock.gif                            break;
211ExpandedSubBlockEnd.gif                        }

212ExpandedSubBlockEnd.gif                    }

213ExpandedSubBlockEnd.gif                }

214InBlock.gif                Thread.Sleep(100);
215ExpandedSubBlockEnd.gif            }

216InBlock.gif
217InBlock.gif            return bRet;
218ExpandedSubBlockEnd.gif        }

219InBlock.gif
220ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
221InBlock.gif        /// 释放所有连接
222ExpandedSubBlockEnd.gif        /// </summary>

223InBlock.gif        public void ReleaseConnection()
224ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
225InBlock.gif
226InBlock.gif            if (m_hConnection.ToInt32() != 0)
227ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
228InBlock.gif                ConnMgrReleaseConnection(m_hConnection, 0);
229InBlock.gif                m_hConnection = IntPtr.Zero;
230ExpandedSubBlockEnd.gif            }

231ExpandedSubBlockEnd.gif        }

232InBlock.gif
233InBlock.gif        [StructLayout(LayoutKind.Sequential)]
234InBlock.gif        public struct CONNMGR_CONNECTIONINFO
235ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
236InBlock.gif            public uint cbSize;
237InBlock.gif            public uint dwParams;
238InBlock.gif            public uint dwFlags;
239InBlock.gif            public uint dwPriority;
240InBlock.gif            public int bExclusive;
241InBlock.gif            public int bDisabled;
242InBlock.gif            public GUID guidDestNet;
243InBlock.gif            public IntPtr hWnd;
244InBlock.gif            public uint uMsg;
245InBlock.gif            public uint lParam;
246InBlock.gif            public uint ulMaxCost;
247InBlock.gif            public uint ulMinRcvBw;
248InBlock.gif            public uint ulMaxConnLatency;
249ExpandedSubBlockEnd.gif        }

250InBlock.gif
251InBlock.gif        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
252InBlock.gif        public struct CONNMGR_DESTINATION_INFO
253ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
254InBlock.gif            public GUID guid;  // @field GUID associated with network
255InBlock.gif            [MarshalAs(UnmanagedType.ByValTStr,SizeConst = CONNMGR_MAX_DESC)]
256InBlock.gif            public string szDescription;  // @field Description of network
257InBlock.gif            public int fSecure; // @field Is it OK to allow multi-homing on the network
258ExpandedSubBlockEnd.gif        }

259InBlock.gif
260InBlock.gif        public struct GUID
261ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{          // size is 16
262InBlock.gif            public uint Data1;
263InBlock.gif            public UInt16 Data2;
264InBlock.gif            public UInt16 Data3;
265InBlock.gif            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
266InBlock.gif            public byte[] Data4;
267ExpandedSubBlockEnd.gif        }

268InBlock.gif
269InBlock.gif        [DllImport("coredll.dll")]
270InBlock.gif        public static extern uint GetTickCount();
271InBlock.gif
272InBlock.gif        [DllImport("coredll.dll")]
273InBlock.gif        public static extern uint WaitForSingleObject(IntPtr hHandle,uint dwMilliseconds);
274InBlock.gif
275InBlock.gif        [DllImport("cellcore.dll")]
276InBlock.gif        public static extern int ConnMgrMapURL(string pwszURL, ref GUID pguid, ref uint pdwIndex);
277InBlock.gif
278InBlock.gif        [DllImport("cellcore.dll", CharSet = CharSet.Unicode, SetLastError = true)]
279InBlock.gif        unsafe static extern int ConnMgrMapURL(string url, ref Guid guid, int* nIndex);
280InBlock.gif
281InBlock.gif        [DllImport("cellcore.dll")]
282InBlock.gif        public static extern int ConnMgrEstablishConnectionSync(ref CONNMGR_CONNECTIONINFO ci, ref IntPtr phConnection, uint dwTimeout, ref uint pdwStatus);
283InBlock.gif
284InBlock.gif        [DllImport("cellcore.dll")]
285InBlock.gif        private static extern IntPtr ConnMgrApiReadyEvent();
286InBlock.gif
287InBlock.gif        [DllImport("cellcore.dll")]
288InBlock.gif        public static extern int ConnMgrReleaseConnection(IntPtr hConnection, int bCache);
289InBlock.gif
290InBlock.gif        [DllImport("cellcore.dll")]
291InBlock.gif        public static extern int ConnMgrEnumDestinations(int nIndex,ref CONNMGR_DESTINATION_INFO pDestInfo);
292InBlock.gif
293InBlock.gif        [DllImport("cellcore.dll")]
294InBlock.gif        public static extern int ConnMgrConnectionStatus(IntPtr hConnection,    // @parm Handle to connection, returned from ConnMgrEstablishConnection
295InBlock.gif            ref uint pdwStatus       // @parm Returns current connection status, one of CONNMGR_STATUS_*
296InBlock.gif            );
297InBlock.gif
298InBlock.gif        [DllImport("coredll.dll")]
299InBlock.gif        private static extern int CloseHandle(IntPtr hObject);
300ExpandedSubBlockEnd.gif    }

301ExpandedBlockEnd.gif}

302None.gif

 

ContractedBlock.gif ExpandedBlockStart.gif Code
  1None.gifusing System;
  2None.gif//using System.Collections.Generic;
  3None.gif//using System.ComponentModel;
  4None.gif//using System.Data;
  5None.gifusing System.Runtime.InteropServices;
  6None.gifusing System.Collections;
  7None.gif//using System.Net.Sockets;
  8None.gif//using System.Windows.Forms;
  9None.gif
 10None.gifnamespace GPRSmanage
 11ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 12InBlock.gif
 13ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 14InBlock.gif    /// GPRS连接类
 15ExpandedSubBlockEnd.gif    /// </summary>

 16InBlock.gif    public class GPRSmanage
 17ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 18InBlock.gif        const int CONNMGR_PARAM_GUIDDESTNET = 0x1// guidDestNet -  
 19InBlock.gif        const int CONNMGR_PARAM_MAXCOST = 0x2// ulMaxCost -  
 20InBlock.gif        const int CONNMGR_PARAM_MINRCVBW = 0x4// ulMinRcvBw -  
 21InBlock.gif        const int CONNMGR_PARAM_MAXCONNLATENCY = 0x8;
 22InBlock.gif        const int CONNMGR_FLAG_PROXY_HTTP = 0x1// HTTP Proxy
 23InBlock.gif        const int CONNMGR_FLAG_PROXY_WAP = 0x2// WAP Proxy (gateway)
 24InBlock.gif        const int CONNMGR_FLAG_PROXY_SOCKS4 = 0x4// SOCKS4 Proxy
 25InBlock.gif        const int CONNMGR_FLAG_PROXY_SOCKS5 = 0x8// SOCKS5 Proxy
 26InBlock.gif        const int CONNMGR_PRIORITY_VOICE = 0x20000//  
 27InBlock.gif        const int CONNMGR_PRIORITY_USERINTERACTIVE = 0x08000//  
 28InBlock.gif        const int CONNMGR_PRIORITY_USERBACKGROUND = 0x02000//
 29InBlock.gif        const int CONNMGR_PRIORITY_USERIDLE = 0x00800//
 30InBlock.gif        const int CONNMGR_PRIORITY_HIPRIBKGND = 0x00200//
 31InBlock.gif        const int CONNMGR_PRIORITY_IDLEBKGND = 0x00080//
 32InBlock.gif        const int CONNMGR_PRIORITY_EXTERNALINTERACTIVE = 0x00020//
 33InBlock.gif        const int CONNMGR_PRIORITY_LOWBKGND = 0x00008//
 34InBlock.gif        const int CONNMGR_PRIORITY_CACHED = 0x00002//
 35InBlock.gif
 36InBlock.gif
 37InBlock.gif        const int CONNMGR_STATUS_UNKNOWN = 0x00//  
 38InBlock.gif        const int CONNMGR_STATUS_CONNECTED = 0x10// 
 39InBlock.gif        const int CONNMGR_STATUS_DISCONNECTED = 0x20// 
 40InBlock.gif        const int CONNMGR_STATUS_CONNECTIONFAILED = 0x21// 
 41InBlock.gif        const int CONNMGR_STATUS_CONNECTIONCANCELED = 0x22//  
 42InBlock.gif        const int CONNMGR_STATUS_CONNECTIONDISABLED = 0x23//  
 43InBlock.gif        const int CONNMGR_STATUS_NOPATHTODESTINATION = 0x24//  
 44InBlock.gif        const int CONNMGR_STATUS_WAITINGFORPATH = 0x25//  
 45InBlock.gif        const int CONNMGR_STATUS_WAITINGFORPHONE = 0x26//  
 46InBlock.gif        const int CONNMGR_STATUS_WAITINGCONNECTION = 0x40//  
 47InBlock.gif        const int CONNMGR_STATUS_WAITINGFORRESOURCE = 0x41//  
 48InBlock.gif        const int CONNMGR_STATUS_WAITINGFORNETWORK = 0x42// 
 49InBlock.gif        const int CONNMGR_STATUS_WAITINGDISCONNECTION = 0x80//  
 50InBlock.gif        const int CONNMGR_STATUS_WAITINGCONNECTIONABORT = 0x81// 
 51InBlock.gif       
 52InBlock.gif
 53InBlock.gif        unsafe class CONNMGR_CONNECTIONINFO
 54ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 55InBlock.gif            public int cbSize = 0x40;
 56InBlock.gif            public int dwParams = 0;
 57InBlock.gif            public int dwFlags = 0;
 58InBlock.gif            public int dwPriority = 0;
 59InBlock.gif            public int bExclusive = 0;
 60InBlock.gif            public int bDisabled = 0;
 61InBlock.gif            public Guid guidDestNet = Guid.Empty;
 62InBlock.gif            public IntPtr hWnd = IntPtr.Zero;
 63InBlock.gif            public int uMsg = 0// ID Message  
 64InBlock.gif            public int lParam = 0// lParam  
 65InBlock.gif            public int ulMaxCost = 0;
 66InBlock.gif            public int ulMinRcvBw = 0;
 67InBlock.gif            public int ulMaxConnLatency = 0;
 68ExpandedSubBlockEnd.gif        }

 69InBlock.gif
 70InBlock.gif        unsafe class CONNMGR_DESTINATION_INFO
 71ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 72InBlock.gif            public Guid guid = Guid.Empty;
 73InBlock.gif            public char* szDescription = null;
 74ExpandedSubBlockEnd.gif        }

 75InBlock.gif
 76InBlock.gif        [DllImport("cellcore.dll", CharSet = CharSet.Unicode, SetLastError = true)]
 77InBlock.gif        unsafe static extern int ConnMgrEnumDestinations(int nIndex, CONNMGR_DESTINATION_INFO destinationInfo);
 78InBlock.gif
 79InBlock.gif        [DllImport("cellcore.dll", CharSet = CharSet.Unicode, SetLastError = true)]
 80InBlock.gif        unsafe static extern int ConnMgrMapURL(string url, ref Guid guid, int* nIndex);
 81InBlock.gif
 82InBlock.gif        [DllImport("cellcore.dll", CharSet = CharSet.Unicode, SetLastError = true)]
 83InBlock.gif        unsafe static extern int ConnMgrEstablishConnection(CONNMGR_CONNECTIONINFO pConnInfo, out IntPtr hConnection);
 84InBlock.gif
 85InBlock.gif        [DllImport("cellcore.dll", CharSet = CharSet.Unicode, SetLastError = true)]
 86InBlock.gif        unsafe static extern int ConnMgrEstablishConnectionSync(CONNMGR_CONNECTIONINFO pConnInfo, out IntPtr hConn, uint timeout, uint* status);
 87InBlock.gif
 88InBlock.gif        [DllImport("cellcore.dll", CharSet = CharSet.Unicode, SetLastError = true)]
 89InBlock.gif        unsafe static extern int ConnMgrConnectionStatus(IntPtr hConnection, uint* status);
 90InBlock.gif
 91InBlock.gif        [DllImport("cellcore.dll", CharSet = CharSet.Unicode, SetLastError = true)]
 92InBlock.gif        unsafe static extern int ConnMgrReleaseConnection(IntPtr hConnection, int bCache);
 93InBlock.gif
 94InBlock.gif        [DllImport("coredll.dll")]
 95InBlock.gif        private static extern int CloseHandle(IntPtr hObject);
 96InBlock.gif
 97InBlock.gif
 98InBlock.gif        public static Hashtable ht = new Hashtable();
 99InBlock.gif        public IntPtr hConnect;
100InBlock.gif        public static unsafe Guid GuidConnection(int nIndex)
101ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
102InBlock.gif            Guid guid = Guid.Empty;
103InBlock.gif            CONNMGR_DESTINATION_INFO connMgrDestinationInfo = new CONNMGR_DESTINATION_INFO();
104InBlock.gif            fixed (char* pDescription = new char[129])
105ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
106InBlock.gif                connMgrDestinationInfo.szDescription = pDescription;
107InBlock.gif                if (ConnMgrEnumDestinations(nIndex, connMgrDestinationInfo) == 0)
108InBlock.gif                    guid = new Guid(connMgrDestinationInfo.guid.ToString());
109ExpandedSubBlockEnd.gif            }

110InBlock.gif            return guid;
111ExpandedSubBlockEnd.gif        }

112InBlock.gif        public static unsafe Guid GuidConnectionURL(string url)
113ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
114InBlock.gif            Guid guid = Guid.Empty;
115InBlock.gif            int result = ConnMgrMapURL(url, ref guid, null);
116InBlock.gif            if (result != 0throw new Exception("ConnMgrMapURL()");
117InBlock.gif            return guid;
118ExpandedSubBlockEnd.gif        }

119InBlock.gif
120InBlock.gif        public static unsafe IntPtr EstablishConnection(Guid guid)
121ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
122InBlock.gif            CONNMGR_CONNECTIONINFO connectionInfo = new CONNMGR_CONNECTIONINFO();
123InBlock.gif            connectionInfo.dwParams = CONNMGR_PARAM_GUIDDESTNET;
124InBlock.gif            connectionInfo.dwPriority = CONNMGR_PRIORITY_USERINTERACTIVE;
125InBlock.gif            connectionInfo.guidDestNet = guid;
126InBlock.gif            connectionInfo.bExclusive = 1;
127InBlock.gif            IntPtr hConnection = IntPtr.Zero;
128InBlock.gif            int result = ConnMgrEstablishConnection(connectionInfo, out hConnection);
129InBlock.gif            if (result != 0throw new Exception("ConnMgrEstablishConnection()");
130InBlock.gif            return hConnection;
131ExpandedSubBlockEnd.gif        }

132InBlock.gif        public static unsafe IntPtr EstablishConnection_noConnect(Guid guid)
133ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
134InBlock.gif            CONNMGR_CONNECTIONINFO connectionInfo = new CONNMGR_CONNECTIONINFO();
135InBlock.gif            connectionInfo.dwParams = CONNMGR_PARAM_GUIDDESTNET;
136InBlock.gif            connectionInfo.dwPriority = CONNMGR_PRIORITY_USERINTERACTIVE;
137InBlock.gif            connectionInfo.guidDestNet = guid;
138InBlock.gif            connectionInfo.bDisabled = 0;
139InBlock.gif            connectionInfo.bExclusive = 1;
140InBlock.gif            IntPtr hConnection = IntPtr.Zero;
141InBlock.gif            int result = ConnMgrEstablishConnection(connectionInfo, out hConnection);
142InBlock.gif            if (result != 0throw new Exception("ConnMgrEstablishConnection()");
143InBlock.gif            return hConnection;
144ExpandedSubBlockEnd.gif        }

145InBlock.gif        public static unsafe IntPtr EstablishConnectionSync(Guid guid, double timeout)
146ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
147InBlock.gif            CONNMGR_CONNECTIONINFO connectionInfo = new CONNMGR_CONNECTIONINFO();
148InBlock.gif            connectionInfo.dwParams = CONNMGR_PARAM_GUIDDESTNET;
149InBlock.gif            connectionInfo.dwPriority = CONNMGR_PRIORITY_USERINTERACTIVE;
150InBlock.gif            connectionInfo.guidDestNet = guid;
151InBlock.gif            IntPtr hConnection = IntPtr.Zero;
152InBlock.gif            uint tout = (uint)(timeout * 1000 + 1);
153InBlock.gif            uint status = 0;
154InBlock.gif            int result = ConnMgrEstablishConnectionSync(connectionInfo, out hConnection, tout, &status);
155InBlock.gif            if (result != 0throw new Exception("ConnMgrEstablishConnectionSync()");
156InBlock.gif            return hConnection;
157ExpandedSubBlockEnd.gif        }

158InBlock.gif
159InBlock.gif        public static unsafe void ReleaseConnection(IntPtr hConnection)
160ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
161InBlock.gif            int result = ConnMgrReleaseConnection(hConnection, 0);
162InBlock.gif            if (result != 0throw new Exception("ConnMgrReleaseConnection()");
163ExpandedSubBlockEnd.gif        }

164InBlock.gif        
165InBlock.gif        public void OpenGprs()
166ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
167InBlock.gif            try
168ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
169InBlock.gif                Guid guid = GuidConnectionURL("http://www.google.cn/");
170InBlock.gif                hConnect = EstablishConnection(guid);
171ExpandedSubBlockEnd.gif            }

172InBlock.gif            catch (Exception ex)
173ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
174InBlock.gif                throw new Exception("打开GPRS错误");
175ExpandedSubBlockEnd.gif            }

176ExpandedSubBlockEnd.gif        }

177InBlock.gif
178InBlock.gif        public void CloseGprs()
179ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
180InBlock.gif            try
181ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
182InBlock.gif                Guid guid = GuidConnectionURL("http://www.google.cn/");
183InBlock.gif                IntPtr hConnection = EstablishConnection(guid);
184InBlock.gif                //IntPtr hConnection = EstablishConnection_noConnect(guid);
185InBlock.gif                ReleaseConnection(hConnection);
186InBlock.gif                //IsConnected = false;
187ExpandedSubBlockEnd.gif            }

188InBlock.gif            catch (Exception ex)
189ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
190InBlock.gif                throw new Exception("关闭GPRS错误,可能未存在GPRS联接");
191ExpandedSubBlockEnd.gif            }

192ExpandedSubBlockEnd.gif        }

193InBlock.gif
194InBlock.gif        public unsafe uint CheckGprsStatus()
195ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
196InBlock.gif            uint status = 0;
197InBlock.gif
198InBlock.gif            Guid guid = GuidConnectionURL("http://www.google.cn/");
199InBlock.gif            IntPtr hConnection = EstablishConnection_noConnect(guid);
200InBlock.gif            System.Threading.Thread.Sleep(500);
201InBlock.gif            int ret = ConnMgrConnectionStatus(hConnection, &status);
202InBlock.gif        
203InBlock.gif            return status;
204ExpandedSubBlockEnd.gif        }

205InBlock.gif
206ExpandedSubBlockEnd.gif    }

207InBlock.gif
208ExpandedBlockEnd.gif}

209None.gif



 

public uint ConnectMobileNetwork()
     {
         try
         {
             uint dwStatus = 0;
             string csDesc = "";
             ConnectManager.GUID guidNetworkObject = new ConnectManager.GUID();
             int nIndex = connectManager.MapURLAndGUID("http://www.google.com.cn%22/, ref guidNetworkObject, ref csDesc);

             if (nIndex >= 0)
             {
                 connectManager.EstablishConnection(guidNetworkObject, dwStatus);
             }

             return dwStatus;

         }
         catch
         {
             return 0;
         }
     }

转载于:https://www.cnblogs.com/Samples/archive/2010/08/20/1804541.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值