检查Windows上子进程的退出状态

时间:2011-07-28 09:51:56

标签: c windows subprocess glib

gEDA中,我们有一个帮助程序,需要创建一个子进程并检查其退出状态以确保它成功完成。在Linux上,我们使用类似的东西:

#include <glib.h>
#include <sys/wait.h>

static gboolean
build_and_run_command (const gchar *format, ...)
{
  int result, status;
  gchar *args, *standard_error;
  GError *error = NULL;

  /* Set up argument variables */

  if (g_spawn_sync (".",
                    args,
                    NULL,
                    G_SPAWN_SEARCH_PATH | G_SPAWN_STDOUT_TO_DEV_NULL,
                    NULL,
                    NULL,
                    NULL,
                    &standard_error,
                    &status,
                    &error)) {
    result = (WIFEXITED (status) && WEXITSTATUS(status) == 0);
  }

  /* Clean up */

  return result;
}

可以在我们的git存储库中找到full source code for the program

不幸的是,在使用MinGW编译Windows时,我们发现sys/wait.h不存在,WIFEXITEDWEXITSTATUS宏也不存在。使用g_spawn_sync检查正常退出并在Windows上检索退出状态的“正确方法”是什么?谷歌出人意料地无益!

2 个答案:

答案 0 :(得分:0)

在Windows API中,您通常使用CreateProcess创建进程。要获取错误代码(或“成功代码”^^),请查看此处:http://msdn.microsoft.com/en-us/library/ms683189%28v=vs.85%29.aspx

答案 1 :(得分:0)

g_spawn_async_with_pipes()的文档说明了如何在Windows上执行此操作。

相关问题