From f5ac4a7f1471d580f2f8e6d471c1bf1771a0b2e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kai=20K=C3=B6hne?= Date: Sat, 15 Mar 2025 07:07:58 +0100 Subject: [PATCH] Windows: Add helper function to initialize COM per thread This adapts a pattern first introduced for qtmultimedia (7398c5daa9). Pick-to: 6.9 6.9.0 Change-Id: I4682fcf49fe074c55b4a70a1e1ec659b33b31bbb Reviewed-by: Thiago Macieira --- src/corelib/kernel/qfunctions_win.cpp | 17 +++++++++++++++++ src/corelib/kernel/qfunctions_win_p.h | 2 ++ 2 files changed, 19 insertions(+) diff --git a/src/corelib/kernel/qfunctions_win.cpp b/src/corelib/kernel/qfunctions_win.cpp index 048fdbc934c..4c057cdb2c5 100644 --- a/src/corelib/kernel/qfunctions_win.cpp +++ b/src/corelib/kernel/qfunctions_win.cpp @@ -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} diff --git a/src/corelib/kernel/qfunctions_win_p.h b/src/corelib/kernel/qfunctions_win_p.h index ab5417f8a25..e8cc57cc84a 100644 --- a/src/corelib/kernel/qfunctions_win_p.h +++ b/src/corelib/kernel/qfunctions_win_p.h @@ -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