获取退出代码始终返回1

时间:2018-11-20 20:40:17

标签: c windows exit-code

我正在一个包含线程和进程的项目中。我打开多线程,并为每个线程打开一个运行exe文件的proess。 如果程序返回的值不为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);              
}

1 个答案:

答案 0 :(得分:1)

您应该查看退出代码,而不是崩溃;

  

返回值

     

如果函数成功,则返回值为非零。

     

如果函数失败,则返回值为零。获取扩展错误   信息,请致电GetLastError。