Anonymous pipeline Learning

Source: Internet
Author: User
Tags readfile


Pipe is a shared memory area used for inter-process communication. The process for creating an MPS queue is called the MPs Queue Server, and the process connecting to the MPs queue is called the MPs queue client. One process writes information to the pipeline, while the other process reads the information from the pipeline. The anonymous pipeline is a character-based one-way channel, which is very suitable for locating the output of one program to the input of another program.

First look at the sub-process code:

#include <iostream>#include <string>using namespace std;int main(int argc,wchar_t* argv[]){wstring strSource;wcin >> strSource;if (!strSource.empty()){for (wstring::iterator iter = strSource.begin();iter != strSource.end();++iter){*iter ^= 23;}}wcout << strSource << endl;return 0;}

The input data is output after 23 is exclusive,

It is required to change the program input to read from the file and output the result to a file.

Steps:

1. Create two pipelines. One is the input pipeline and the other is the output pipeline.

2. Each pipe has a read handle and a write handle. Set the standard input and output of the process as the read handle of the input pipeline and the write handle of the input pipeline. (Setstdhandle)

3. Copy the write handle of the input pipeline and the read handle of the output pipeline respectively. (Duplicatehandle)

4. Specify that sub-processes can be inherited and created.

5. Read and Write pipelines.

# Include <windows. h ># include <iostream> using namespace STD; const uint buf_size = 1024; bool readwritepipe (char * pstrfilename, handle hpipe, bool breadpipe); int main (INT argc, char * argv []) {If (argc! = 4) {wcout <L "Usage: main.exe child.exe inputfile outputfile" <Endl; return 0;} security_attributes SA; SA. nlength = sizeof (security_attributes); SA. binherithandle = true; SA. lpsecuritydescriptor = NULL; handle hstdinre, hstdinwr, hstdinwrdup, hstdoutre, hstdoutwr, hstdout1_p, hsavein, hsaveout; // create the input pipe if (! Createpipe (& hstdinre, & hstdinwr, & SA, 0) {wcerr <L "create input pipe failed. the last error is: "<getlasterror () <Endl; return 0;} hsavein = getstdhandle (std_input_handle); If (! Setstdhandle (std_input_handle, hstdinre) {wcerr <L "redirecting stdin failed" <Endl; return 0 ;}// construct the duplicate if (! Duplicatehandle (getcurrentprocess (), hstdinwr, getcurrentprocess (), & hstdinwrdup, 0, false, success) {wcerr <L "duplicatehandle failed" <Endl; return 0 ;} closehandle (hstdinwr); // create an output pipeline if (! Createpipe (& hstdoutre, & hstdoutwr, & SA, 0) {wcerr <L "create input pipe failed. the last error is: "<getlasterror () <Endl; return 0;} hsaveout = getstdhandle (std_output_handle); If (! Setstdhandle (std_output_handle, hstdoutwr) {wcerr <L "redirecting stdout failed" <Endl; return 0 ;}/ * 'constructs a replica of the output pipeline read handle */If (! Duplicatehandle (getcurrentprocess (), hstdoutre, getcurrentprocess (), & hstdout1_p, 0, false, success) {wcerr <L "duplicatehandle failed" <Endl; return 0 ;} closehandle (hstdoutre); // create a sub-process process_information Pi = {0}; startupinfoa ST = {0}; ST. CB = sizeof (startupinfoa); If (! Createprocessa (argv [1], null, true, null, & St, & PI) {wcerr <L "CreateProcess failed, the error code is: "<getlasterror () <Endl; return 0;} closehandle (Pi. hprocess); closehandle (Pi. hthread); If (! Setstdhandle (std_input_handle, hsavein) {wcerr <L "re-redirecting stdin failed" <Endl; return 0 ;}if (! Setstdhandle (std_output_handle, hsaveout) {wcerr <L "re-redirecting stdout failed" <Endl; return 0 ;}// read the file, write if (! Readwritepipe (argv [2], hstdinwrdup, false) {wcerr <L "An error occurred while reading files and writing data to the input Pipeline" <Endl; return 0;} closehandle (hstdinwrdup ); closehandle (hstdoutwr); // read the replica of the read handle of the output pipeline and write the file if (! Readwritepipe (argv [3], hstdoutredup, true) {wcerr <L "An error occurred while reading the output pipeline and writing the file" <Endl; return 0;} closehandle (hstdinre ); return 0;} bool readwritepipe (char * pstrfilename, handle hpipe, bool breadpipe) {handle hinputfile = createfilea (pstrfilename, generic_read | generic_write, 0, null, open_always, limit, 0 ); if (invalid_handle_value = hinputfile) {wcerr <L "open" <pstrfilename <"file error, the erro R code is: "<getlasterror () <Endl; return false;} DWORD dwbyteread = 0, dwbytewrite = 0; char strbuf [buf_size] = {0 }; bool bsuccess = false; For (;) {If (breadpipe) {// write data to the file bsuccess = readfile (hpipe, strbuf, buf_size, & dwbyteread, null ); if (! Bsuccess | dwbyteread = 0) break; bsuccess = writefile (hinputfile, strbuf, buf_size, & dwbytewrite, null); If (! Bsuccess) {break ;}} else {// write the read file content to the input pipeline bsuccess = readfile (hinputfile, strbuf, buf_size, & dwbyteread, null); If (! Bsuccess | dwbyteread = 0) break; bsuccess = writefile (hpipe, strbuf, buf_size, & dwbytewrite, null); If (! Bsuccess) Break ;}} closehandle (hinputfile); Return true ;}

It is more appropriate to separate the functions of reading and writing files from the MPs queue. I am too lazy to change the function. I used a bool variable to control whether to read files or MPs queues. Command Line program parameters: pipe.exe pipe_test.exe input.txt output.txt. The project has been packaged. Pipe.exeis the parent process, and pipe_test.exe is a sub-process. Http://download.csdn.net/detail/tornodo/4224317

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.