I have this MFC program that when I kill it with task manager I get an exception on my program and then it crashes.
I want to get the event from the task manager, wh
Yes, both of these are correct. You should respond to WM_CLOSE to close gracefully. This could come from anywhere, not just task manager (shutdown for instance).
MFC normally handles WM_CLOSE. If your app is not responding then your main thread must be sat doing something else, or more likely from your description is crashing somewhere in the WM_CLOSE handler.
Can you debug your app to find where the exception is being raised?
When you get a WM_CLOSE, you can easily detect that so your application can act upon it.
I don't believe it's possible to know when TerminateProcess is being called to kill your application. The TerminateProcess
documentation says it's an immediate and unconditional shutdown of the target process.
(Depending on how much you want to achieve this, take a look at this link about hooking into the Windows API but don't expect it to be easy.)
Task manager internally uses the EndTask function. This functions sends a WM_CLOSE message to your application. If your application does not respond to that message and the user forces to terminate your Application, TerminateProcess is called on your process.
Yes, these are the options.
For completeness, note that console mode applications get the CTRL_CLOSE_EVENT
send that you could react upon, when the "End Task" button is clicked.
Note that you cannot intercept or react upon TerminateProcess. Your process will die and there is nothing you can do before that happens. Actually, it would be pretty bad if you could. Because then there would be no way to terminate a process that went haywire.