Anonymous pipe reads cmd echo information

Source: Internet
Author: User

Before using the very pit dad to read the cmd command echo information, now found the use of anonymous pipeline implementation method, because the landlord did not learn Windows core programming, found a code to dine

To study after it was saved

#include <windows.h>#include<stdio.h>#defineExe_name NULL//TEXT ("Cmd.exe")#defineExe_cmd TEXT ("cmd.exe/c ipconfig/all")intMain () {Charbuffer[4096];        Startupinfo sinfo;//The main window characteristics of the new process process_information pInfo;        Security_attributes sa;        HANDLE Hread, Hwrite;    DWORD Bytesread; Read the length of the code sa.nlength=sizeof(security_attributes);//structure size, can be obtained by sizeof Sa.lpsecuritydescriptor=null;//Security Descriptor Sa.binherithandle=whether the true;;//security-described object can be inherited by the newly created ææ processif(! CreatePipe (&hread, &hwrite, &sa,0))//Create an anonymous pipeline        {            returnGetLastError ();//Returns a recent error, 0 indicates normal} Getstartupinfo (&sinfo); SINFO.CB=sizeof(Sinfo); Sinfo.dwflags= Startf_useshowwindow |Startf_usestdhandles; Sinfo.wshowwindow=Sw_hide; Sinfo.hstderror= Hwrite;//handing the write end of the pipeline to the child processSinfo.hstdoutput =Hwrite; memset (&pinfo,0,sizeof(PInfo)); if(! CreateProcess (exe_name, exe_cmd, NULL, NULL, TRUE,0, NULL, NULL, &sinfo, &pinfo))//Create Child process{CloseHandle (hwrite);            CloseHandle (Hread); returnGetLastError (); } closehandle (Hwrite); //Close the write end of the parent process                 for(inti =0;; ++i) {if(! ReadFile (Hread, Buffer,sizeof(Buffer)-1, &bytesread, NULL))//Read Content            {                 Break; } Buffer[bytesread]=0; printf ("%s\n", Buffer); } WaitForSingleObject (Pinfo.hprocess, INFINITE);//When the wait is still pending, the handle is closed, then the function behavior is undefined.    The handle must have SYNCHRONIZE access rights;    CloseHandle (Hread); System ("Pause"); return 0;}

Anonymous pipe reads cmd echo information

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.