Re: Terminating an external application (.exe)?
From: Peter Below (TeamB) (100113.1101_at_compuXXserve.com)
Date: 10/17/03
- Next message: Ben Hochstrasser: "Re: How to set next position in FileListBox"
- Previous message: Peter Below (TeamB): "Re: File transfert"
- In reply to: Jose J. Sanchez Rico: "Terminating an external application (.exe)?"
- Next in thread: Valentin Tihomirov: "Re: Terminating an external application (.exe)?"
- Reply: Valentin Tihomirov: "Re: Terminating an external application (.exe)?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Fri, 17 Oct 2003 11:34:07 +0200
In article <3f8f1c90@newsgroups.borland.com>, Jose J. Sanchez Rico wrote:
> Hi, I have an application that run's 3 other applications and monitors them
> through some registry values,
>
> when I detect that the application presented an error or just doesn't
> response (checking the registry values) i wan't to close o terminate the
> application and run it again.
>
> How do I force a terminate without any kind of user input, this is because I
> want the application to manage the other 3, and don't want to be logging to
> the server to see if everything is OK?
If your monitor also starts the application it monitors you already have the
required information at hand. Use CreateProcess or ShellExecuteEx to start the
applications. This gives you the apps process handle, which you store for
later reference. Close the thread handle you also get from CreateProcess since
you don't need it.
You did not tell use whether the monitored applications have windows or not.
If they have windows the first step in trying to close them would be to find
the window handle of the apps main window. You know the class name of that
window if you wrote the app yourself and it is easy enough to figure out if
this is a 3rd-party app. Use FindWindow to get the windows handle. Once you
have the handle you do a
var
retval: Cardinal;
begin
if SendMessageTimeout( wnd, WM_CLOSE, 0, 0,
SMTO_ABORTIFHUNG, 1000, retval ) <> 0
then
...app received the close request and should go down on itself
else
.. app is hung and needs to be nuked
If the app turns out to be hung or has no windows in the first place you use
the TerminateProcess API call to shoot it down. But this is really a measure
of last resort since it does not allow the app to do any proper cleanup on
resources, and the automatic cleanup the OS does may not be able to handle
everything properly (e.g. connections to remote databases).
Do not forget to call CloseHandle on the stored process handle when you are
done with it. By the way, you can use this handle with GetExitCodeProcess to
determine whether the process is dead already.
-- Peter Below (TeamB) Use the newsgroup archives : http://www.mers.com/searchsite.html http://www.tamaracka.com/search.htm http://groups.google.com http://www.prolix.be
- Next message: Ben Hochstrasser: "Re: How to set next position in FileListBox"
- Previous message: Peter Below (TeamB): "Re: File transfert"
- In reply to: Jose J. Sanchez Rico: "Terminating an external application (.exe)?"
- Next in thread: Valentin Tihomirov: "Re: Terminating an external application (.exe)?"
- Reply: Valentin Tihomirov: "Re: Terminating an external application (.exe)?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|