From 6258dee96b705407bac3fc82cfc190a466975882 Mon Sep 17 00:00:00 2001 From: sledgehammer999 Date: Sun, 30 Aug 2015 04:34:55 +0300 Subject: [PATCH] Fix a bug with highlighting selected file on Windows. Closes #3185. --- src/core/misc.cpp | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/src/core/misc.cpp b/src/core/misc.cpp index 9482bb912..532887f27 100644 --- a/src/core/misc.cpp +++ b/src/core/misc.cpp @@ -719,7 +719,33 @@ void misc::openFolderSelect(const QString& absolutePath) if (QFileInfo(path).exists()) { // Syntax is: explorer /select, "C:\Folder1\Folder2\file_to_select" // Dir separators MUST be win-style slashes - QProcess::startDetached("explorer.exe", QStringList() << "/select," << fsutils::toNativePath(absolutePath)); + + // 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(fsutils::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); + } } else { // If the item to select doesn't exist, try to open its parent