Windows: Add helper function to initialize COM per thread

This adapts a pattern first introduced for qtmultimedia (7398c5daa9).

Pick-to: 6.9.0
Change-Id: I4682fcf49fe074c55b4a70a1e1ec659b33b31bbb
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
(cherry picked from commit f5ac4a7f1471d580f2f8e6d471c1bf1771a0b2e6)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Kai Köhne 2025-03-15 07:07:58 +01:00 committed by Qt Cherry-pick Bot
parent 43a4b9c1f2
commit e2977d2e97
2 changed files with 19 additions and 0 deletions

View File

@ -33,6 +33,23 @@ QComHelper::~QComHelper()
CoUninitialize();
}
/*!
\internal
Make sure the COM library is is initialized on current thread.
Initializes COM as a single-threaded apartment on this thread and
ensures that CoUninitialize will be called on the same thread when
the thread exits. Note that the last call to CoUninitialize on the
main thread will always be made during destruction of static
variables at process exit.
https://docs.microsoft.com/en-us/windows/apps/desktop/modernize/modernize-packaged-apps
*/
void qt_win_ensureComInitializedOnThisThread()
{
static thread_local QComHelper s_comHelper;
}
/*!
\internal
Checks if the application has a \e{package identity}

View File

@ -42,6 +42,8 @@ private:
DWORD m_threadId{ GetCurrentThreadId() };
};
Q_CORE_EXPORT void qt_win_ensureComInitializedOnThisThread();
Q_CORE_EXPORT bool qt_win_hasPackageIdentity();
QT_END_NAMESPACE