How use CreateProcess?

Dippen used Ask the Experts™

I've been skimming through the Online-reference for CreateProcess, but it seems somewhat confusing.
Your mission if you choose to accept it:

Show me how I use CreateProcess to run an application (with command line parameters, no other fancy stuff) within my Delphi code just as if I double-clicked it in a file-window, and show me how to make the parent process halt until the child process is finished and terminates. Or does the parent process halt automatically??

Thanks, folks!

//Daniel, Sweden
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
You want to know also when the process dies?
The parent application does not wait. In order to let it wait until the process dies, you need to call WaitforSingleObject.

The following function comes from .
I just added the WaitforSingleObject line, in order to stop the parent application from continuing as long as the process live.
If you click the button, then the message will be shown only when the notepad is closed.

function CreateProcessSimple( sExecutableFilePath : string ) : String;
  pi: TProcessInformation;
  si: TStartupInfo;
  FillMemory( @si, sizeof( si ), 0 );
  si.cb := sizeof( si );
                Nil, PChar( sExecutableFilePath ), Nil, Nil,                                 False, NORMAL_PRIORITY_CLASS, Nil, Nil, si, pi );
  WaitforSingleObject(pi.hProcess, INFINITE);//wait until the process terminates
  CloseHandle( pi.hProcess );
  CloseHandle( pi.hThread );

procedure TForm1.Button1Click(Sender: TObject);
  CreateProcessSimple( 'notepad' );
  showmessage('The Process is DEAD');


Thanks, Ronit! I'll try the code later and grade your answer accordingly. But now I've got to go put my pillow in my face...


Worked perfectly except for one minor thing: The procedure doesn't seem to accept long filenames... I thought CreateProcess was a Win32-command. But as I said, besides that it worked perfectly. Thanks, Ronit!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial