void WriteToPipe(void)
// Read from a file and write its contents to the pipe for the child's STDIN.
// Stop when there is no more data.
{
DWORD dwRead, dwWritten;
CHAR chBuf[BUFSIZE];
BOOL bSuccess = FALSE;
char * name = malloc(100);
fgets(name, 100, stdin);
bSuccess = WriteFile(g_hChildStd_IN_Wr, name, 10, &dwWritten, NULL);
if (!bSuccess)
ErrorExit("");
}
void ReadFromPipe(void)
// Read output from the child process's pipe for STDOUT
// and write to the parent process's pipe for STDOUT.
// Stop when there is no more data.
{
DWORD dwRead, dwWritten;
CHAR chBuf[BUFSIZE];
BOOL bSuccess = FALSE;
HANDLE hParentStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
bSuccess = ReadFile(g_hChildStd_OUT_Rd, chBuf, BUFSIZE, &dwRead, NULL);
if (!bSuccess || dwRead == 0)
return 100;
bSuccess = WriteFile(hParentStdOut, chBuf,
dwRead, &dwWritten, NULL);
if (!bSuccess)
return 101;
}
主要(不完整):
while (1)
{
Sleep(500);
ReadFromPipe();
WriteToPipe();
}
我正在尝试将cmd作为子进程打开并将父输入传递给子STDIN流,而不是打印子进程的STDOUT。
正如你所看到的,它第一次起作用,但后来我得到了“更多?”从子进程(cmd)返回,然后它等待输出卡住。
为什么我会“更多”?回来?
那是什么“更多?”
答案 0 :(得分:2)
我找到的父输入重定向的唯一方法是创建一个额外的线程。常见的算法是:
hPipeRead
,hPipeWrite
)hPipeRead
GetStdHandle(STD_INPUT_HANDLE)
的新线程,并立即将读缓冲区写入hPipeWrite
。当stdInput结束时线程完成。