I am writing a windows QT app that needs to launch other apps. If I use the following windows calls everything works fine:
QString qsExePath = "C:\\Program Files (x86)\\Some Company\\SomeApp.exe";
QString qsCommandLine = "";
DWORD dwLastError = 0;
STARTUPINFO startupInfo;
ZeroMemory(&startupInfo, sizeof(startupInfo));
startupInfo.cb = sizeof(startupInfo);
startupInfo.dwFlags = STARTF_USESHOWWINDOW;
startupInfo.wShowWindow = (WORD)1;
PROCESS_INFORMATION processInfo;
ZeroMemory(&processInfo, sizeof(processInfo));
if (CreateProcess((TCHAR*)(qsExePath.utf16()), (TCHAR*)(qsCommandLine.utf16()),
NULL, NULL, FALSE, 0, NULL, NULL,
&startupInfo, &processInfo))
{
CloseHandle(processInfo.hProcess);
CloseHandle(processInfo.hThread);
}
else
{
dwLastError = GetLastError();
}
However, if I use the following QT calls it does not work and fails with QProcess::Unknown Error.
QProcess process;
bool bStarted = process.startDetached(qsExePath);
qDebug() << process.error();
I can get QProcess to work if copy SomeApp.exe to my %TMP% directory and change the qsExePath accordingly, so it is obviously some kind of permissions error. I don’t understand why though… if it were really permissions, shouldn’t my CreateProcess windows call fail?
Your path has spaces in it. You are calling the overloaded version of
QProcess.startDetached()that takes a single parameter, so it interprets that as the complete command line to execute. As such, try wrapping the path in quotes, otherwise it will think that “C:\Program” is the program to execute and everything else are arguments, which would be wrong:Alternatively, call one of the other overloaded versions of
startDetached()and let it work out the necessary quoting internally for you: