简体   繁体   中英

CreateProcess can start a process, but QProcess cannot… why?

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:

QString qsExePath = "\"C:\\Program Files (x86)\\Some Company\\SomeApp.exe\"";
bool bStarted = process.startDetached(qsExePath);

Alternatively, call one of the other overloaded versions of startDetached() and let it work out the necessary quoting internally for you:

QString qsExePath = "C:\\Program Files (x86)\\Some Company\\SomeApp.exe";
bool bStarted = process.startDetached(qsExePath, QStringList());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM