Neil Weicher
Neil Weicher

Reputation: 2502

How to tell when my Windows app is being terminated?

Is there any way my Windows program (C/C++) can receive a notification when it is being killed from Taskmgr.exe? It does not appear to receive any special Windows Messages - it just terminates.

I don't want to stop it from terminating, I just want to write a notification of some kind that it was manually terminated.

Thanks.

Upvotes: 1

Views: 137

Answers (2)

Jonathan Potter
Jonathan Potter

Reputation: 37132

If you need to know immediately when your process is killed the only way I know of is to use another process as a watchdog. If you use OpenProcess() to get a handle to the process in question, you can wait on that handle (via WaitForSingleObject or similar), and the handle will be signalled when the process terminates. You'll need to do some coordination with the target process in order to track whether the shutdown was clean or forcible.

Upvotes: 1

Sekhat
Sekhat

Reputation: 4479

If it's a full windows app, you should get WM_QUIT in your message pump right before the application quits.

As MSDN states: http://msdn.microsoft.com/en-us/library/windows/desktop/ms632641(v=vs.85).aspx

This isn't posted to a window's message queue, you can only retrieve it in your main message pump.

This is only when it quits cleanly. If the process is killed, this never happens.

A way you can detect it being killed on next launch, is to have a file be created on start-up and destroyed on shutdown, If the file still exists on the next start up you know that the process was killed, but not whether it was killed due to an error or because it was killed at a users request.

Upvotes: 3

Related Questions