views:

278

answers:

4

I have a windows C++ application (app.exe). When the app is closed, I need to perform some cleanup tasks specific to my application. What happens when this process (app.exe) is killed through the Task Manager. Assuming that the application is still responsive, can I somehow handle this situation in my app.exe?

I am looking for something similar to how kill <pid> in Linux will send the SIGTERM signal to the process indicated by pid. I could then register my own signal handler for SIGTERM and perform the cleanup.

A: 

I think you will need another PID that is monitoring the PID of your app.exe and does the necessary work at the time.

Raj More
A: 

I found a similar question here

dbrien
It's as similar as apple and pear. Fruits may they be, but tasting quite different.
Ivan Vučica
The answers to that question address some of the questions he's asking here actually.
dbrien
+3  A: 

There are two ways to kill application in Task Manager.

  • Killing through Applications tab would roughly be equivalent of SIGTERM. Application may intercept it and do more processing, since it's basically sending a "close window" message. Message to catch is WM_CLOSE.
  • Killing through Processes tab would roughly be equivalent of SIGKILL. There is nothing you can do to intercept that, short of monitoring user's actions in Task Manager's listbox and End Process button, or having a watchdog process that will see when the first one is killed.

Alternatively, design the application in a way that does not require cleanup, or in a way that it will perform cleanup at startup.

Ivan Vučica
Unfortunately, can't re-design the app. Its a 10 year old legacy app with several thousand lines of code.
Umsd
A: 

That depends, if the user chooses to "End Task" your application you will be notified and you can handle it see this.

but if the user chooses to end the process, you have no way to handle it in your application. the easiest way would be a second process or you can inject into process manager and hook the TerminateProcess API.

Mehran