Solved

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

Posted on 2004-09-16
8
379 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

763 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