From 41c0a5169bfb6e87a48f90a2af57c03bba707470 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Thu, 11 Aug 2016 11:32:50 +0200 Subject: compat: add win32 COM init function --- opentrack-compat/win32-com.cpp | 60 ++++++++++++++++++++++++++++++++++++++++++ opentrack-compat/win32-com.hpp | 18 +++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 opentrack-compat/win32-com.cpp create mode 100644 opentrack-compat/win32-com.hpp (limited to 'opentrack-compat') diff --git a/opentrack-compat/win32-com.cpp b/opentrack-compat/win32-com.cpp new file mode 100644 index 00000000..dd7c24a8 --- /dev/null +++ b/opentrack-compat/win32-com.cpp @@ -0,0 +1,60 @@ +#ifdef _WIN32 + +#include "win32-com.hpp" + +#include +#include +#include + +bool OPENTRACK_COMPAT_EXPORT init_com_threading(com_type t) +{ + static thread_local com_type initialized = com_type(-1); + + if (initialized != com_type(-1)) + { + if (t != initialized) + { + QString tp("invalid type"); + switch (t) + { + case com_apartment: + tp = "apartment threaded"; + break; + case com_multithreaded: + tp = "multithreaded"; + break; + } + + qDebug() << "COM for thread" + << QThread::currentThread() << QThread::currentThreadId() + << "already initialized to" << tp; + + return false; + } + + return true; + } + + HRESULT ret = CoInitializeEx(0, t); + + if (ret != S_OK && ret != S_FALSE) + { + qDebug() << "CoInitializeEx failed:" << ret << GetLastError(); + return false; + } + + if (t == com_apartment) + { + ret = OleInitialize(nullptr); + + if (ret != S_OK && ret != S_FALSE) + qDebug() << "OleInitialize() failed:" << ret << GetLastError(); + + return false; + } + + initialized = t; + + return true; +} +#endif diff --git a/opentrack-compat/win32-com.hpp b/opentrack-compat/win32-com.hpp new file mode 100644 index 00000000..dcbea089 --- /dev/null +++ b/opentrack-compat/win32-com.hpp @@ -0,0 +1,18 @@ +#pragma once + +#ifdef _WIN32 + +#include "export.hpp" + +#include +#include + +enum com_type : int +{ + com_multithreaded = COINIT_MULTITHREADED, + com_apartment = COINIT_APARTMENTTHREADED, +}; + +bool OPENTRACK_COMPAT_EXPORT init_com_threading(com_type t = com_multithreaded); + +#endif -- cgit v1.2.3