Use of anonymous pipelines under windows

Source: Internet
Author: User

Windows sometimes need to use multi-process programming, sometimes because a complex process needs to open a new process for processing, or just need to call an existing EXE, and so on, when creating multiple processes, communication between processes is an important issue, The following discusses how to use anonymous pipes to communicate in a parent-child process.

Usage: The parent process uses CreateProcess to communicate with the child process.

Pipes are very similar to pipes in the real world, where they are loaded from one end to the other (data), which can be removed from the other end through the pipe. Here, both ends of the pipeline are the two processes on the computer that need to communicate, one process sends the data to the pipeline, and the other process reads the data from the pipeline, thus completing the interprocess communication.

First question: Since two processes are communicating through a pipeline, the two processes must have a pipeline, and when the parent process creates a pipeline, how does the child process get the pipeline created by the parent process, and if the pipe is the anonymous pipe discussed here, The child process can then create the pipeline through an integrated approach to the parent process.

Because the parent process creates the child process, the child process does not integrate the address space of the parent process (each process has its own separate address space), so the child process does not have direct access to the data of the parent process, meaning that the two processes cannot communicate directly to different threads in the same process, such as variables. However, when the child process is created, it integrates the handle that the parent process creates that can be inherited, so that when the parent process creates the pipe (actually, or gets the handle to the pipeline that was created), by making this pipe handle to be integrated, the child process is created, This handle will have the opportunity to integrate the quilt process (in fact, you need to specify the inheritance attribute when calling the CreateProcess function to create the child process, as described below).

Create a pipeline function as follows:

BOOL __stdcal CreatePipe (
Phandle Hreadpipe,
Phandle Hwritepipe,
Lpsecurity_attributes Lppipeattributes,
DWORD nSize
);

Where Hreadpipe,hwritepipe These two handles are the write handle and read handle of the created pipeline, as can be seen from the name, we use the hwritepipe handle to write data to the pipe, use the hreadpipe handle to read the data from the pipe, nsize specify the size of the pipe to be created If 0, the default value provided by Windows is used.

The third parameter is a struct of type lpsecurity_attributes, which is defined as follows:

typedef struct _SECURITY_ATTRIBUTES {
DWORD nlength;
LPVOID Lpsecuritydescriptor;
BOOL bInheritHandle;
} security_attributes;

Nlength is always set to sizeof (Security_attributes), Lpsecuritydescriptor is a security descriptor and can be simply set to NULL, using the default settings, regardless of the discussion here, The third parameter, bInheritHandle, is set to true to indicate that the kernel object created with this security attribute (Security_attributes) can be inherited by the quilt process, and, similarly, if the bInheritHandle is false, The child process cannot inherit the corresponding handle created by the parent process and cannot access the handle.

Here, the first problem is solved. Creates a pipe handle in the parent process, creates a Lppipeattributes member by setting the parameter bInheritHandle to true so that the child process can inherit the handle created by the parent process, then how does the child process use the handle created by the parent process? Obviously it cannot directly get any variables of the parent process, then there is a second problem, the handle is passed.

Before discussing how a parent process can pass a handle to a child process, it is necessary to look at the function that created the child process:

BOOL __stdcal CreateProcess (
LPCSTR Lpapplicationname,
LPSTR lpCommandLine,
Lpsecurity_attributes Lpprocessattributes,
Lpsecurity_attributes Lpthreadattributes,
BOOL bInheritHandles,
DWORD dwCreationFlags,
LPVOID Lpenvironment,
LPCSTR Lpcurrentdirectory,
Lpstartupinfoa Lpstartupinfo,
Lpprocess_information lpprocessinformation
);

Where Lpapplicationname sets the name of the child process, lpCommandLine sets the parameters of the subprocess, Lpprocessattributes and Lpthreadattributes sets the security of the child process, dwCreationFlags how the child process will be created, Lpenvironment set the environment block of the subprocess, Lpcurrentdirectory set the current directory of the child process, these are not related to this topic, and are not explained. For specific usage, refer to the relevant information. During the process creation process, this function provides the various parameters required for process creation, where the binherithandles, in this topic is critical, if you want the child process to be able to inherit the handle of the parent process, you need to set bInheritHandles to true, so that In a child process, you can access the handles that can be inherited from the parent process, and the child process is still inaccessible to those handles that cannot be inherited from the parent process. If bInheritHandles is false, the child process cannot access the handle of the parent process. The handle created by the parent process can be passed to the child process through the lpCommandLine parameter, and the following is an example of how to create a file handle:

Parent process: HANDLE file;

... ...

CreateProcess ("Child.exe", (char*) &file,0,0,true,null,0,0,&startupinfo,&processinformation);

After the creation succeeds, the child processes:

HANDLE cFile = (HANDLE) (*argv[0]);

In this way, a file handle passed by the parent process can be obtained in the child process to read and write to the file. Here is the communication between the parent and child processes, and we pass the handle in another way.

CreateProcess parameters in the Lpstartupinfo there are many members, not listed here, we only look at the three we need: Hstderror,hstdoutput,hstdinput, which is the standard error output handle, the standard output handle, The standard input handle. The three handles of the child process can be specified in the parent process, and the parent process can then use the handle to communicate by paying the handle to the three handle by createpipe the handle that was generated by the child process, and then, in the child process, taking the standard handle out of the handle that the parent process has passed to it:

The possible brief pseudocode is as follows:

Parent process:

CreatePipe (&hread, &hwrite, &saattr, 0);

Startupinfo SSI;

Ssi.hstderror = Hwrite;
Ssi.hstdoutput = Hwrite;

CreateProcess ("Child.exe", handlebuffer,0,0,true,null,0,0,&ssi,&processinformation);

Readmessage (Hread);

Child process:

HANDLE hstdout = GetStdHandle (Std_output_handle);

Writemessage (hStdOut);

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.