Re: Terminating an external application (.exe)?

From: Peter Below (TeamB) (100113.1101_at_compuXXserve.com)
Date: 10/17/03


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


Relevant Pages

  • Re: Themes & Colors
    ... The whole idea of Windows is to provide a consistent color ... If you change the color scheme, you change ALL applications. ... If you want to change the way your app looks color-wise, ...
    (microsoft.public.fox.programmer.exchange)
  • Re: Honestly Curious
    ... This is not a real-world problem, but I give you points for being creative enough to try and dream up an excuse for my moving an app *might* be a bad thing somehow. ... The registry however is the method Microsoft not only recommends for applications to use, but they require it for vendors to get the Windows logo on their packaging and marketing material. ...
    (comp.sys.mac.advocacy)
  • Re: user and group troubles
    ... change on the applications installation directory. ... to trace execution failure and discover where the app is throwing ... Microsoft MVP (Windows Server System: ... > giving the user local Admin rights on the said workstation. ...
    (microsoft.public.windowsxp.security_admin)
  • Re: Make a Windows Service start a windows program.
    ... never be implemented as Windows Services, they should be normal applications ... that get started at logon (search for Autorun in windows help). ... Only the UI portion of the app depends on an interactive logon. ... When the service instantiates the Project object from the class in the UI app, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Make a Windows Service start a windows program.
    ... Sounds to me like a normal application running with a notify icon rather ... start-up, Applications that depend on interactive logon session, should ... that get started at logon (search for Autorun in windows help). ... Only the UI portion of the app depends on an interactive logon. ...
    (microsoft.public.dotnet.languages.csharp)