Solved

How use CreateProcess?

Posted on 1998-02-08
5
212 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

830 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