From 9cb01dedbadd0c84918cc40bbc3d4f1587fce700 Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Sat, 23 Jul 2016 11:54:07 +0800 Subject: [PATCH] Avoid spawning a new explorer.exe process when selecting "Open containing folder". Closes #5564. --- src/base/utils/misc.cpp | 36 ++++++++---------------------------- winconf.pri | 4 ++-- 2 files changed, 10 insertions(+), 30 deletions(-) diff --git a/src/base/utils/misc.cpp b/src/base/utils/misc.cpp index 142a1cf53..646b3e66f 100644 --- a/src/base/utils/misc.cpp +++ b/src/base/utils/misc.cpp @@ -52,6 +52,7 @@ #ifdef Q_OS_WIN #include #include +#include const int UNLEN = 256; #else #include @@ -578,35 +579,14 @@ void Utils::Misc::openFolderSelect(const QString &absolutePath) return; } #ifdef Q_OS_WIN - // Syntax is: explorer /select, "C:\Folder1\Folder2\file_to_select" - // Dir separators MUST be win-style slashes - - // QProcess::startDetached() has an obscure bug. If the path has - // no spaces and a comma(and maybe other special characters) it doesn't - // get wrapped in quotes. So explorer.exe can't find the correct path and - // displays the default one. If we wrap the path in quotes and pass it to - // QProcess::startDetached() explorer.exe still shows the default path. In - // this case QProcess::startDetached() probably puts its own quotes around ours. - - STARTUPINFO startupInfo; - ::ZeroMemory(&startupInfo, sizeof(startupInfo)); - startupInfo.cb = sizeof(startupInfo); - - PROCESS_INFORMATION processInfo; - ::ZeroMemory(&processInfo, sizeof(processInfo)); - - QString cmd = QString("explorer.exe /select,\"%1\"").arg(Utils::Fs::toNativePath(absolutePath)); - LPWSTR lpCmd = new WCHAR[cmd.size() + 1]; - cmd.toWCharArray(lpCmd); - lpCmd[cmd.size()] = 0; - - bool ret = ::CreateProcessW(NULL, lpCmd, NULL, NULL, FALSE, 0, NULL, NULL, &startupInfo, &processInfo); - delete [] lpCmd; - - if (ret) { - ::CloseHandle(processInfo.hProcess); - ::CloseHandle(processInfo.hThread); + HRESULT hresult = ::CoInitializeEx(nullptr, COINIT_MULTITHREADED); + ITEMIDLIST *pidl = ::ILCreateFromPathW(reinterpret_cast(Utils::Fs::toNativePath(path).utf16())); + if (pidl) { + ::SHOpenFolderAndSelectItems(pidl, 0, nullptr, 0); + ::ILFree(pidl); } + if ((hresult == S_OK) || (hresult == S_FALSE)) + ::CoUninitialize(); #elif defined(Q_OS_UNIX) && !defined(Q_OS_MAC) QProcess proc; proc.start("xdg-mime", QStringList() << "query" << "default" << "inode/directory"); diff --git a/winconf.pri b/winconf.pri index d71c33db7..63d12f33b 100644 --- a/winconf.pri +++ b/winconf.pri @@ -37,8 +37,8 @@ DEFINES += _UNICODE DEFINES += WIN32 DEFINES += _WIN32 DEFINES += WIN32_LEAN_AND_MEAN -DEFINES += _WIN32_WINNT=0x0500 -DEFINES += _WIN32_IE=0x0500 +DEFINES += _WIN32_WINNT=0x0501 +DEFINES += _WIN32_IE=0x0501 DEFINES += _CRT_SECURE_NO_DEPRECATE DEFINES += _SCL_SECURE_NO_DEPRECATE DEFINES += __USE_W32_SOCKETS