【问题标题】:CreateProcessA,Wait and ExitCode in C - getting wrong exit codeC 中的 CreateProcessA、Wait 和 ExitCode - 获取错误的退出代码
【发布时间】:2021-03-27 09:37:10
【问题描述】:

我在 C 中使用 CreateProcessA 和 Wait(...) 函数的代码存在问题。一切似乎都工作正常,除了退出代码。它始终为 0。我知道(至少)这部分代码(带有退出代码)是错误的,但我尝试了很多方法来修复它,但没有一个真正起作用。此外,在控制台中显示 PID 时,它们有时不在应有的位置(例如:有 3 行具有相同的 PID,有时还有一个来自其他组的额外 PID,例如:6656 6656 1234 6656 ...)。也许你们知道解决方案。代码如下:

    ...//some argument conditions above
    STARTUPINFO si;
    PROCESS_INFORMATION pi[2];

    memset(&si, 0, sizeof(si));
    memset(&pi, 0, sizeof(pi));
    si.cb = sizeof(si);

    int argument = atoi(argv[1]);   
    int argument1 = argument - 1;
    int argument2 = argument - 2;

    char a1[50];
    sprintf(a1, "file.exe %d", argument1);

    BOOL v1 = CreateProcessA("file.exe", a1, NULL, NULL, FALSE, 0, NULL, NULL, &si, pi+0);


    char a2[50];
    sprintf(a2, "file.exe %d", argument2);

    BOOL v2 = CreateProcessA("file.exe", a2, NULL, NULL, FALSE, 0, NULL, NULL, &si, pi+1);


    
    HANDLE children[2] = { pi[0].hProcess, pi[1].hProcess };
    WaitForMultipleObjects(2, children, 1, INFINITE);
    for (int i = 1; i >= 0; i--)
    {       
        CloseHandle(pi[i].hProcess);
        CloseHandle(pi[i].hThread);
    }

    unsigned long int exit1 = GetExitCodeProcess(children[0], &exit1);
    unsigned long int exit2 = GetExitCodeProcess(children[1], &exit2);
    
    printf("%d \t %d \t %d \t %d \t\n", GetCurrentProcessId(), pi[0].dwProcessId, argument1, exit1);

    
    printf("%d \t %d \t %d \t %d \t\n", GetCurrentProcessId(), pi[1].dwProcessId, argument2, exit2);

    printf("%d \t \t \t %d\n\n", GetCurrentProcessId(), exit1 + exit2);


    return exit1 + exit2;
}

感谢您的帮助。

【问题讨论】:

  • 您将&exit1 作为参数传递,并使用返回值初始化exit1GetExitCodeProcess 返回一个 BOOL 值,失败时为 0,成功时为非零(可能为 1)。它返回失败值 0,因为您已经关闭了进程句柄。
  • 从您的代码中不清楚生成的进程应该返回什么。请创建最小的可重现示例。
  • 那不是因为你在使用它获取退出码之前关闭了句柄吗?

标签: c process operating-system wait createprocess


【解决方案1】:

在这段代码中:

    HANDLE children[2] = { pi[0].hProcess, pi[1].hProcess };
    WaitForMultipleObjects(2, children, 1, INFINITE);
    for (int i = 1; i >= 0; i--)
    {       
        CloseHandle(pi[i].hProcess);
        CloseHandle(pi[i].hThread);
    }

    unsigned long int exit1 = GetExitCodeProcess(children[0], &exit1);
    unsigned long int exit2 = GetExitCodeProcess(children[1], &exit2);

一个问题是exit1exit2 被初始化为GetExitCodeProcess 的返回值。该函数的返回类型为 BOOL,失败时返回 0 (FALSE),成功时返回非零值(推测为 1 (TRUE))。由于进程句柄在前面的for 循环中已经关闭,GetExitCodeProcess 将返回失败值 0。

尝试将其更改为以下内容:

    HANDLE children[2] = { pi[0].hProcess, pi[1].hProcess };
    WaitForMultipleObjects(2, children, 1, INFINITE);
    DWORD exit1, exit2;
    GetExitCodeProcess(children[0], &exit1);
    GetExitCodeProcess(children[1], &exit2);
    for (int i = 1; i >= 0; i--)
    {       
        CloseHandle(pi[i].hProcess);
        CloseHandle(pi[i].hThread);
    }

【讨论】:

  • @sikwakw7 实际上,由于剪切和粘贴错误,它略有错误。我现在已将GetExitCodeProcess(children[0], &exit2); 更正为GetExitCodeProcess(children[1], &exit2);。 (它从错误的进程中获取了 exit2 值!)
  • 哦,我什至没有注意到,但做得对,我看到了解决方法
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-18
  • 2014-11-15
  • 2018-11-04
  • 1970-01-01
相关资源
最近更新 更多