简体   繁体   English

匿名管道的PeekNamePipe()的替代方法— WIN32

[英]alternatives to PeekNamePipe() with anonymous pipe — WIN32

I have a program that launch another console program as its child process and communicate with it using anonymous pipe. 我有一个程序可以启动另一个控制台程序作为其子进程,并使用匿名管道与之通信。 I redirected both its stdin and stdout. 我重定向了它的标准输入和标准输出。 The pseudocode is like: 伪代码如下:

// Create pipes for stdin and stdout
CreatePipe(&std_in_rd, &std_in_wr, NULL, 0);
CreatePipe(&std_out_rd, &std_out_wr, NULL, 0);

// redirection
startup_info.hStdOutput = std_out_wr;
startup_info.hStdInput = std_in_rd;

// create the process
CreateProcess(...);

// send command to the child process.
WriteFile(hWriteStdin, ...);

// receive feedback from the child process.
ReadFile(hReadStdout, ...);

But, the child process's processing the commands needs time, and I don't know how much time I should wait to get its output. 但是,子进程在处理命令时需要时间,而且我不知道我应该等待多少时间才能获得其输出。 I used a loop that calls PeekNamedPipe to examine whether I can read from the pipe, but this method is not good for it consums a lot CPU. 我使用了一个调用PeekNamedPipe的循环来检查是否可以从管道中读取数据,但是这种方法不利于CPU的使用。

The child process was not written by me and I can't modify its code. 子进程不是由我编写的,我无法修改其代码。

How can I get informed when the child process has finished writing, like a hook? 当子进程完成写操作时,如钩子一样,如何获得通知?

Thanks. 谢谢。

您应该尝试使用ReadFileEx()从管道异步读取。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM