Solved

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

Posted on 2004-09-16
8
387 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
8 Comments
 
LVL 39

Expert Comment

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

Author Comment

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

Author Comment

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

0
 
LVL 3

Accepted Solution

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

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 62 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
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

726 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