troubleshooting Question


Avatar of pepr
pepr asked on
C++Windows OS
5 Comments1 Solution2757 ViewsLast Modified:
How to use WM_QUERYENDSESSION and/or WM_ENDSESSION correctly in my case?

Hi experts,

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,
                                   BOOL& bHandled)
    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?
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 5 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros