检查 Windows 上子进程的退出状态
在 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;
}
: hb=HEAD" rel="nofollow">程序的完整源代码可以在我们的 git 存储库中找到。
不幸的是,当使用 MinGW 编译 Windows 时,我们发现 sys/wait.h
不存在,WIFEXITED
或 WEXITSTATUS
也不存在> 宏。在 Windows 上使用 g_spawn_sync 检查正常退出并检索退出状态的“正确方法”是什么?谷歌出人意料地毫无帮助!
In gEDA, we have a helper program that needs to create a subprocess and check its exit status to ensure that it completed successfully. On Linux, we use something similar to:
#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;
}
The full source code for the program can be found in our git repository.
Unfortunately, when compiling for Windows using MinGW, we have discovered that sys/wait.h
doesn't exist, and neither do the WIFEXITED
or WEXITSTATUS
macros. What is the "right way" to check for a normal exit and to retrieve the exit status on Windows using g_spawn_sync
? Google has been surprisingly unhelpful!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
g_spawn_async_with_pipes 的文档()
解释了如何在 Windows 上执行此操作。The documentation for
g_spawn_async_with_pipes()
explains how to do it on Windows.在 Windows API 中,您通常使用 CreateProcess 创建进程。要获取错误代码(或“成功代码”^^),请查看此处:http://msdn.microsoft.com/en-us/library/ms683189%28v=vs.85%29.aspx
In the Windows API you normally create processes using CreateProcess. To get the error code (or "success code" ^^) take a look here: http://msdn.microsoft.com/en-us/library/ms683189%28v=vs.85%29.aspx