The example in this topic demonstrates how to create a child process using the CreateProcess function from a console process. It also demonstrates a technique for using anonymous pipes to redirect the child process's standard input and output handles. Note that named pipes can also be used to redirect process I/O.
The CreatePipe function uses the SECURITY_ATTRIBUTES structure to create inheritable handles to the read and write ends of two pipes. The read end of one pipe serves as standard input for the child process, and the write end of the other pipe is the standard output for the child process. These pipe handles are specified in the STARTUPINFO structure, which makes them the standard handles inherited by the child process.
The parent process uses the opposite ends of these two pipes to write to the child process's input and read from the child process's output. As specified in the STARTUPINFO structure, these handles are also inheritable. However, these handles must not be inherited. Therefore, before creating the child process, the parent process uses the SetHandleInformation function to ensure that the write handle for the child process's standard input and the read handle for the child process's standard input cannot be inherited. For more information, see Pipes .
The following is the code for the parent process. It takes a single command-line argument: the name of a text file.
The following is the code for the child process. It uses the inherited handles for STDIN and STDOUT to access the pipe created by the parent. The parent process reads from its input file and writes the information to a pipe. The child receives text through the pipe using STDIN and writes to the pipe using STDOUT. The parent reads from the read end of the pipe and displays the information to its STDOUT.
http://msdn.microsoft.com/zh-cn/library/ms682499.aspx
本文介绍了一种通过创建匿名管道来实现父进程与子进程间标准输入和输出重定向的技术。父进程创建可继承的管道句柄,并设置句柄属性以确保句柄不会被子进程继承。接着创建子进程并传递管道句柄。父进程从文件读取内容写入子进程的标准输入,同时从管道读取子进程的标准输出。
2686

被折叠的 条评论
为什么被折叠?



