c - 获取退出代码总是返回 1

标签 c windows exit-code

我正在从事一个包含线程和进程的项目。我打开多线程,并为每个线程打开一个运行 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/

相关文章:

c - 如果没有崩溃,我真的应该担心修复 null defres 吗?

windows - Makefile 未设置 GOOS 环境变量

c# - 从 C# 中的控制台应用程序退出

docker - docker-compose 退出代码的含义?

c - 在给定链表中添加包含整数值的替代节点?

c++ - 使用configure文件生成makefile

java - 在 Java/PHP/Python 框架内运行已编译的 C/C++ 代码中的算法?

windows - Chrome 在哪里存储在 Windows 7 上使用 Amazon-Cloud Extension 下载的离线书籍?

windows - 打开命令外壳并执行命令

python - 在 Python 3 中,使用 Pytest,我们如何测试退出代码 : exit(1) and exit(0) for a python program?