Ruslan F.
Ruslan F.

Reputation: 5776

Interaction with Windows rebooting/shutdown

For example we have a Win32 application which on close of main window hides to tray.
When user reboot or shutdown OS applications were closed in some way.
How to handle properly this closing event?

Upvotes: 0

Views: 1091

Answers (3)

m0e
m0e

Reputation: 37

Shuting down should cause a SIGTERM or SIGINT be sent to you program. You can handle this via . Ref: http://www.cplusplus.com/reference/csignal/signal/

void my_handler (int param)
{
  // clean up here
}
int main ()
{
    signal(SIGINT my_handler);   // try also SIGTERM
}

Upvotes: -2

David Heffernan
David Heffernan

Reputation: 613461

This is documented over on MSDN: Shutting Down.

Essentially you need a top-level window that listens for either WM_QUERYENDSESSION or WM_ENDSESSION or possibly both. You get an opportunity to perform shutdown related tasks when these messages arrive.

Applications with a window and message queue receive shutdown notifications through the WM_QUERYENDSESSION and WM_ENDSESSION messages. These applications should return TRUE to indicate that they can be terminated. Applications should not block system shutdown unless it is absolutely necessary. Applications should perform any required cleanup while processing WM_ENDSESSION. Applications that have unsaved data could save the data to a temporary location and restore it the next time the application starts. It is recommended that applications save their data and state frequently; for example, automatically save data between save operations initiated by the user to reduce the amount of data to be saved at shutdown.

If you wish to show UI during shutdown, perhaps because your app is performing critical actions that cannot survive interruption, then you can use ShutdownBlockReasonCreate.

If an application must block a potential system shutdown, it can call the ShutdownBlockReasonCreate function. The caller provides a reason string that will be displayed to the user. The reason string should be short and clear, providing the user with the information necessary to decide whether to continue shutting down the system.

Note that this process was changed dramatically from Vista. If you need to support XP then you may need code that behaves differently under XP. That topic is also covered on MSDN: Application Shutdown Changes in Windows Vista.

Upvotes: 2

Werner Henze
Werner Henze

Reputation: 16771

You should handle the System Shutdown Messages WM_QUERYENDSESSION and WM_ENDSESSION.

Upvotes: 2

Related Questions