diff --git a/src/plugins/platforms/windows/qwindowsservices.cpp b/src/plugins/platforms/windows/qwindowsservices.cpp index 09ae55cd99..8a95cc53a8 100644 --- a/src/plugins/platforms/windows/qwindowsservices.cpp +++ b/src/plugins/platforms/windows/qwindowsservices.cpp @@ -44,6 +44,8 @@ #include #include #include +#include +#include #include @@ -54,15 +56,33 @@ QT_BEGIN_NAMESPACE enum { debug = 0 }; +static quintptr runShellExecute(const wchar_t *path) +{ + HINSTANCE result = nullptr; + if (SUCCEEDED(CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE))) { + result = ShellExecute(nullptr, nullptr, path, nullptr, nullptr, SW_SHOWNORMAL); + CoUninitialize(); + } + return reinterpret_cast(result); +} + static inline bool shellExecute(const QUrl &url) { const QString nativeFilePath = url.isLocalFile() && !url.hasFragment() && !url.hasQuery() ? QDir::toNativeSeparators(url.toLocalFile()) : url.toString(QUrl::FullyEncoded); - const auto result = - reinterpret_cast(ShellExecute(nullptr, nullptr, - reinterpret_cast(nativeFilePath.utf16()), - nullptr, nullptr, SW_SHOWNORMAL)); + + + // Run ShellExecute() in a thread since it may spin the event loop. + // Prevent it from interfering with processing of posted events (QTBUG-85676). + quintptr result = 0; + quintptr *resultPtr = &result; + const auto path = reinterpret_cast(nativeFilePath.utf16()); + QScopedPointer thread(QThread::create([path, resultPtr] + () { *resultPtr = runShellExecute(path); })); + thread->start(); + thread->wait(); + // ShellExecute returns a value greater than 32 if successful if (result <= 32) { qWarning("ShellExecute '%ls' failed (error %zu).", qUtf16Printable(url.toString()), result);