Solved

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

Posted on 2004-09-16
8
353 Views
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!
0
Comment
Question by:howardsd
  • 2
  • 2
8 Comments
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
Comment Utility
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
0
 

Author Comment

by:howardsd
Comment Utility
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.
0
 

Author Comment

by:howardsd
Comment Utility
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?

0
 
LVL 3

Accepted Solution

by:
CmdrRickHunter earned 63 total points
Comment Utility
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 =)
0
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 62 total points
Comment Utility
>>> 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
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

762 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now