1
0
mirror of https://github.com/d47081/qBittorrent.git synced 2025-01-11 23:37:59 +00:00

Merge pull request #5408 from Chocobo1/run_ext

Fix external program command too long error in Windows
This commit is contained in:
sledgehammer999 2016-07-17 10:18:18 -05:00 committed by GitHub
commit 53e3f8a239

View File

@ -261,9 +261,16 @@ void Application::runExternalProgram(BitTorrent::TorrentHandle *const torrent) c
#if defined(Q_OS_UNIX) #if defined(Q_OS_UNIX)
QProcess::startDetached(QLatin1String("/bin/sh"), {QLatin1String("-c"), program}); QProcess::startDetached(QLatin1String("/bin/sh"), {QLatin1String("-c"), program});
#elif defined(Q_OS_WIN) // test cmd: `echo "%F" > "c:\ab ba.txt"` #elif defined(Q_OS_WIN) // test cmd: `echo "%F" > "c:\ab ba.txt"`
program.prepend(QLatin1String("cmd.exe /C ")); static const QString cmdPath = []() -> QString {
if (program.size() >= MAX_PATH) { WCHAR systemPath[64] = {0};
logger->addMessage(tr("Torrent: %1, run external program command too long (length > %2), execution failed.").arg(torrent->name()).arg(MAX_PATH), Log::CRITICAL); GetSystemDirectoryW(systemPath, sizeof(systemPath) / sizeof(WCHAR));
return QString::fromWCharArray(systemPath) + QLatin1String("\\cmd.exe /C ");
}();
program.prepend(QLatin1String("\"")).append(QLatin1String("\""));
program.prepend(cmdPath);
const uint cmdMaxLength = 32768; // max length (incl. terminate char) for `lpCommandLine` in `CreateProcessW()`
if ((program.size() + 1) > cmdMaxLength) {
logger->addMessage(tr("Torrent: %1, run external program command too long (length > %2), execution failed.").arg(torrent->name()).arg(cmdMaxLength), Log::CRITICAL);
return; return;
} }