/// <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)); }
static XInput() { var library = LoadLibrary("XInput1_4.dll"); var version = 140; if (library == NULL) { library = LoadLibrary("XInput1_3.dll"); version = 130; } if (library == NULL) { library = LoadLibrary("XInput9_1_0.dll"); version = 910; } if (library != NULL) { var getStatePointer = GetProcAddress(library, "XInputGetStateEx"); if (getStatePointer == NULL && version >= 130 && version <= 140) { getStatePointer = GetProcAddress(library, new IntPtr(100)); // <- XInputGetStateEx has ordinal 100 in these versions } if (getStatePointer == NULL) { getStatePointer = GetProcAddress(library, "XInputGetState"); } if (getStatePointer != NULL) { getState = Marshal.GetDelegateForFunctionPointer<XInputGetState>(getStatePointer); } } }