From 62c137157ed04d08e6bcc7a741bcdb046943776f Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Wed, 17 Aug 2016 21:40:22 +0200 Subject: compat/win32-com: default to right COM type without caller specifying it The main window always needs the apartment threaded context for OLE used by Qt. Default to apartment threaded before a QApplication is created, or if the thread is the QApplication thread. Use multithreaded COM otherwise. --- compat/camera-names.cpp | 2 +- compat/win32-com.cpp | 15 ++++++++++++++- compat/win32-com.hpp | 3 ++- 3 files changed, 17 insertions(+), 3 deletions(-) (limited to 'compat') diff --git a/compat/camera-names.cpp b/compat/camera-names.cpp index 21ff3b52..2f4c2f91 100644 --- a/compat/camera-names.cpp +++ b/compat/camera-names.cpp @@ -33,7 +33,7 @@ OPENTRACK_COMPAT_EXPORT QList get_camera_names() #if defined(_WIN32) // Create the System Device Enumerator. HRESULT hr; - init_com_threading(com_apartment); + init_com_threading(); ICreateDevEnum *pSysDevEnum = NULL; hr = CoCreateInstance(CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER, IID_ICreateDevEnum, (void **)&pSysDevEnum); if (FAILED(hr)) diff --git a/compat/win32-com.cpp b/compat/win32-com.cpp index 689e1411..7bebc500 100644 --- a/compat/win32-com.cpp +++ b/compat/win32-com.cpp @@ -5,11 +5,24 @@ #include "compat/util.hpp" #include +#include +#include #include #include -bool OPENTRACK_COMPAT_EXPORT init_com_threading(com_type t) +bool OPENTRACK_COMPAT_EXPORT init_com_threading(com_type t_) { + const com_type t = progn( + if (t_ != com_invalid) + return t_; + if (QCoreApplication::instance() == nullptr) + return com_apartment; + if (qApp->thread() == QThread::currentThread()) + return com_apartment; + else + return com_multithreaded; + ); + HRESULT ret = CoInitializeEx(0, t); if (ret != S_OK && ret != S_FALSE) diff --git a/compat/win32-com.hpp b/compat/win32-com.hpp index 4e535815..fe2b478f 100644 --- a/compat/win32-com.hpp +++ b/compat/win32-com.hpp @@ -11,8 +11,9 @@ enum com_type : int { com_multithreaded = COINIT_MULTITHREADED, com_apartment = COINIT_APARTMENTTHREADED, + com_invalid = 0, }; -bool OPENTRACK_COMPAT_EXPORT init_com_threading(com_type t); +bool OPENTRACK_COMPAT_EXPORT init_com_threading(com_type t = com_invalid); #endif -- cgit v1.2.3