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?
Log in to continue reading
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform for $9.99/mo
View membership options
Unlock 1 Answer and 5 Comments.
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
The Value of Experts Exchange in My Daily IT Life

Experts Exchange (EE) has become my company's go-to resource to get answers. I've used EE to make decisions, solve problems and even save customers. OutagesIO has been a challenging project and... Keep reading >>


Owner of Outages.IO
Phoenix, Arizona, United States
Member Since 2016
Join a full scale community that combines the best parts of other tools into one platform.
Unlock 1 Answer and 5 Comments.
View membership options
“All of life is about relationships, and EE has made a virtual community a real community. It lifts everyone's boat.”
William Peck

Member since 2004