Skip to content

Commit c07cd44

Browse files
committed
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.
1 parent ccc91e2 commit c07cd44

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

src/app/application.cpp

+25-2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@
3131

3232
#include <algorithm>
3333

34+
#ifdef Q_OS_WIN
35+
#include <memory>
36+
#endif
37+
3438
#include <QAtomicInt>
3539
#include <QDebug>
3640
#include <QFileInfo>
@@ -77,6 +81,10 @@
7781
#include <iostream>
7882
#endif // DISABLE_GUI
7983

84+
#ifdef Q_OS_WIN
85+
#include <Shellapi.h>
86+
#endif
87+
8088
#ifndef DISABLE_WEBUI
8189
#include "webui/webui.h"
8290
#endif
@@ -275,7 +283,7 @@ void Application::processMessage(const QString &message)
275283

276284
void Application::runExternalProgram(const BitTorrent::TorrentHandle *torrent) const
277285
{
278-
QString program = Preferences::instance()->getAutoRunProgram();
286+
QString program = Preferences::instance()->getAutoRunProgram().trimmed();
279287
program.replace("%N", torrent->name());
280288
program.replace("%L", torrent->category());
281289

@@ -297,7 +305,22 @@ void Application::runExternalProgram(const BitTorrent::TorrentHandle *torrent) c
297305
#if defined(Q_OS_UNIX)
298306
QProcess::startDetached(QLatin1String("/bin/sh"), {QLatin1String("-c"), program});
299307
#else
300-
QProcess::startDetached(program);
308+
std::unique_ptr<wchar_t[]> programWchar(new wchar_t[program.length() + 1] {});
309+
program.toWCharArray(programWchar.get());
310+
311+
// Need to split arguments manually because QProcess::startDetached(QString)
312+
// will strip off empty parameters.
313+
// E.g. `python.exe "1" "" "3"` will become `python.exe "1" "3"`
314+
int argCount = 0;
315+
LPWSTR *args = ::CommandLineToArgvW(programWchar.get(), &argCount);
316+
317+
QStringList argList;
318+
for (int i = 1; i < argCount; ++i)
319+
argList += QString::fromWCharArray(args[i]);
320+
321+
QProcess::startDetached(QString::fromWCharArray(args[0]), argList);
322+
323+
::LocalFree(args);
301324
#endif
302325
}
303326

0 commit comments

Comments
 (0)