c# 断开GPRS

 class CloseGPRS
    {
        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", SetLastError = true, CharSet = CharSet.Auto)]
        private static extern uint RasGetConnectStatus(IntPtr hrasconn, RASCONNSTATUS lprasconnstatus);

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

        /**/
        /// <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);
                    if (l_Err == ERROR_INVALID_HANDLE)
                        break;
                    if (l_TimeCnt >= l_TimeOut)
                        break;
                    System.Threading.Thread.Sleep(300);
                    l_TimeCnt += 300;
                }
            }
        }
    }

 

这是偶在网上找的源码`不知哪位写的``借来用用`呵呵```

这个是关闭所有RAS连接`

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值