Link to home
Start Free TrialLog in
Avatar of mdrasted
mdrastedFlag for Denmark

asked on

How to start and then kill a process

How do I start a command line (DOS) process and then kill it from Delphi? My platform is NT.

Regards,
Morten
Avatar of PeterLarsen
PeterLarsen

Use ShellExecute to activate the program and FindWindow to close it again.
You may search for it here.

You can call CreateProcess with cmd.exe as executable and keep the returned process handle (in TProcessInformation). Whenever you think it is time to close the process then you can try sending a WM_QUIT message to the process or (if it doesn't react) call KillProcess.

Ciao, Mike
Avatar of mdrasted

ASKER

Can you please give me an example on how to use CreateProcess?

Regards;
Morten
Mike.
I dont think it is a good idea to remember the handle and use it to close the window again, because it may no longer exist.
-just a comment.
ASKER CERTIFIED SOLUTION
Avatar of Lischke
Lischke

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Peter, you don't need a window to post a message. All you need is a message queue which is automatically created when a thread calls GetMessage or PeekMessage the very first time. For these calls no window is needed but the command line program has indeed one so you can be sure that a message queue has been created.

Ciao, Mike
Thanks for your excellent help Mike!!

Regards,
Morten
I'm pleased to help you and thank you for the A grading :-)

Ciao, Mike
Ok Mike, i understand this but it was not my point.
If you have a handle to a process and this process dont exist anymore or the handle has been recreated to another process - is it not possible that this may be a problem ??

I'm not asking just for keeping the discussion going, but because i dont know!

Regards
Peter
No problem. If the process is already gone without that the application got notified of this fact (which one can prevent with the WaitFor* calls) then the APIs will just return False on return (PostThreadMessage and the like). The process ID as such is not used again, AFAIK, until the computer is rebooted.

Ciao, Mike