Execute an exe file, anonymous pipeline, and exe pipeline in c ++.

Source: Internet
Author: User

Execute an exe file, anonymous pipeline, and exe pipeline in c ++.
The pipeline is used for operating system experiments. The pipeline (Pipe) is actually a shared memory used for inter-process communication. The process that creates the pipeline is called the pipeline server, and the process that connects to a pipeline is the pipeline client. After a process writes data to the pipeline, the other process can read data from the other end of the pipeline. The Anonymous pipeline (Anonymous Pipes) is an unnamed pipeline for one-way data transmission between the parent process and the child process. It can only be used on a local computer and cannot be used for inter-network communication. Anonymous pipeline implementation rules: An anonymous pipeline is created by the CreatePipe () function. When an anonymous pipeline is created, this function returns two handles: The read handle of the pipeline and the write handle of the pipeline. The CreatePipe () function is prototype: BOOL CreatePipe (PHANDLE hReadPipe, // the pointer to the read handle PHANDLE hWritePipe, // the pointer to the write handle LPSECURITY_ATTRIBUTES lpPipeAttributes, // pointer to the Security Attribute DWORD nSize // pipeline size); the handles pointed to by hReadPipe and hWritePipe can access the pipeline in read-only and write-only modes respectively. Anonymous pipelines do not support asynchronous read and write operations, which means that ReadFileEx () and WriteFileEx () cannot be used in anonymous pipelines, and ReadFile () and WriteFile () the lpOverLapped parameter in will also be ignored. The anonymous pipeline closes the Read and Write handles. You can also call the CloseHandle () function in the process to close the handles. 1. If you only want to get the results of the sub-process, you can create only one pipeline and then get the output in ReadFile. STARTUPINFO si; PROCESS_INFORMATION pi; char ReadBuf [100]; DWORD ReadNum; HANDLE hRead; // HANDLE hWrite; // pipeline write handle BOOL bRet = CreatePipe (& hRead, & hWrite, NULL, 0); // create an anonymous pipeline if (bRet = TRUE) printf ("anonymous pipeline created successfully! \ N "); else printf (" failed to create anonymous pipeline, error code: % d \ n ", GetLastError ()); // get the current standard output HANDLE hTemp = GetStdHandle (STD_OUTPUT_HANDLE) of the process; // set the standard output to the anonymous pipeline SetStdHandle (STD_OUTPUT_HANDLE, hWrite); GetStartupInfo (& si ); // obtain the STARTUPINFO structure information of this process. bRet = CreateProcess (NULL, "Client.exe", NULL, NULL, TRUE, NULL, & si, & pi ); // create a sub-process SetStdHandle (STD_OUTPUT_HANDLE, hTemp); // restore the standard output of this process if (bRet = TRUE) // input information p Rintf ("sub-process created successfully! \ N "); else printf (" failed to create sub-process, error code: % d \ n ", GetLastError (); CloseHandle (hWrite ); // close the write handle // read the pipeline until the pipeline is closed while (ReadFile (hRead, ReadBuf, 100, & ReadNum, NULL) {ReadBuf [ReadNum] = '\ 0 '; printf ("read % d byte data from pipeline [% s] \ n", ReadBuf, ReadNum);} if (GetLastError () = ERROR_BROKEN_PIPE) // output information printf ("pipeline quilt process closed \ n"); else printf ("read data error, error code: % d \ n", GetLastError (); 2. if you want to input data to a sub-process, you must also create an MPS queue for the standard input of the sub-process. enter it with WriteFile. # 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 assigned to the standard output (or standard error) handle si. hStdInput = hInputRead; // when the parent process sends data to the child process, use SetStdHandle () // to assign the read handle of the MPs queue to the standard input handle; when receiving data from a sub-process, // use SetStdHandle () to assign the write handle of the pipeline to the standard output (or standard error) handle. // Then, the parent process can call the CreateProcess () function of the Process Creation function to generate sub-processes. // If the parent process needs to send data to the child process, the parent process can call WriteFile () // write data to the pipeline (pass the pipeline write handle to the function ), the sub-process calls GetStdHandle () // to obtain the read handle of the pipeline. It passes the handle into ReadFile () and then reads data 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, & 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 and inputs WriteFile (hInputWrite, szInPut, strlen (szInPut), & dwWritten, NULL); char buffer [4096] = {0}; DWORD bytesRead; // The parent process obtains data from the child process, output while (true ){
If (ReadFile (hOutputRead, buffer, 4095, & bytesRead, NULL) = NULL)
{Break;} printf (buffer); Sleep (500 );}
CloseHandle (hInputWrite); CloseHandle (hOutputRead );}

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.