From 7f1d1847cab86f3c38813d673ea67fcc066c6f06 Mon Sep 17 00:00:00 2001 From: Roland Winklmeier Date: Mon, 1 Oct 2018 16:45:57 +0200 Subject: [PATCH] Revert back to DirectInput COM initialization Using and linking against DirectInput libraries directly caused dinput.dll to be loaded at startup time. Windows 10 kills applications started from services (as the Jenkins slave service) that load dinput.dll. This commit also adds some safety in case no helper windows could be created. --- src/blackinput/win/joystickwindows.cpp | 20 +++++++++++++++----- src/blackinput/win/joystickwindows.h | 7 ++++--- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/src/blackinput/win/joystickwindows.cpp b/src/blackinput/win/joystickwindows.cpp index 1f0223ba4..4ff95e043 100644 --- a/src/blackinput/win/joystickwindows.cpp +++ b/src/blackinput/win/joystickwindows.cpp @@ -152,15 +152,20 @@ namespace BlackInput CJoystickWindows::CJoystickWindows(QObject *parent) : IJoystick(parent) { + // Initialize COM + CoInitializeEx(nullptr, COINIT_MULTITHREADED); this->createHelperWindow(); - this->initDirectInput(); - this->enumJoystickDevices(); + + if (helperWindow) + { + this->initDirectInput(); + this->enumJoystickDevices(); + } } CJoystickWindows::~CJoystickWindows() { - m_joystickDevices.clear(); - m_directInput.reset(); + CoUninitialize(); } void ReleaseDirectInput(IDirectInput8 *obj) @@ -171,8 +176,13 @@ namespace BlackInput HRESULT CJoystickWindows::initDirectInput() { IDirectInput8 *directInput = nullptr; - HRESULT hr = DirectInput8Create(GetModuleHandle(nullptr), DIRECTINPUT_VERSION, IID_IDirectInput8, reinterpret_cast(&directInput), nullptr); + // HRESULT hr = DirectInput8Create(GetModuleHandle(nullptr), DIRECTINPUT_VERSION, IID_IDirectInput8, reinterpret_cast(&directInput), nullptr); + HRESULT hr = CoCreateInstance(CLSID_DirectInput8, nullptr, CLSCTX_INPROC_SERVER, IID_IDirectInput8, reinterpret_cast(&directInput)); + if(FAILED(hr)) { return hr; } m_directInput = DirectInput8Ptr(directInput, ReleaseDirectInput); + + HINSTANCE instance = GetModuleHandle(nullptr); + hr = m_directInput->Initialize(instance, DIRECTINPUT_VERSION); return hr; } diff --git a/src/blackinput/win/joystickwindows.h b/src/blackinput/win/joystickwindows.h index 36253fb17..bb1103d23 100644 --- a/src/blackinput/win/joystickwindows.h +++ b/src/blackinput/win/joystickwindows.h @@ -131,15 +131,16 @@ namespace BlackInput //! Joystick enumeration callback static BOOL CALLBACK enumJoysticksCallback(const DIDEVICEINSTANCE *pdidInstance, VOID *pContext); - DirectInput8Ptr m_directInput; //!< DirectInput object + ATOM helperWindowClass = 0; + HWND helperWindow = nullptr; + + DirectInput8Ptr m_directInput; //!< DirectInput object QVector m_joystickDevices; //!< Joystick devices BlackMisc::Input::CHotkeyCombination m_buttonCombination; const TCHAR *helperWindowClassName = TEXT("HelperWindow"); const TCHAR *helperWindowName = TEXT("JoystickCatcherWindow"); - ATOM helperWindowClass = 0; - HWND helperWindow = nullptr; }; } // ns