exe needs to catch a message when being killed by the task manager

Posted on 2004-09-16
Medium Priority
Last Modified: 2013-12-03
i have an exe that if killed in a non graceful fasion, ie. being killed by Task Manager i need to catch it and then perfom some action.

i'm assuming i need to create some message pump in the exe that will catch a message being sent by the Task Manager.  is this correct?  i need to use win32 to complete this.

thns for the help!
Question by:howardsd
  • 2
  • 2
LVL 39

Expert Comment

ID: 12077681
That depends on how the exe was killed.

If killed by 'Stop Task' at the first page Task Manager sends a WM_CLOSE message to the main window of the application (but you don't have a window til now, right?). However, if the process got killed with 'Kill Process' on the second page, you get no message and the only means i know to prevent that is to prevent killing at all (e. g. by turning your prog to a system service).

Some wild ideas:

- You can have a thread that watches if TaskManager window was started and tries to
   hook on mouse and keyboard then.
- You always start a second executable that is watching if your first exe is still alive
   (waiting on process handle). If not, it does the cleaning you need or restarts your executable.
   In your first exe you need thread that is watching for the watchdog exe as well.

Regards, Alex

Author Comment

ID: 12077859
so even if the exe starts a hidden dlg, neither the exe or the dialog will receive a message if killed from the processes window in the task manager?

your first suggesstion isn't plausible in my case, because i'm not actually worried about the native windows task manager because it will more than likely not be available on the desired systems (for security).  i'm more concerned with 3rd party process viewers that are easily downloadable from the inet.  so, i really need to catch if the process is killed in the harshest possbile way.  i'm assuming that most third party process viewers kill processes in the same manner as the native windows task manager??

i like your second suggestion!  kind of like a watch dog thread.  if my exe or hidden dialog can't catch a message i'm thinking that this might be the only possible way to accomplish what i need to.

Author Comment

ID: 12078156
i'm thinkin that running my app as a service is going to be the best course of action.  i'm hoping that won't be too difficult.  any suggestions?


Accepted Solution

CmdrRickHunter earned 252 total points
ID: 12079080
When windows kills a process (using the kill process button), it simply ends its execution.  The program no longer recieves time slices, and windows cleans up its memory usage.  No message is sent because no message is needed, and in theory no message is wanted, because killing a process is usually done in response to a program which is stuck, and wont handle the WM_CLOSE messages.

If you are REALLY worried about secuirty, make certain that both watchdogs (the watchdog watching your app, and the thread in your app watching the watchdog) are at the highest thread priority possible, and then have them "wait" on the process.  This wont take up any timeslices, until the program is stopped, and then, as a high priority thread, your thread immediatly gets to execute.

Actually, if you're really worried about security, dont go windows... but that's probably out of the realm of possibility.

Annother thing you could look into is, instead of having the watchdog being watched by your app, have the watchdog run as a service started by the administrator, and then make your user log in as a normal user.  I'm pretty certain that normal users are not allowed to shut down services started by the administrator, or the kill the svchost that encapsulates them into processees.
Other big advantages of this are the fact that you dont have to worry about debugging some complicated service, and you can always have the administrator kill the watchdog incase your program runs away.

Make sure you can send messages to the watchdog so that when you actually want the program to go away, it doesn't keep respawning it =)
LVL 39

Assisted Solution

itsmeandnobodyelse earned 248 total points
ID: 12079626
>>> running my app as a service ....  i'm hoping that won't be too difficult.

No, it's not *very* difficult, but ...  somewhat strange. Your function main() turns to service_main() and you will have a new main() function that will install the service rather than start it. Also a service should be able to respond to Service Control Manager (SCM) requests  like stopping or give status within reasonable times. So, you need some kind of message loop or a thread to handle these requests. In MSDN you'll find a sample with service.c that easily could be adapted ...

As far as i know services only can be stopped via SCM (since W2k) . At WinNt you could attach a service using the Debugger and then simply stop it there. That doesn't work on Win2k and WinXP .

Regards, Alex

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

621 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