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

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!
Who is Participating?
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 =)
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
howardsdAuthor Commented:
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.
howardsdAuthor Commented:
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?

>>> 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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.