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