summaryrefslogtreecommitdiffhomepage
path: root/compat/win32-com.cpp
blob: 7bebc500f7162d185e414c2a5afd28b36b8b0b68 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#ifdef _WIN32

#include "win32-com.hpp"

#include "compat/util.hpp"

#include <QString>
#include <QCoreApplication>
#include <QApplication>
#include <QThread>
#include <QDebug>

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)
    {
        qDebug() << "CoInitializeEx failed" << (progn (
                                                     switch (ret)
                                                     {
                                                         case RPC_E_CHANGED_MODE:
                                                            return QStringLiteral("COM threading mode already set");
                                                         default:
                                                            return QStringLiteral("Unknown error ") + QString::number(long(ret));
                                                     }
                                                     ));
        return false;
    }

    return true;
}
#endif