summaryrefslogtreecommitdiffhomepage
path: root/compat/win32-com.cpp
blob: dd7c24a868d1e36c1d10251936465eefd1a817a1 (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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#ifdef _WIN32

#include "win32-com.hpp"

#include <QString>
#include <QThread>
#include <QDebug>

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