我正在从事一个包含线程和进程的项目。我打开多线程,并为每个线程打开一个运行 exe 文件的进程。 如果程序以非 0 的返回值结束,它就会崩溃,并且应该打印返回值。为了获得返回值,我使用 WIN API 函数 GetExitCodeProcess() 我的问题是这个函数总是返回值 1,即使程序应该崩溃,但为什么呢? 这是我的相关代码:
static DWORD WINAPI RunningTests(test_s *test)
{
PROCESS_INFORMATION procinfo;
DWORD waitcode;
DWORD exitcode;
int status = 0, crashed = 0;
char cmdLineString[MAX_NUMBER_OF_CHARS_IN_CMD_LINE]="";
char *cmdLineStringPtr = cmdLineString;
(test)->isCrashed = 0;
CreateCmdLine((*test).testExePath, &cmdLineStringPtr);
status = CreateProcessSimple(_T(cmdLineString), &procinfo);
if (status == -1)
{
return 1;
}
waitcode = WaitForSingleObject(procinfo.hProcess,
TIME_UNTIL_TIMED_OUT_IN_MILLISEC);
if (waitcode == WAIT_TIMEOUT) /* Process is still alive */
{
strcpy((*test).status, "Timed Out");
CloseHandle(procinfo.hProcess);
CloseHandle(procinfo.hThread);
return 0;
}
crashed = GetExitCodeProcess(procinfo.hProcess, &exitcode);
if (crashed == 0) /* Process is crashed */
{
strcpy((*test).status, "Crashed");
(*test).isCrashed = 1;
(*test).returnedCrashedValue = exitcode;
CloseHandle(procinfo.hProcess);
CloseHandle(procinfo.hThread);
return 0;
}
CloseHandle(procinfo.hProcess);
CloseHandle(procinfo.hThread);
return CompareFiles(&test);
}
最佳答案
您应该查看退出代码,而不是崩溃;
Return Value
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. To get extended error information, call GetLastError.
关于c - 获取退出代码总是返回 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53401165/