Windows Anonymous pipeline reads sub-process output

Source: Internet
Author: User

      // Prepare pipe handles for standard output redirection
      Security_attributes saattr;
      // Set the binherithandle flag so pipe handles are inherited.
      Saattr. nlength = sizeof (security_attributes );
      Saattr. binherithandle = true;
      Saattr. lpsecuritydescriptor = NULL;
      Handle hreadpipe, hwritepipe;
      Bool res = createpipe (& hreadpipe, & hwritepipe, & saattr, 0 );
      If (! Sethandleinformation (hreadpipe, handle_flag_inherit, 0 ))
      {
          Log (error <"sethandleinfomation fail .");
      }

      String stroutfile;
      If (! Res)
      {
          Return res;
      }
      Else
      {
          Startupinfo Si;
          Memset (& Si, 0, sizeof (SI ));
          Si. cb = sizeof (SI );
          Getstartupinfo (& Si );
          // Hide DOS window
          Si. dwflags = startf_useshowwindow;
          Si. wshowwindow = sw_hide;
          Si. dwflags = startf_useshowwindow | startf_usestdhandles;
          Si. hstdoutput = hwritepipe;
          Si. hstdinput = hreadpipe;
          Si. hstderror = hwritepipe;
          Process_information PI;
          Zeromemory (& Pi, sizeof (process_information ));

          Wstring sfullcommand = l "WMIC diskdrive list ";
          Res = CreateProcess (null, (lptstr) (lpctstr) sfullcommand. c_str (), null, null, true,
              0, null, null, & Si, & PI );
          If (! Res)
          {
              // Process error of RSH
              Log (error <"CreateProcess fail .");
              Return err_error;
          }
          Else
          {
              // Close write Pipe
              Closehandle (hwritepipe );
              DWORD numberofbytesread;
              Char buffer [257];
              While (: readfile (hreadpipe, buffer, sizeof (buffer)/sizeof (buffer [0])-1,
                  & Numberofbytesread, null ))
              {
                  Log (info <buffer <"numberofbytesread:" <numberofbytesread );
                  If (numberofbytesread)
                  {
                      Buffer [numberofbytesread] = tchar (0 );
                      Stroutfile + = buffer;
                  }
                  Else
                      Break;
              };
              Log (info <"lasterror:" <getlasterror ());
              // Close read pipe and wait for the finish
              Closehandle (hreadpipe );
              Waitforsingleobject (PI. hprocess, infinite );
              Closehandle (PI. hprocess );
              Res = true;
          }
      }

Refer:
Http://msdn.microsoft.com/en-us/library/windows/desktop/ms682499 (V = vs.85). aspx

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.