diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2016-08-17 21:40:22 +0200 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2016-08-17 22:32:03 +0200 |
commit | 62c137157ed04d08e6bcc7a741bcdb046943776f (patch) | |
tree | 8f717bd5fa9fdda743f08677544e8cd5c9f9fef2 /compat | |
parent | fad4b316af2cde1a5988ed1bbdbc4ba5933e88df (diff) |
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.
Diffstat (limited to 'compat')
-rw-r--r-- | compat/camera-names.cpp | 2 | ||||
-rw-r--r-- | compat/win32-com.cpp | 15 | ||||
-rw-r--r-- | compat/win32-com.hpp | 3 |
3 files changed, 17 insertions, 3 deletions
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<QString> 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 <QString> +#include <QCoreApplication> +#include <QApplication> #include <QThread> #include <QDebug> -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 |