WZero3の通信を切断する方法


RASの切断方法
            に書いてあるとおり。
 
呼び出し
            RasConn.CloseAllConnections();
—————–
using System;
using System.Runtime.InteropServices;
 
namespace Uchukamen.WZero3
{
    /// <summary>
    /// This class is based on code from "mikinder".
    /// http://www.developersdex.com/vb/message.asp?p=2916&r=5643969
    /// </summary>
    class RasConn
    {
        const int MAX_PATH = 260;
        const int RAS_MaxDeviceType = 16;
        const int RAS_MaxPhoneNumber = 128;
        const int RAS_MaxEntryName = 20;
        const int RAS_MaxDeviceName = 128;
        const int SUCCESS = 0;
        const int ERROR_NOT_ENOUGH_MEMORY = 8;
        const int RASBASE = 600;
        const int ERROR_BUFFER_TOO_SMALL = RASBASE + 3;
        const int ERROR_INVALID_SIZE = RASBASE + 32;
        #region DllImport
        //// — RASCONN data structure definition (refer to ras.h) —
        //private const int RAS_MaxEntryName = 20;
        [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;
        }
        // ——————————————–
        [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")]
        private static extern uint RasHangUp(IntPtr pRasConn);
        #endregion
        /// <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[] connections = GetAllConnections();
            for (int i = 0; i < connections.Length; ++i)
            {
                RasHangUp(connections[i].hrasconn);
            }
        }
    }
}
広告

コメントを残す

以下に詳細を記入するか、アイコンをクリックしてログインしてください。

WordPress.com ロゴ

WordPress.com アカウントを使ってコメントしています。 ログアウト / 変更 )

Twitter 画像

Twitter アカウントを使ってコメントしています。 ログアウト / 変更 )

Facebook の写真

Facebook アカウントを使ってコメントしています。 ログアウト / 変更 )

Google+ フォト

Google+ アカウントを使ってコメントしています。 ログアウト / 変更 )

%s と連携中


%d人のブロガーが「いいね」をつけました。