我正在尝试启动一个应用程序,然后监视它直到它关闭。我正在使用 ShellExecuteEX 和 GetExitCodeProcess 并且有几个问题。
下面的代码在调用 GetExitCodeProcess 时会导致分段错误。如果我更改 shellInfo.fMask = NULL,它不会出现 seg 错误,但我收到一个错误,提示 Invalid Handle。
Notepad.exe 会启动。
QString executeFile("notepad.exe");
// Conversion QString to LPCTSTR
wchar_t* tempEF = new wchar_t[executeFile.size()+1];
int tempEFTerminator = executeFile.toWCharArray(tempEF);
tempEF[tempEFTerminator] = 0;
LPDWORD exitCode = 0;
SHELLEXECUTEINFO shellInfo;
shellInfo.cbSize = sizeof(SHELLEXECUTEINFO);
shellInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
shellInfo.hwnd = NULL;
shellInfo.lpVerb = NULL;
shellInfo.lpFile = tempEF;
shellInfo.lpParameters = NULL;
shellInfo.lpDirectory = NULL;
shellInfo.nShow = SW_MAXIMIZE;
shellInfo.hInstApp = NULL;
if(ShellExecuteEx(&shellInfo))
{
if(!GetExitCodeProcess(shellInfo.hProcess, exitCode))
{
DWORD lastError = GetLastError();
LPTSTR lpMsgBuf;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS , NULL, lastError, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&lpMsgBuf, 0, NULL);
QString errorText = ("failed with error: " + QString::number(lastError) + QString::fromWCharArray(lpMsgBuf));
}
}