Solved

How use CreateProcess?

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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

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…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

770 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