Compilation Tutorials: Piping

Source: Internet
Author: User

This talk will explore the pipeline and see what it is and what it is for. To make it more interesting, I'll illustrate this technique with how to change the background color and text color of the Edit control.

Theory:

Pipelines, as the name suggests, have two-port channels. You can use pipelines to exchange data between processes, in the same process, like a hand-held wireless telephone. Give one end of the pipe to the other and he can communicate with you through the pipe.

There are two kinds of pipes, known pipes and anonymous pipes. Anonymous pipes are pipes with no names, which means you don't need to know their names when you use them. The famous pipe, on the contrary, must know its name before using it.

can also be classified according to the characteristics of the pipeline, that is one-way or two-way. A one-way pipeline in which data can be moved in one direction and from end to the other, while two-way pipe data is freely exchanged between the two ends. Anonymous pipes are usually one-way, and well-known pipes are usually two-way. A well-known pipe is commonly used in a network environment where a server contacts multiple clients.

This talk will cover the anonymous pipeline in detail. The primary purpose of an anonymous pipeline is to act as a link between the parent process and the child process, and the child process. Anonymous pipes are useful when dealing with console problems. A console application is a WIN32 application that uses the console as input and output. A console is like a DOS window. But the console application is indeed a 32-bit application that can use GUI functions like other graphics programs, except that it happens to use the console.

The console application has three standard handles for input and output, which are standard input, standard output, and standard error handles. Standard input is used to read or take information from the console and standard output is used to write or print information to the console. Standard errors are used to report errors that the output cannot be redirected.

The console application can obtain these three handles by calling the function GetStdHandle. A graphics application does not have a console, and if you call GetStdHandle it returns an error, and if you do use the console, you can call AllocConsole to allocate a new console for use, but when the processing is complete, don't forget to call Freeconsole To release the console.
The most commonly used function of an anonymous pipeline is the standard input and standard output of the redirected child process. The parent process can be either a console or a graphics program, and the child process must be a console application. As we all know, console applications use standard input and output handles. To redirect the input output, replace the standard handle with a handle pointing to one end of the pipe. The console application will not know that we use a handle that points to either end of the pipe, and it treats the handle as a standard handle. By borrowing object-oriented terminology, this is one of polymorphism. This method is useful because the subprocess does not require any changes.

Another point about what the console application should grasp is where it gets the standard handle. When a console application is created, the parent process has two choices: Create a new console for the child process or allow the child process to inherit its own console. If you use the latter, the parent process itself must be a console application, or if it is a GUI application, it must first call AllocConsole and assign a console.

By calling CreatePipe to create an anonymous pipe, its prototype is:


CreatePipe Proto Preadhandle:dword, \
Pwritehandle:dword,\
Ppipeattributes:dword,\
Nbuffersize:dword

Preadhandle a two-character pointer variable that points to a handle to the pipe read end.

Pwritehandle Two-word pointer variable, pointing to a handle to a pipe write end

Ppipeattributes a two-character pointer variable, pointing to a security_attributes structure that determines whether a read-write handle can be inherited by the quilt process

Nbuffersize recommends the size of the buffer to be left to the user, which is only a recommended value and can be NULL to use the default value

Zero if the function call successfully returns a Non-zero value. After a successful call, you get two handles, one to the read end of the pipe, and the other to the write end of the pipe. Now I'm going to focus on the steps needed to redirect the standard output of the child console program to my own process. Note that my method differs from the example of Borland's API reference. The Win32 API reference assumes that the parent process is a console application, so the child process can inherit its standard handle. In most cases, however, we need to redirect the output from the console application to the GUI application.

Create an anonymous pipe using createpipe, and do not forget to set the security_attributes struct member binheritable to true so that the handle can be inherited.

The function that is now ready to create the process, that is, the CreateProcess parameter, is the only one that can mount the child console application. Startupinfo is an important structure that determines the appearance of the main window when a child process appears, and it is critical to our goal. Through this structure, you can hide the main window and pass the pipe handle to the child process.

The following are the members that must be filled in:

The size of CB STARTUPINFO structure

dwflags a binary flag bit that determines which members of the structure are valid and whether the main window is displayed or hidden. Use the combination of Startf_useshowwindow and startf_usestdhandles in our program

Hstdoutput and hstderror you want the standard output and standard error handles used by the subprocess, for us, we will use the write end of the pipe as the standard output and error of the subprocess. So when a subprocess sends a message to a standard output or standard error, it actually passes the information through the pipe to the parent process

Wshowwindow determines whether the main window is displayed or hidden. We do not want to display the main window of the subprocess, so put the member into Sw_hide

Call CreateProcess to create the child process, but the child process is still not active after the call succeeds. It is loaded into memory but does not run immediately.

It is also necessary to close the write end of the pipe in the parent process. This is because the parent process does not use a write handle to the pipe, and if a pipe has two writes, it does not work, so we must close the pipe's write end before we can read data from the pipeline. However, you cannot shut down before calling CreateProcess, or the pipe will be broken. You should turn off the write end of the pipe before CreateProcess just returns and reads the data.

You can now read the data through the function ReadFile on the channel's read end. By using ReadFile, you can make the child process run. It will start executing, and when it writes data to standard output (which is actually the write end of the pipe), the data is sent to the read end of the pipe. You should keep calling ReadFile until it returns a value of 0, which means there is no more data to read. You can do any processing of the data that is read from the pipe, and it is shown in our example in the Edit control.
Remember to close the read handle of the pipe when you are done with it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.