Exemplo n.º 1
0
        /// <summary>
        /// DLLを動的にロードし、関数ポインタを格納する。
        /// </summary>
        /// <param name="DllName">ロードするDLL名</param>
        private static void Load(string DllName)
        {
            _IsEnable = false;
            //DLLを読み込み
            hModule = NativeMethods.LoadLibrary(DllName);
            //失敗したら終了
            if (hModule == IntPtr.Zero)
            {
                return;
            }

            //関数アドレスの取得
            XInputEnable_FuncAdd               = NativeMethods.GetProcAddress(hModule, "XInputEnable");
            XInputGetState_FuncAdd             = NativeMethods.GetProcAddress(hModule, "XInputGetState");
            XInputSetState_FuncAdd             = NativeMethods.GetProcAddress(hModule, "XInputSetState");
            XInputGetBatteryInfomation_FuncAdd = NativeMethods.GetProcAddress(hModule, "XInputGetBatteryInformation");
            XInputGetKeystroke_FuncAdd         = NativeMethods.GetProcAddress(hModule, "XInputGetKeystroke");
            XInputGetCapabilities_FuncAdd      = NativeMethods.GetProcAddress(hModule, "XInputGetCapabilities");
            XInputGetStateEx_FuncAdd           = NativeMethods.GetProcAddressFromOrdinalNum(hModule, (IntPtr)100);            //非公開関数:XInputGetStateEx
            XInputPowerOffController_FuncAdd   = NativeMethods.GetProcAddressFromOrdinalNum(hModule, (IntPtr)103);            //非公開関数:XInputPowerOffController OrdinalNumber=103

            //関数アドレスからDelegateを取得
            funcXInputEnable   = (XInputEnable)Marshal.GetDelegateForFunctionPointer(XInputEnable_FuncAdd, typeof(XInputEnable));
            funcXInputGetState = (XInputGetState)Marshal.GetDelegateForFunctionPointer(XInputGetState_FuncAdd, typeof(XInputGetState));
            funcXInputSetState = (XInputSetState)Marshal.GetDelegateForFunctionPointer(XInputSetState_FuncAdd, typeof(XInputSetState));
            funcXInputGetBatteryInformation = (XInputGetBatteryInformation)Marshal.GetDelegateForFunctionPointer(XInputGetBatteryInfomation_FuncAdd, typeof(XInputGetBatteryInformation));
            funcXInputGetKeystroke          = (XInputGetKeystroke)Marshal.GetDelegateForFunctionPointer(XInputGetKeystroke_FuncAdd, typeof(XInputGetKeystroke));
            funcXInputGetCapabilities       = (XInputGetCapabilities)Marshal.GetDelegateForFunctionPointer(XInputGetCapabilities_FuncAdd, typeof(XInputGetCapabilities));
            func_XInputGetStateEx           = (XInputGetStateEx)Marshal.GetDelegateForFunctionPointer(XInputGetStateEx_FuncAdd, typeof(XInputGetStateEx));
            func_XInputPowerOffController   = (XInputPowerOffController)Marshal.GetDelegateForFunctionPointer(XInputPowerOffController_FuncAdd, typeof(XInputPowerOffController));

            _DllName  = DllName;
            _IsEnable = true;
        }
            internal XInput()
            {
                // Try to load the newest XInput***.dll installed on the system
                // The delegates below will be loaded dynamically from that dll
                dll = Kernel32.LoadLibrary("XINPUT1_4");
                if (dll == IntPtr.Zero)
                {
                    dll = Kernel32.LoadLibrary("XINPUT1_3");
                }

                if (dll == IntPtr.Zero)
                {
                    dll = Kernel32.LoadLibrary("XINPUT1_2");
                }

                if (dll == IntPtr.Zero)
                {
                    dll = Kernel32.LoadLibrary("XINPUT1_1");
                }

                if (dll == IntPtr.Zero)
                {
                    dll = Kernel32.LoadLibrary("XINPUT9_1_0");
                }

                if (dll == IntPtr.Zero)
                {
                    Debug.Print("XInput was not found on this platform");
                    return;
                }

                // Load the entry points we are interested in from that dll
                GetCapabilities = (XInputGetCapabilities)Load("XInputGetCapabilities", typeof(XInputGetCapabilities));
                GetState        =
                    // undocumented XInputGetStateEx (Ordinal 100) with support for the "Guide" button (requires XINPUT_1_3+)
                    (XInputGetState)Load(100, typeof(XInputGetState)) ??
                    // documented XInputGetState (no support for the "Guide" button)
                    (XInputGetState)Load("XInputGetState", typeof(XInputGetState));
                SetState = (XInputSetState)Load("XInputSetState", typeof(XInputSetState));
            }