Inter-process communication-Anonymous Pipeline

Source: Internet
Author: User

Note: The anonymous pipeline can only be performed between the parent process and the child process. Therefore, the parent process must create the child process through CreateProcess to form a parent-child relationship. Otherwise, the parent-child process relationship cannot be formed.

 

Createpipe creates an anonymous pipeline and returns the read handle and write handle of the pipeline.

Bool createpipe (
Phandle hreadpipe,
Phandle hwritepipe,
Lpsecurity_attributes lppipeattributes,
DWORD nsize
);
The first two parameters are the read handle and write handle of the pipeline.

The third parameter points to the security_attributes pointer and checks whether the returned handle can be inherited by the sub-process. If this parameter is null, the handle cannot be inherited. in most other functions, security_attributes is generally set to null by default, so that the system sets the default security descriptor, and the obtained handle cannot be inherited by the quilt process. however, it cannot be set to null here, because for anonymous pipelines, communication can be performed between parent and child processes. If a child process wants an anonymous pipeline handle, it can only be inherited from the parent pipeline, when a child process inherits the read/write handle of the pipeline from the parent process, it can communicate with the parent process.

The last parameter sets the buffer size of the MPs queue. The value 0 indicates the default value. The default value is provided.

Here

Typedef struct _ security_attributes {
DWORD nlength;
Lpvoid lpsecuritydescriptor;
Bool binherithandle;
} Security_attributes,
* Psecurity_attributes;
The first parameter is the structure length, which is sizeof (security_attributes ).

The second member is the security descriptor. Here, it is set to null so that the system can automatically set it. However, the entire structure cannot be set to null, so the third variable will be set to null,

If the third member is true, it can be inherited by the quilt process.

 

CreateProcess creates a process

Bool CreateProcess (
Lptstr lpapplicationname,
Lptstr lpcommandline,
Lpsecurity_attributes lpprocessattributes, // sets the Security Attribute
Lpsecurity_attributes lpthreadattributes,
Bool binherithandles, // whether the handle can be inherited
DWORD dwcreationflags, // additional identifier for controlling the priority
Lpvoid lpenvironment,
Maid directory,
Lpstartupinfo, // specifies how the new window is implemented
Lpprocess_information lpprocessinformation
);
 

The above function can use the first parameter to pass the name of an executable module. If it is used to load the name of an executable module without a relative path, the program will find it in the current file directory, instead of searching for the module to be executed in the system path. if it cannot be found, a failure is returned. at the same time, it does not automatically add extensions to the executable module.

 

The second parameter is used to pass the parameters of the executable module.

You can also use it to add an executable module. If it cannot be found in the current directory, it will be searched in the system path. If it does not write the extension of the executable module, it automatically expands the extension of the executable file. it is usually used to pass the executable module name and parameters at the same time.

 

 

Code snippet:

Create MPs queue

Define class member variables:

Handle hwrite;

Handle hread;

The constructor initializes these two handles.

Cparentview: cparentview ()

{

// Todo: Add construction code here

Hread = NULL;

Hwrite = NULL;

}

Destructor

Cparentview ::~ Cparentview ()

{

If (hread)

Closehandle (hread );

If (hwrite)

Closehandle (hwrite );

}

 

Void cparentview: onpipecreate ()

{

Security_attributes SA;

SA. binherithandle = true;

SA. lpsecuritydescriptor = NULL;

SA. nlength = sizeof (security_attributes );

If (! Createpipe (& hread, & hwrite, & SA, 0 ))

{

MessageBox ("An error occurred while creating the anonymous MPs queue! ");

Return;

}

Startupinfo Sui;

Process_information PI; // process information structure pointer

/*

Set all members in the structure to 0.

*/

Zeromemory (& Sui, sizeof (startupinfo ));

Sui. cb = sizeof (startupinfo );

Sui. dwflags = startf_usestdhandles;

Sui. hstdinput = hread;

Sui. hstdoutput = hwrite;

// Get the standard error handle

Sui. hstderror = getstdhandle (std_error_handle );

If (! CreateProcess (".. // child // debug // child.exe", null,

True, 0, null, null, & Sui, & PI ))

{

Closehandle (hread );

Closehandle (hwrite );

// Set hread and hwrite to null below to prevent the Destructor from closing the two handles again.

Hread = NULL;

Hwrite = NULL;

MessageBox ("failed to create sub-process! ");

Return;

}

Else

{

Closehandle (PI. hprocess );

Closehandle (PI. hthread );

}

}

 

Read and Write Data from anonymous Pipelines

Use readfile and writefile to write data to and read data from the pipeline

Void cparentview: onpiperead ()

{

// Todo: add your command handler code here

Char Buf [100];

DWORD dwread;

If (! Readfile (hread, Buf, 100, & dwread, null ))

{

MessageBox ("failed to read data! ");

Return;

}

MessageBox (BUF );

}

 

Void cparentview: onpipewrite ()

{

// Todo: add your command handler code here

Char Buf [] = "http://www.sunxin.org ";

DWORD dwwrite;

If (! Writefile (hwrite, Buf, strlen (BUF) + 1, & dwwrite, null ))

{

MessageBox ("failed to write data! ");

Return;

}

}

 

Sub-processes read and write data

Also create member variables

Handle hwrite;

Handle hread;

 

Cparentview: cparentview ()

{

Hread = NULL;

Hwrite = NULL;

}

 

Cparentview ::~ Cparentview ()

{

If (hread)

Closehandle (hread );

If (hwrite)

Closehandle (hwrite );

}

Oninitialupdate () is the first function called after the window is created.

Void cchildview: oninitialupdate ()

{

Cview: oninitialupdate ();

// Handle the standard input and standard output to the sub-process

Hread = getstdhandle (std_input_handle );

Hwrite = getstdhandle (std_output_handle );

}

 

 

Void cchildview: onpiperead ()

{

Char Buf [100];

DWORD dwread;

If (! Readfile (hread, Buf, 100, & dwread, null ))

{

MessageBox ("failed to read data! ");

Return;

}

MessageBox (BUF );

}

 

Void cchildview: onpipewrite ()

{

Char Buf [] = "anonymous Pipeline Test Program ";

DWORD dwwrite;

If (! Writefile (hwrite, Buf, strlen (BUF) + 1, & dwwrite, null ))

{

MessageBox ("failed to write data! ");

Return;

}

}

 

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.