Sign in to follow this  
Followers 0
ken82m

Detect Windows Shutdown/Logoff/Restart event ?

8 posts in this topic

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.

Thanks,

Kenny


My Contributions _StringMultiReplace PC Builders Console - Secure PDF Creator - Cisco VPN Installer MS DNS Server Backup Script - MS DHCP Backup Script IT Admin Console - Toggle Admin Mode - MyMovies-Add Discs Script - IT Help Desk and System Information Tool - Set On Lid Close Power Option - Streaming Media Server & Website "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."

Share this post


Link to post
Share on other sites



Windows sends the WM_EndSession to all desktop windows when it is about to shutdown.

You can catch it with the GUIRegisterMsg() function.


Broken link? PM me and I'll send you the file!

Share this post


Link to post
Share on other sites

You might as well read up on this message as well. It's closely realted to WM_EndSession.


Broken link? PM me and I'll send you the file!

Share this post


Link to post
Share on other sites

but it seems that this works only for programs with GUI. If I have a process without window then the message is not sent to it or GUIRegisterMsg does not receive it... Too bad

Share this post


Link to post
Share on other sites

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

GUIRegisterMsg($WM_QUERYENDSESSION, "Cancel_Shutdown")
GUICreate("PreventShutdownGUI")
GUISetSTate(@SW_HIDE)

; 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
    EndIf
EndIf

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)
    EndIf
    sleep(10)
WEnd

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


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]
EndFunc

Share this post


Link to post
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...

Share this post


Link to post
Share on other sites

and how do the normal windows services do it? You think they have to be programmed that it does not matter if they are ended anytime or is there some programming interface which we don't have in autoit?

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
Sign in to follow this  
Followers 0