Search code examples
visual-c++shutdownafxmessage-map

Catching windows shutdown events using AFX Message maps, handler function is never called


I have a windows c++ app that I want to gracefully shutdown. Doing my research seems like QueryEndSession EndSession and Powerbroadcast would be the windows events of interest.

So I plugged them into my message map like this


    BEGIN_MESSAGE_MAP(CProgView, CFormView)
        
        ON_WM_QUERYENDSESSION()
        ON_WM_ENDSESSION()
        ON_WM_POWERBROADCAST()
    
    END_MESSAGE_MAP()

And added the functions needed in the .h


    afx_msg BOOL OnQueryEndSession();
    afx_msg void OnEndSession(BOOL);
    afx_msg UINT OnPowerBroadcast(UINT, LPARAM);

and .cpp


    BOOL CProgView::OnQueryEndSession()
    {
        return 0;
    }
    
    void CProgView::OnEndSession(BOOL b)
    {
    
        int x = 1;
    
    }
    
    UINT CProgView::OnPowerBroadcast(UINT nPowerEvent, LPARAM nEventData)
    {
        return 0;
    }

But when I try to shutdown, or simulate a shutdown it fails and my breakpoints are not hit.

C:\Program Files (x86)\Windows Kits\10\App Certification Kit>rmlogotest.exe 5112

LOGO Validation FAILED. Unable to shutdown registered process id 5112 with error code 0000015f

Debugging Information --
# of Proc/Svcs   : 1
Reboot ReasonCode: 00000000

    Session 1, Pid 5112, Type 1, Status 1 - progName ()

What am I missing?


Solution

  • @Dxiv pointed out that these handlers need to be in the top level window, which was correct.