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