1

例如,在 linux 中,下面的命令

$ firstProgram | secondProgram

将 firstProgram 的输出作为 secondProgram 的输入

使它在 linux 中发生的 C 中的基本代码是

#include <unistd.h>
.
.
.
int fd[2];
forkStatus = fork();
if (status == 0)
{
  close(1);
  dup(fd[1]);
  close(fd[1]);
  close(fd[0]);
  execv("firstProgram",...);
}
forkStatus = fork();
if (status == 0)
{
  close(0);
  dup(fd[0]);
  close(fd[1]);
  close(fd[0]);
  execv("secondProgram",...);
}
close(fd[1]);
close(fd[0]);

我需要在 Windows 中做类似的事情。谢谢

4

2 回答 2

3

请参阅 Win32CreatePipe()创建匿名管道。此示例(标题为“使用重定向的输入和输出创建子进程”)展示了如何在 Win32 中复制代码。

于 2010-12-13T13:16:37.577 回答
0

在 linux 版本中,您基本上是在重定向输入和输出。这可以使用本机 Win32 API 或者如果 .NET 是允许的 System.* 库来完成。您可以在 MSDN http://msdn.microsoft.com/en-us/library/ccf1tfx0.aspx上找到更多示例

于 2010-12-13T13:18:48.100 回答