Jump to content

Windows 7 / 8.1 / 10 WM_ENDSESSION Behavior

Recommended Posts

Contrary to what Microsoft says here about WM_ENDSESSION


I am not receiving the message on shutdown on any of Windows 7 / 8.1 / 10, if I return TRUE (1) to WM_QUERYENDSESSION. I'm only receiving WM_ENDSESSION if I return FALSE (0) to WM_QUERYENDSESSION. This is rather annoying, as I don't want to halt shutdown, just do some application cleanup. And according to MS, that should be done in WM_ENDSESSION, not the former.

For now I can work around this in 1 of 2 ways:

1) If my app has a GUI, do the cleanup in WM_QUERYENDSESSION

2) If my app does not have a GUI (note I must create a hidden GUI to receive the messages), I return FALSE to QUERY and do my cleanup in END. Also note that Windows does NOT display the Cancel shutdown dialog after returning FALSE to QUERY if there is no visible GUI window.

See my simple sample code below. Try it with or without displaying the GUI and varying the return of QUERY, and see when and where things are logged. Really I'm just looking for confirmation that MS is full of crap and things don't really work the way they say they should. You can just log out instead of a full shutdown, the result is the same.

#include <WinAPISys.au3>
#include <WindowsConstants.au3>

Global $hGui
Global $bExit = False


Func _Main()

    $hGui = GUICreate("")


    While 1
        If $bExit Then ExitLoop

Func QUERY($hWnd, $iMsg, $wParam, $lParam)
    FileWriteLine(@DesktopDir & "\QUERY.txt", @HOUR & ":" & @MIN & ":" & @SEC & @TAB & _
        $hWnd & " : " & $iMsg & " : " & $wParam & " : " & $lParam)

    Return 1

Func END($hWnd, $iMsg, $wParam, $lParam)
    FileWriteLine(@DesktopDir & "\END.txt", @HOUR & ":" & @MIN & ":" & @SEC & @TAB & _
        $hWnd & " : " & $iMsg & " : " & $wParam & " : " & $lParam)

    $bExit = True
    Return 0


Edited by wraithdu

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...