Solved

How use CreateProcess?

Posted on 1998-02-08
5
216 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses
Course of the Month11 days, 9 hours left to enroll

623 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