C + + executes an EXE file, anonymous pipe

Source: Internet
Author: User
Tags function prototype readfile

The operation of the operating system is to use pipelines. Pipe is actually a piece of shared memory used for interprocess communication, the process of creating a pipeline is called a pipe server, and the process connecting to a pipeline is a pipe client. Once a process has written data to the pipeline, another process can read it from the other end of the pipeline.  An anonymous pipeline (Anonymous Pipes) is an unnamed pipe that transmits data one way between the parent and child processes, and can only be used on the local computer and not for communication between networks. Anonymous pipeline Implementation details the anonymous pipeline is created by the CreatePipe () function, which returns two handles when an anonymous pipe is created: a pipe read handle and a pipe write handle. The function prototype for CreatePipe () is: BOOL createpipe (Phandle hreadpipe,//Pointer to read handle phandle hwritepipe,//Pointer to write handle Lpsecurity_attri  Butes lppipeattributes,//Pointer to the security attribute DWORD nSize//pipe size); The handles pointed to by Hreadpipe and Hwritepipe can be accessed in a read-only, write-only manner. Anonymous pipes do not support asynchronous read and write operations, which means that ReadFileEx () and WriteFileEx () are not used in anonymous pipes, and lpoverlapped parameters in ReadFile () and WriteFile () are ignored. The anonymous pipe exits after both the read and write handles are closed, and the CloseHandle () function can be called in the process to close the handle. 1. If you want only the result of the child process, you can create only one pipe and then ReadFile () to get the output.  as follows: Startupinfo si;  Process_information Pi;  Char readbuf[100];  DWORD Readnum; HANDLE Hread; Pipe reading handle HANDLE hwrite; Pipe Write handle BOOL BRet = CreatePipe (&hread, &hwrite, NULL, 0);  Create an anonymous pipe if (BRet = = TRUE) printf ("Successfully created anonymous pipe!\n");   else printf ("Create anonymous pipe failed with error code:%d\n", GetLastError ()); Get the current standard output of this process HANDLE htemp = GetStdHandle (Std_output_handle);   Set standard output to anonymous pipe setstdhandle (Std_output_handle, hwrite); Getstartupinfo (&SI); Gets the STARTUPINFO structure information for this process bRet = CreateProcess (null, "Client.exe", NULL, NULL, TRUE, NULL, NULL, NULL, &SI, &PI); Create Child process Setstdhandle (Std_output_handle, htemp);   Restore the standard output of this process if (BRet = = TRUE)//Input information printf ("Successfully created sub-process!\n");    else printf ("Create Child process failed, error code:%d\n", GetLastError ()); CloseHandle (Hwrite);     Close the Write handle//read pipeline until the pipe closes while (ReadFile (Hread, Readbuf, &readnum, NULL)) {Readbuf[readnum] = ' + ';    printf ("read%d bytes of data from pipe [%s] \ n", Readbuf, Readnum);    } if (GetLastError () = = error_broken_pipe)//Output information printf ("pipe quilt process off \ n"); else printf ("Read data error, error code:%d\n", GetLastError ());  2. If you want to enter data into a child process, create a pipeline for the child process's standard input as well. Use the WriteFile () input. #include "Windows.h" #include "stdio.h" void Main () {security_attributes sa,sa2; HANDLE Hinputread,hinputwrite; HANDLE houtputread,houtputwrite; 
sa.nlength = sizeof (security_attributes);  Sa.lpsecuritydescriptor = NULL;    Sa.binherithandle = TRUE; if (!  CreatePipe (&houtputread,&houtputwrite,&sa,0)) {printf ("Error on CreatePipe1"); return;}  sa2.nlength = sizeof (security_attributes); Sa2.lpsecuritydescriptor = NULL;
Sa2.binherithandle = TRUE; if (!  CreatePipe (&hinputread,&hinputwrite,&sa2,0)) {printf ("Error on CreatePipe2");  Return }
Startupinfo si;  Process_information Pi;  SI.CB = sizeof (STARTUPINFO);  Getstartupinfo (&SI); Si.hstderror = Houtputwrite; Si.hstdoutput = Houtputwrite; The write handle is given a standard output (or standard error) handle si.hstdinput = Hinputread;  When the parent process sends data to the child process, the read handle of the pipe is given a standard input handle with setstdhandle ()///And the write handle of the pipe is given a standard output (or standard error) handle with Setstdhandle () when the data is received from the child process. The parent process can then call the process creation function CreateProcess () to generate the child process. If the parent process is sending data to a child process, the parent process can call WriteFile ()//write the data to the pipe (pass the pipe write handle to the function), and the child process calls GetStdHandle ()//To get the read handle of the pipe, The handle is passed into the ReadFile () and the data is read from the pipeline.  Si.wshowwindow = Sw_hide; Si.dwflags = Startf_useshowwindow |  Startf_usestdhandles;  DWORD Dwwritten; if (! CreateProcess (NULL, "C:\\windows\\system32\\cmd.exe", Null,null,true,null,null,null,&si,&pi)) {printf ("  Error on CreateProcess ");  Return }
CloseHandle (Hinputread);  CloseHandle (Houtputwrite);  Char szinput[20] = "dir\r\n";  The parent process sends data to the child process, entering WriteFile (Hinputwrite, Szinput, strlen (szinput), &dwwritten, NULL);  Char buffer[4096] = {0};  DWORD Bytesread; The parent process gets the data to the child process, outputting while (true) {
if (ReadFile (houtputread,buffer,4095,&bytesread,null) = = NULL)
{break;  } printf (buffer);  Sleep (500); }
CloseHandle (Hinputwrite); CloseHandle (Houtputread);}

C + + executes an EXE file, anonymous pipe

Related Article

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.