Create a process and wait for it to exit

Source: Internet
Author: User

After you have typed a command in the command line, you generally obtain the console after the command is executed. This effect also exists in the 360 software Manager (during software upgrade ). To achieve this, you generally need to create a process and wait for it to exit. The key to implementing this function is the createprocessw and waitforsingleobject functions. Such code is also available online. The following is a piece of code in an open-source project called eraser, which also contains some useful Code. For details, refer:

Http:// REV = 1278


Int createprocessandwait (const STD: wstring & CommandLine, const STD: wstring & appname) <br/> {<br/> // get a mutable version of the command line <br/> wchar_t * cmdline = new wchar_t [CommandLine. length () + 1]; <br/> wcscpy_s (CommandLine, CommandLine. length () + 1, CommandLine. c_str (); <br/> // launch the process <br/> startupinfow startupinfo; <br/> process_information pinfo; <br/>: zeromemory (& startupin Fo, sizeof (startupinfo); <br/>: zeromemory (& pinfo, sizeof (pinfo); <br/> If (! Createprocessw (null, cmdline, null, null, false, 0, null, null, & startupinfo, <br/> & pinfo )) <br/> {<br/> Delete [] response line; <br/> throw l "error while executing" + appname + L ":" + geterrormessage (getlasterror ()); <br/>}< br/> Delete [] using line; <br/> // OK the process was created, wait for it to terminate. <br/> DWORD lastwait = 0; <br/> while (lastwait = waitforsingleobject (pinfo. hprocess, 50 )) = Wait_timeout) <br/> application: Get (). yield (); <br/> If (lastwait = wait_abandoned) <br/> throw STD: wstring (L "the condition waiting on the termination of. net installer was abandoned. "); <br/> // get the exit code <br/> DWORD exitcode = 0; <br/> If (! Getexitcodeprocess (pinfo. hprocess, & exitcode) <br/> throw geterrormessage (getlasterror (); <br/> // clean up <br/> closehandle (pinfo. hprocess); <br/> closehandle (pinfo. hthread); <br/> // return the exit code. <br/> return exitcode; <br/>} 


The following code creates a process in chrome and obtains the output:

Bool getappoutput (const CommandLine & CL, STD: string * output) {<br/> handle out_read = NULL; <br/> handle out_write = NULL; <br/> security_attributes sa_attr; <br/> // set the binherithandle flag so pipe handles are inherited. <br/> sa_attr.nlength = sizeof (security_attributes); <br/> sa_attr.binherithandle = true; <br/> outputs = NULL; <br/> // create the pipe for the child P Rocess's stdout. <br/> If (! Createpipe (& out_read, & out_write, & sa_attr, 0) {<br/> notreached () <"failed to create pipe"; <br/> return false; <br/>}< br/> // ensure we don't leak the handles. <br/> win: scopedhandle scoped_out_read (out_read); <br/> win: scopedhandle scoped_out_write (out_write ); <br/> // ensure the read handle to the pipe for stdout is not inherited. <br/> If (! Sethandleinformation (out_read, handle_flag_inherit, 0) {<br/> notreached () <"failed to disabled pipe inheritance"; <br/> return false; <br/>}< br/> // now create the child process <br/> process_information proc_info = {0}; <br/> startupinfo start_info = {0 }; <br/> start_info.cb = sizeof (startupinfo); <br/> start_info.hstdoutput = out_write; <br/> // keep the normal stdin and stderr. <br/> Response = getstdhandle (std_input_handle); <br/> response = getstdhandle (std_error_handle); <br/> start_info.dwflags | = startf_usestdhandles; <br/> // create the child process. <br/> If (! CreateProcess (null, <br/> const_cast <wchar_t *> (Cl. command_line_string (). c_str (), <br/> null, null, <br/> true, // handles are inherited. <br/> 0, null, null, & start_info, & proc_info) {<br/> notreached () <"failed to start process "; <br/> return false; <br/>}< br/> // we don't need the thread handle, close it now. <br/> closehandle (proc_info.hthread); <br/> // close our writing end of pipe now. Otherwise later read wocould not be able <br/> // to detect end of Child's output. <br/> scoped_out_write.close (); <br/> // read output from the child process's pipe for stdout <br/> const int kbuffersize = 1024; <br/> char buffer [kbuffersize]; <br/> for (;) {<br/> DWORD bytes_read = 0; <br/> bool success = readfile (out_read, buffer, kbuffersize, & bytes_read, null); <br/> If (! Success | bytes_read = 0) <br/> break; <br/> output-> append (buffer, bytes_read ); <br/>}< br/> // Let's wait for the process to finish. <br/> waitforsingleobject (proc_info.hprocess, infinite); <br/> closehandle (proc_info.hprocess); <br/> return true; <br/>}< br/> 

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: 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.