gprs 自动拨号 wince ppc

来源:互联网 发布:mac pro 2016 尺寸 编辑:程序博客网 时间:2024/05/21 11:22

因为当我们做一个http请求时ppc会自动拨号,但是如果我们使用UDP做数据传输时是不会自动拨号的,为此我曾经给windowmobile@microsoft.com发邮件询问怎样用C#实现GPRS拨号.MS告诉我可以先做一个HTTP请求.做为一个应急的方案确实可以这样做.

view plainprint?
  1.    
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.Text;  
  5. using System.Net;  
  6. using System.Runtime.InteropServices;  
  7. using System.Threading;  
  8. using System.Collections;  
  9. namespace PPc.GPRS  
  10. {  
  11.     public class GPRSConnection   
  12.     {   
  13.         const int S_OK = 0;   
  14.         const uint CONNMGR_PARAM_GUIDDESTNET = 0x1;   
  15.         const uint CONNMGR_FLAG_PROXY_HTTP = 0x1;   
  16.         const uint CONNMGR_PRIORITY_USERINTERACTIVE = 0x08000;   
  17.         const uint INFINITE = 0xffffffff;   
  18.         const uint CONNMGR_STATUS_CONNECTED = 0x10;   
  19.         static Hashtable ht = new Hashtable();   
  20.         static GPRSConnection()   
  21.         {   
  22.             ManualResetEvent mre = new ManualResetEvent(false);   
  23.             mre.Handle = ConnMgrApiReadyEvent();   
  24.             mre.WaitOne();   
  25.             CloseHandle(mre.Handle);   
  26.         }   
  27.         ~GPRSConnection()   
  28.         {   
  29.             ReleaseAll();   
  30.         }   
  31.         public static bool Setup(Uri url)   
  32.         {   
  33.             return Setup(url.ToString());   
  34.         }   
  35.         public static bool Setup(string urlStr)   
  36.         {   
  37.             ConnectionInfo ci = new ConnectionInfo();   
  38.             IntPtr phConnection = IntPtr.Zero;   
  39.             uint status = 0;  
  40.             if (ht[urlStr] != null)  
  41.             {  
  42.                 return true;  
  43.             }  
  44.             if (ConnMgrMapURL(urlStr, ref ci.guidDestNet, IntPtr.Zero) != S_OK)  
  45.             {  
  46.                 return false;  
  47.             }  
  48.             ci.cbSize = (uint)Marshal.SizeOf(ci);   
  49.             ci.dwParams = CONNMGR_PARAM_GUIDDESTNET;   
  50.             ci.dwFlags = CONNMGR_FLAG_PROXY_HTTP;   
  51.             ci.dwPriority = CONNMGR_PRIORITY_USERINTERACTIVE;   
  52.             ci.bExclusive = 0;   
  53.             ci.bDisabled = 0;   
  54.             ci.hWnd = IntPtr.Zero;   
  55.             ci.uMsg = 0;   
  56.             ci.lParam = 0;  
  57.             if (ConnMgrEstablishConnectionSync(ref ci, ref phConnection, INFINITE, ref status) != S_OK && status != CONNMGR_STATUS_CONNECTED)  
  58.             {  
  59.                 return false;  
  60.             }  
  61.             ht[urlStr] = phConnection;   
  62.             return true;   
  63.         }   
  64.         public static bool Release(Uri url)   
  65.         {   
  66.             return Release(url.ToString());   
  67.         }   
  68.         public static bool Release(string urlStr)   
  69.         {   
  70.             return Release(urlStr, true);   
  71.         }   
  72.         private static bool Release(string urlStr, bool removeNode)   
  73.         {   
  74.             bool res = true;   
  75.             IntPtr ph = IntPtr.Zero;  
  76.             if (ht[urlStr] == null)  
  77.             {  
  78.                 return true;  
  79.             }  
  80.             ph = (IntPtr)ht[urlStr];  
  81.             if (ConnMgrReleaseConnection(ph, 1) != S_OK)  
  82.             {  
  83.                 res = false;  
  84.             }  
  85.             CloseHandle(ph);  
  86.             if (removeNode)  
  87.             {  
  88.                 ht.Remove(urlStr);  
  89.             }  
  90.             return res;   
  91.         }   
  92.         public static void ReleaseAll()   
  93.         {   
  94.             foreach (DictionaryEntry de in ht)   
  95.             {   
  96.                 Release((string)de.Key, false);   
  97.             }   
  98.             ht.Clear();   
  99.         }   
  100.         [StructLayout(LayoutKind.Sequential)]    
  101.         public struct ConnectionInfo   
  102.         {   
  103.             public uint cbSize;   
  104.             public uint dwParams;   
  105.             public uint dwFlags;   
  106.             public uint dwPriority;   
  107.             public int bExclusive;   
  108.             public int bDisabled;   
  109.             public Guid guidDestNet;   
  110.             public IntPtr hWnd;   
  111.             public uint uMsg;   
  112.             public uint lParam;   
  113.             public uint ulMaxCost;  
  114.             public uint ulMinRcvBw;   
  115.             public uint ulMaxConnLatency;          
  116.         }   
  117.         [DllImport("cellcore.dll")]         
  118.         private static extern int ConnMgrMapURL(string pwszURL, ref Guid pguid, IntPtr pdwIndex);   
  119.         [DllImport("cellcore.dll")]   
  120.         private static extern int ConnMgrEstablishConnectionSync(ref ConnectionInfo ci, ref IntPtr phConnection, uint dwTimeout, ref uint pdwStatus);   
  121.         [DllImport("cellcore.dll")] private static extern IntPtr ConnMgrApiReadyEvent();          
  122.         [DllImport("cellcore.dll")] private static extern int ConnMgrReleaseConnection(IntPtr hConnection, int bCache);          
  123.         [DllImport("coredll.dll")] private static extern int CloseHandle(IntPtr hObject);      
  124.     }  
  125. }  
  126.   
  127. e.g :  
  128. GPRSConnection .Setup("http://www.microsoft.com");  

http://blog.csdn.net/ssihc0/article/details/5058054
原创粉丝点击