Avoid spawning a new explorer.exe process when selecting "Open containing

folder". Closes #5564.
This commit is contained in:
Chocobo1 2016-07-23 11:54:07 +08:00
parent bb956b8453
commit 9cb01dedba
2 changed files with 10 additions and 30 deletions

View file

@ -52,6 +52,7 @@
#ifdef Q_OS_WIN
#include <windows.h>
#include <powrprof.h>
#include <Shlobj.h>
const int UNLEN = 256;
#else
#include <unistd.h>
@ -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<PCTSTR>(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");

View file

@ -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