Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qfunctions_win.cpp
Go to the documentation of this file.
1// Copyright (C) 2022 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
5
6#include <QtCore/qdebug.h>
7
8#include <combaseapi.h>
9#include <objbase.h>
10
11#if __has_include(<appmodel.h>)
12# include <appmodel.h>
13# define HAS_APPMODEL
14#endif
15
16QT_BEGIN_NAMESPACE
17
18QComHelper::QComHelper(COINIT concurrencyModel)
19{
20 // Avoid overhead of initializing and using obsolete technology
21 concurrencyModel = COINIT(concurrencyModel | COINIT_DISABLE_OLE1DDE);
22
23 m_initResult = CoInitializeEx(nullptr, concurrencyModel);
24
25 if (FAILED(m_initResult))
26 qErrnoWarning(m_initResult, "Failed to initialize COM library");
27}
28
29QComHelper::~QComHelper()
30{
31 Q_ASSERT(m_threadId == GetCurrentThreadId());
32 if (SUCCEEDED(m_initResult))
33 CoUninitialize();
34}
35
36/*!
37 \internal
38 Make sure the COM library is is initialized on current thread.
39
40 Initializes COM as a single-threaded apartment on this thread and
41 ensures that CoUninitialize will be called on the same thread when
42 the thread exits. Note that the last call to CoUninitialize on the
43 main thread will always be made during destruction of static
44 variables at process exit.
45
46 https://docs.microsoft.com/en-us/windows/apps/desktop/modernize/modernize-packaged-apps
47*/
49{
50 static thread_local QComHelper s_comHelper;
51}
52
53/*!
54 \internal
55 Checks if the application has a \e{package identity}
56
57 Having a \e{package identity} is required to use many modern
58 Windows APIs.
59
60 https://docs.microsoft.com/en-us/windows/apps/desktop/modernize/modernize-packaged-apps
61*/
63{
64#if defined(HAS_APPMODEL)
65 static const bool hasPackageIdentity = []() {
66 UINT32 length = 0;
67 switch (const auto result = GetCurrentPackageFullName(&length, nullptr)) {
68 case ERROR_INSUFFICIENT_BUFFER:
69 return true;
70 case APPMODEL_ERROR_NO_PACKAGE:
71 return false;
72 default:
73 qWarning("Failed to resolve package identity (error code %ld)", result);
74 return false;
75 }
76 }();
77 return hasPackageIdentity;
78#else
79 return false;
80#endif
81}
82
83QT_END_NAMESPACE
#define __has_include(x)
void qt_win_ensureComInitializedOnThisThread()
bool qt_win_hasPackageIdentity()