中斷PDA上的GPRS連線

  • 4538
  • 0

中斷PDA上的所有GPRS連線

 


        // ------------------------------------------------------------------
        // GPRS connection control
        // ------------------------------------------------------------------

        private const int SUCCESS = 0;
        private const int ERROR_NOT_ENOUGH_MEMORY = 8;
        private const int RASBASE = 600;
        private const int ERROR_BUFFER_TOO_SMALL = RASBASE + 3;
        private const int ERROR_INVALID_SIZE = RASBASE + 32;

        private const int RAS_MaxPhoneNumber = 128;
        private const int RAS_MaxDeviceName = 128;
        private const int RAS_MaxDeviceType = 16;
        private const int MAX_PATH = 260;


        // --- RASCONN data structure definition (refer to ras.h) --
        private const int RAS_MaxEntryName = 20;
        private const int ERROR_INVALID_PORT_HANDLE = (RASBASE + 1);
        private const int ERROR_INVALID_HANDLE = 6;  


        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
        public struct RASCONN
        {
            public int dwSize;
            public IntPtr hrasconn;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = RAS_MaxEntryName + 1)]
            public string szEntryName;
        }


        
        [StructLayout(LayoutKind.Sequential,CharSet=CharSet.Auto)]
        struct RASCONNSTATUS
        {
            public int dwSize;
            public int rasconnstate;
            public int dwError;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = RAS_MaxDeviceType + 1)]
            public string szDeviceType;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = RAS_MaxDeviceName + 1)]
            public string szDeviceName;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = RAS_MaxPhoneNumber + 1)]
            public string szPhoneNumber;
        }
;

        // --------------------------------------------


        [DllImport("coredll.dll", SetLastError = true, CharSet = CharSet.Auto)]
        private static extern uint RasEnumConnections([In, Out] RASCONN[] rasconn, [In, Out] ref int cb, [Out] out int connections);

      
        [DllImport("coredll.dll")]
        public static extern uint RasHangUp(IntPtr pRasConn);


      

 

重點來了        //執行RasHangUp後要確認離線後再離開

 

        /// <summary>
        /// Returns all active RAS connections as an array of data structure RASCONN
        /// </summary>
        /// <returns></returns>

        public static RASCONN[] GetAllConnections()
        {
            RASCONN[] tempConn = new RASCONN[1];
            RASCONN[] allConnections = tempConn;

            tempConn[0].dwSize = Marshal.SizeOf(typeof(RASCONN));
            int lpcb = tempConn[0].dwSize;
            int lpcConnections = 0;
            uint ret = RasEnumConnections(tempConn, ref lpcb, out lpcConnections);
            if (ret == ERROR_INVALID_SIZE)
            {
                throw new Exception("RAS: RASCONN data structure has invalid format");
            }

            else if (ret == ERROR_BUFFER_TOO_SMALL && lpcb != 0)
            {
                // first call returned that there are more than one connections
                    // and more memory is required
                allConnections = new RASCONN[lpcb / Marshal.SizeOf(typeof(RASCONN))];
                allConnections[0] = tempConn[0];
                ret = RasEnumConnections(allConnections, ref lpcb, out lpcConnections);
            }


            // Check errors
            if (ret != SUCCESS)
            {
                throw new Exception("RAS returns error: " + ret);
            }

            if (lpcConnections > allConnections.Length)
            {
                throw new Exception("RAS: error retrieving correct connection count");
            }

            else if (lpcConnections == 0)
            {
                // if there are no connections resize the data structure
                allConnections = new RASCONN[0];
            }


            return allConnections;
        }


        /// <summary>
        /// Closes all active RAS connections
        /// </summary>
        /// <returns></returns>

        public static void CloseAllConnections()
        {
            RASCONN[] l_Cons = GetAllConnections();
            for (int i = 0; i < l_Cons.Length; ++i)
            {
                if (l_Cons[i].hrasconn == IntPtr.Zero) continue;

                RasHangUp(l_Cons[i].hrasconn);
                System.Threading.Thread.Sleep(0);

                //
                RASCONNSTATUS l_RASConnStatus = new RASCONNSTATUS();
                l_RASConnStatus.dwSize = Marshal.SizeOf(typeof(RASCONNSTATUS));
                int l_TimeOut = 20000; //20秒TimeOut
                int l_TimeCnt = 0;
                while (true)
                {
                  uint l_Err = RasGetConnectStatus(l_Cons[i].hrasconn, l_RASConnStatus);
                  //已close 連線
                  if (l_Err == ERROR_INVALID_HANDLE)
                    break;
                  //若超過TimeOut時間
                  if (l_TimeCnt >= l_TimeOut)
                    break;
                  //否則Sleep 0.3秒
                  System.Threading.Thread.Sleep(300);
                  l_TimeCnt += 300;
                }
  
            }


    
                }

使用這段就好了
CloseAllConnections()