Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Windows close a program when shutting down the computer?

My application throws some strange errors if you shut down the computer while my application is running.

Sometimes the message is (address) memory can not be "read", sometimes can not be "write".

Shutting down the application in the normal way doesn't generate such messages.

How can I simulate the "windows shutdown" so that I can debug my application? How can I find out what the application is trying to do that it cannot?

like image 992
Neka Avatar asked Jun 23 '15 09:06

Neka


1 Answers

When Windows wants to shutdown, it sends a series of events to the application; such as WM_ENDSESSION and WM_QUIT. You can process these in the message handler you are using; in general the application will need to respond appropriately and quickly to these messages else the OS will just terminate the application anyway. I'm not sure what default processing wxwidgets offers in this regard. Hooking into these would help in diagnosing the application error itself.

There are a few things you could attempt to do;

  • The shutdown sequence will not be easy to simulate (if at all) - a lot happens during shutdown; the exact state and situation is difficult to simulate in it's entirety.
  • In terms of diagnosing the state of the application just before shutdown, you could try to process the WM_QUERYENDSESSION and respond with a FALSE to prevent it from shutting down (with newer versions of Windows you can no longer prevent the shutdown, so it may not work depending on the platform you are on).
  • You could also try to test the application's immediate response to WM_ENDSESSION message by sending it the WM_ENDSESSION (e.g. via a PostMessage) with the appropriate data as detailed on MSDN.

For terminal based applications; You can also hook in the signals (SIGKILL I believe) if required. See this Microsoft reference for more detail. You can also the the SetConsoleCtrlHandler hook. But since you using a toolkit, it would be better to use the messages sent to the application already.

like image 111
Niall Avatar answered Nov 05 '22 04:11

Niall