The anonymous pipeline and redirection technology first we have to understand a definition: What is redirected? It can be understood to be prepared as: re-decision direction! In the console program, the standard input, output handle is replaced by the handle, and the handle is replaced, and the console program itself does not know its input, the output handle has changed It still treats the relevant handle (the read / write handle of the pipe) as a standard input, and the output handle is viewed. The situation when the parent process and sub-process is in data communication: the parent process redirects the standard output to the write handle, the promoter process, because inheritance relationship, the child process has this feature, its standard The output is also associated with the write handle of the pipeline. The parent process at this time will be obtained by the read handle to obtain data from the child process after shutting down the pipe writing handle (must be turned off). To illustrate this real thing, I will give an example, explain how to call the standard output of the console in the graphical interface. The result of the program run is as follows: The program implements: 1. Newly built a dialog box CMYGUIDLG, join an edit box, set the properties of the edit box (multi-line, horizontal && vertical scroll bar, self-scrolling, want return) ID The number is: IDC_EDIT1 Add a CEDIT variable for IDC_EDit1 in ClassWizard. CEDIT M_EDIT1; 2, join a working thread, the function can be a global, of course, can also be used as a STATIC type member. We choose the latter.
Static Uint Testthread (LPVOID P); 3, in the onok () message processing function, add the following code: void cmyguidlg :: ONOK () {AFXBEGINTHREAD (Testthread, this); Updatedata (TRUE);} 4, in thread functions we add the following code: UINT CMyDUIDlg :: TestThread (LPVOID p) {STARTUPINFO si; PROCESS_INFORMATION pi; // read handle pipe hANDLE hRead; // pipe write handle hANDLE hWrite; SECURITY_ATTRIBUTES saAttr; saAttr.nLength = sizeof (SECURITY_ATTRIBUTES); saAttr. LPSecurityDescriptor = 0; sattr.binherithandle = true; // Create anonymous pipe if (! CreatePipe (& HREAD, & HWRITE, & Saattr, 0)) {AFXMessageBox ("Create pipe failed!"); return 0;} // Get this process Current standard output // handle htemp = getStdHandle (std_output_handle); // Setting standard output to anonymous pipe // setstdhandle (std_output_handle, hwrite); Si.cb = sizeof (startupinfo); getStartupInfo (& Si); // Get this process STARTUPINFO structural information si.hStdError = hWrite; si.hStdOutput = hWrite; si.wShowWindow = SW_HIDE; si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES; // let the child hide, standard output into force CString strLine; strLine.Format ( "cl -h % D /? ", (unsigned int) hwrite); // command line parameter // creation sub-process if (! CreateProcess (Null, (LPSTR (LPCTSTSTSTR) Strline, NULL, NULL, TRUE, NULL, NULL, NULL, & Si, & PI) {AFXMessageBox ("Create sub-process failed!"); Return 0;} // Restore the standard output // setstdhandle (STD_OUTPUT_HANDLE, HTEMP); CloseHandle (HWRITE); // Close the handle (must turn off) CString strout; char readbuf [100] = {0}; DWORD DWREAD; // Read the pipeline until the pipe closes while (Readfile (HREAD, Readbuf, 99, & dwread, null) {strout = readbuf; Memset (Readbuf, 0, 99);} cmyduidlg * PLG = (CMYDUIDLG *) P; PLG-> m_edit1.SetWindowText (strout); PLG-> InvalIdateRect NULL); CloseHandle (HREAD); RETURN 0;} Decompose: 1. The console output of this program is output is the result of the CL.exe run in VC . Of course, you can also join some console test programs, assign strline to the console program name you want to test.