2
votes

I have app that should do smth on shutdown, I mean - when user presses shutdown, app blocks shutdown, and does some stuff. We do it by handling WM_QUERYENDSESSION and WM_ENDSESSION. The job is done in WM_ENDSESSION handler. This works perfect in windows 7, XP.

The problem is as follows - on Windows 8 shutdown process gets cancelled after about 1 minute - shutdown UI is shown for some time, with my reason string, and then system goes to start screen. No shutdown, nothing happens. My app is still running. At first I thought that the problem is in our app, but it seems that system behaves in such way for all apps, not only for ours. You can check it for yourself, just run some app, that you know, will block shutdown, and do the shutdown (or sign-off). You'll see shutdown UI, telling what apps are blocking shutdown. Just wait a little and after about 60-90 seconds it'll disappear - shutdown will get cancelled.

So I'm wondering whether MS changed something in shutdown process in Windows 8?

Edit: I can't post actual code here, so I'll post kinda pseudocode, to illustrate main points:

LRESULT CMainFrame::OnQueryEndSession(UINT, WPARAM , LPARAM lParam, BOOL& bHandled) {
    m_bQueryEndSession = true;      
    bHandled = true;
    return TRUE;
}

LRESULT CMainFrame::OnEndSession(UINT, WPARAM wParam, LPARAM lParam, BOOL& bHandled) {
    BOOL bEnding = (BOOL)wParam;

    if(!bEnding)
        return 0;

    if(m_bQueryEndSession && /*and some other checks*/ ) 
    {

        if (g_osver.dwMajorVersion >= 6) { 
            // of Vista and higher load User32.dll and call ShutdownBlockReasonCreate() here
        }

        {
            // all important job is done here
            // zzz
        }

        if (g_osver.dwMajorVersion >= 6) { 
            // of Vista and higher load User32.dll and call ShutdownBlockDestroy() here
        }
    }

    bHandled = true;
    return 0;
}
3

3 Answers

1
votes

Vram you can use the new debugging process that is included in VS2012, see the link: Debug features in VS 2012. Since the Windows 8 enforces states differently there may be a problem that these debugging tools may help you detect. Hopefully this is not news to you.

It would be easier if you could post a code sample that demonstrates the problem you are debugging.

During my research, I also used this link, but might not be very useful: WM_ENDSESSION

1
votes

Yes, shutdown logic has been changed, but not in Windows 8. It was changed way back in Vista, and the changes are documented on MSDN:

Application Shutdown Changes in Windows Vista

It is not enough to simply handle WM_QUERYENDSESSION and WM_ENDSESSION alone, there are new API functions involved to interact with the shutdown process.

-1
votes

Also it seems that the custom string provided when calling ShutdownBlockReasonCreate() does not get updated in Windows 8/8.1. It just displays the initial string, but no updates to it. I'm trying to let the user know why and how long I'm going to block the shutdown.