Jump to content

Detect Windows Shutdown/Logoff/Restart event ?

Recommended Posts

Is it possible to detect when windows is shutting down, restarting, or logging off.

I have two applications that don't seem to be able to figure that out, and it causes all kinds of errors during the above events.

So I'd like to have a little script running in the background that will detect the event and kill the two processes.



 "I believe that when we leave a place, part of it goes with us and part of us remains... Go anywhere, when it is quiet, and just listen.. After a while, you will hear the echoes of all our conversations, every thought and word we've exchanged.... Long after we are gone our voices will linger in these walls for as long as this place remains."

Link to comment
Share on other sites

  • 1 year later...

There were several "intercept shutdown" examples in the forum.
This is one of them:

GUIRegisterMsg($WM_QUERYENDSESSION, "Cancel_Shutdown")

; set highest notification level
If Not _SetProcessShutdownParameters(0xFFF) Then ; MSDN says maximum is 0x4FF, but it worked for me
    If Not _SetProcessShutdownParameters(0x4FF) Then ; MSDN says this is reserved for System, but worked for me
        _SetProcessShutdownParameters(0x3FF) ; highest not reserved number, if everything else does not work

Global $b_ShutdownInitiated = False

While 1
    If $b_ShutdownInitiated = True then
        $b_ShutdownInitiated = False
        MsgBox(48 + 8192 + 262144, "Installer", "Ending your user session has been disabled to allow the installation to complete." _
        & @CRLF & @CRLF & "You will be able to shutdown/reboot/logoff once the installation process has completed." , 8)

Func Cancel_Shutdown($hWndGUI, $MsgID, $WParam, $LParam)
    $b_ShutdownInitiated = True
    ; Windows shows a not responding box after ~5 secs and allows to kill your app.
    Return False

Func _SetProcessShutdownParameters($dwLevel, $dwFlags=0)
    ; http://msdn.microsoft.com/en-us/library/ms686227%28VS.85%29.aspx
    ; Prog@ndy
    Local $aResult = DllCall("Kernel32.dll", "int", "SetProcessShutdownParameters", "dword", $dwLevel, "dword", $dwFlags)
    If @error Then Return SetError(1,0,0)
    Return $aResult[0]

Link to comment
Share on other sites

thanks I will test it, but it will be interesting yet, because actually I have a service running which needs to know this so it does not start doing some stuff during shutdown. So I will have to make this other process maybe write some value into registry and the service will check it before running the risky functions...

Link to comment
Share on other sites

  • 2 weeks later...

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...