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