Solved

How use CreateProcess?

Posted on 1998-02-08
5
204 Views
Last Modified: 2010-04-04
Hi!

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
0
Comment
Question by:Dippen
  • 3
  • 2
5 Comments
 
LVL 5

Expert Comment

by:ronit051397
ID: 1358654
0
 
LVL 5

Expert Comment

by:ronit051397
ID: 1358655
You want to know also when the process dies?
0
 
LVL 5

Accepted Solution

by:
ronit051397 earned 100 total points
ID: 1358656
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 http://www.chami.com/tips/delphi/122096D.html .
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;
var
  pi: TProcessInformation;
  si: TStartupInfo;
begin
  FillMemory( @si, sizeof( si ), 0 );
  si.cb := sizeof( si );
  CreateProcess(
                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 );
end;

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

0
 

Author Comment

by:Dippen
ID: 1358657
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...

0
 

Author Comment

by:Dippen
ID: 1358658
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!
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now