How to use WM_QUERYENDSESSION and/or WM_ENDSESSION correctly in my case?
I am adding reaction to the user log-off or the Windows OS shutdown to our application. I have read the MSDN documentation. I have decide to left the processing of WM_QUERYENDSESSION to DefWindowProc and to process only the WM_ENDSESSION.
The problem is that the application can be used both in interactive mode (with usual GUI) and in noninteractive mode (much simpler GUI that does not react to the user events). In noninteractive mode (launched by system scheduler, controlled by the application script) no message loop is created. Still, start and finalization of the application uses the same code.
When using the interactive mode, the finalization is done by the code placed after the message loop. When working in the non-interactive mode, the message loop is not entered at all.
Whe quitting the application in the interactive mode, finalization of the GUI is also done (remembering user settings...). It is done in WM_DESTROY handler of the main window. The OnDestroy() handler also does PostQuitMessage(0) at the end of its body. This causes leaving the message loop. Consequently, the application finalization place after the message loop (as mentioned above) is done.
My OnEndSession() handler looks this way (using ATL conventions):
LRESULT CWndDirector::OnEndSession(UINT uMsg,
WPARAM wParam, LPARAM lParam,
ATLASSERT(uMsg == WM_ENDSESSION);
ATLASSERT(wParam == TRUE);
ATLASSERT(m_pWnd != 0);
m_pWnd->DestroyWindow(); // sending WM_DESTROY to the main application window
return 0; // should be returned when handled
It seems that the above DestroyWindow() causes the OnDestroy() handle but the application quits somehow more quickly than expected. The code after the message loop is not executed. Please, could you explain that?