From c07cd440cd46345297debb47cb260f8688975f50 Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Sat, 31 Mar 2018 14:58:30 +0800 Subject: [PATCH] Split args manually in runExternalProgram() Need to split arguments manually because QProcess::startDetached(QString) will strip off empty parameters. E.g. `python.exe "1" "" "3"` will become `python.exe "1" "3"`. Closes #8454. --- src/app/application.cpp | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/src/app/application.cpp b/src/app/application.cpp index 4f79a600f88..645dd5c95ad 100644 --- a/src/app/application.cpp +++ b/src/app/application.cpp @@ -31,6 +31,10 @@ #include +#ifdef Q_OS_WIN +#include +#endif + #include #include #include @@ -77,6 +81,10 @@ #include #endif // DISABLE_GUI +#ifdef Q_OS_WIN +#include +#endif + #ifndef DISABLE_WEBUI #include "webui/webui.h" #endif @@ -275,7 +283,7 @@ void Application::processMessage(const QString &message) void Application::runExternalProgram(const BitTorrent::TorrentHandle *torrent) const { - QString program = Preferences::instance()->getAutoRunProgram(); + QString program = Preferences::instance()->getAutoRunProgram().trimmed(); program.replace("%N", torrent->name()); program.replace("%L", torrent->category()); @@ -297,7 +305,22 @@ void Application::runExternalProgram(const BitTorrent::TorrentHandle *torrent) c #if defined(Q_OS_UNIX) QProcess::startDetached(QLatin1String("/bin/sh"), {QLatin1String("-c"), program}); #else - QProcess::startDetached(program); + std::unique_ptr programWchar(new wchar_t[program.length() + 1] {}); + program.toWCharArray(programWchar.get()); + + // Need to split arguments manually because QProcess::startDetached(QString) + // will strip off empty parameters. + // E.g. `python.exe "1" "" "3"` will become `python.exe "1" "3"` + int argCount = 0; + LPWSTR *args = ::CommandLineToArgvW(programWchar.get(), &argCount); + + QStringList argList; + for (int i = 1; i < argCount; ++i) + argList += QString::fromWCharArray(args[i]); + + QProcess::startDetached(QString::fromWCharArray(args[0]), argList); + + ::LocalFree(args); #endif }