The stdin is the standard input, the STDOUT is the standard output, and the stderr is the standard error output. Most command-line programs import from stdin, output to stdout or stderr, and sometimes we need to redirect Stdout,stderr,stdin. For example: Write the output to a file, or we want to display the command-line program output to the Windows dialog box.
In Windows programming, the concept of piping (pipe) is required for redirection. A pipeline is a mechanism for sharing data between processes. A pipe is similar to the ends of a pipe, one end is written, and one end is read out. A process is written from the write-side, and another process reads from the read-out side, enabling communication, just like a "pipe".
The principle of redirection is:
First, two concepts are declared: The main program (the redirected manipulator), the child process (the child process being redirected)
- If you want to redirect stdout, Sir into a pipeline, the writing end of the pipeline to the sub-process to write, the main program from the read-out of the pipeline read data, and then can be written to the data file, display and so on. REDIRECT StdErr and stdout are the same.
- Similarly, to redirect stdin words, generate a pipeline that writes driven by the main program writes, and the subprocess reads the data from the read-out side of the pipeline.
The following is an example of an output redirect: The result of a command-line ping is shown in the edit box.
The core code is as follows:
#define MSG_DATAREC Wm_user +0x400begin_message_map (Cstudystdout_redirectguidlg, CDialog) On_wm_syscommand () ON_WM_ PAINT () On_wm_querydragicon ()//}}afx_msg_mapon_bn_clicked (Idc_button1, &cstudystdout_redirectguidlg::o NBnClickedButton1) on_message (Msg_datarec,&cstudystdout_redirectguidlg::ondatarec) end_message_map () void Cstudystdout_redirectguidlg::onbnclickedbutton1 () {//Todo:add your control notification handler code HERESECURITY_ ATTRIBUTES sa; ZeroMemory (&sa,sizeof (SA)); sa.nlength = sizeof (security_attributes); Sa.lpsecuritydescriptor = NULL; The default security descriptor Sa.binherithandle = TRUE; This must be set to True, reference: "Windows core Programming" Chapter III HANDLE Hread, HWRITE;IF (! CreatePipe (&hread, &hwrite, &sa, 0)) {MessageBox ("CreatePipe return FALSE."); Return } m_hreadpipe = Hread; AfxBeginThread (Readpipeproc,this,null); Startupinfo Sistartinfo; Process_information Piprocinfo; ZeroMemory (&sistartinfo,sizeof (Startupinfo)); Sistartinfo.cb =sizeof (startupinfo); siStartInfo.dwFlAGS |= startf_useshowwindow;sistartinfo.dwflags |= startf_usestdhandles;sistartinfo.hstdoutput = hWrite; Sistartinfo.hstderror = Hwrite; TCHAR szcmdpar[256] = "Ping www.baidu.com"; if (! CreateProcess (NULL, Szcmdpar,null,null,true,null,null,null,&sistartinfo, &piprocinfo)) {MessageBox (" CreateProcess failed! "); Return }closehandle (piprocinfo.hprocess); CloseHandle (Piprocinfo.hthread);} UINT Readpipeproc (lpvoid pparam) {Cstudystdout_redirectguidlg * Pattachwnd = Static_cast<cstudystdout_redirectguid LG *> (Pparam); HANDLE hread = pattachwnd->m_hreadpipe; HWND hwnd = Pattachwnd->getsafehwnd (); DWORD Bytesread; while (1) {int len = sizeof (pattachwnd->m_szdata); ZeroMemory (&pattachwnd->m_szdata,len); if (! ReadFile (Hread, Pattachwnd->m_szdata, len-1, &bytesread, NULL)) break; SendMessage (hwnd,msg_datarec,0,0); } return 0; } HRESULT Cstudystdout_redirectguidlg::ondatarec (WPARAM WPARAM, LPARAM LPARAM) {m_stredit + = M_szdata; UpdatEData (FALSE); return 1;}
Windows pipeline with stdin, stdout redirection