Jump to content
Sign in to follow this  

Query if Shutdown initiated and $WM_ENDSESSION happens too late

Recommended Posts


My script below runs "ssh.exe", but because of network outages, or network layer change from LAN to WLAN it sometimes crashes. Then my script restarts it that's working flawlessly.

The problem ist when I try to shutdown the computer windows begins to close the applications. At some point in the shutdown process it also closes ssh.exe, but because my script is still running (I don't know why the shutdown process always kills ssh.exe first) it tries to restart ssh.exe. But ssh.exe can not be started anymore because windows is in shutdown mode and a messagebox with a red cross icon comes up saying the you cannot start applications while the computer is shutting down. My application then detects that ssh.exe has not been started and tries to restart it. This floods my screen with these messageboxes all over until I cancel the shutdown or windows closes the autoit script.

So I tried not to restart ssh.exe once shutdown has started but my script is notified far too late of the ENDSESSION event. The Messagebox I added comes up just before the script ist terminated, but until this notification arrives the script has already flooded the screen. Can anyone help please?

If Not ProcessExists("ssh.exe") Then

    Const $WM_ENDSESSION = 0x0016 , $ENDSESSION_CLOSEAPP = 0x1 , $ENDSESSION_CRITICAL = 0x40000000 ,$ENDSESSION_LOGOFF = 0x80000000
    GUIRegisterMsg($WM_ENDSESSION , "Computer_Shutdown")

    Global $b_ShutdownInitiated = False

    While 1
        ShellExecuteWait("C:\Program Files\OpenSSH\bin\ssh.exe",'-F "C:\Program Files\OpenSSH\config" shadowsrealm',"C:\Program Files\OpenSSH","",@SW_HIDE)
        If $b_ShutdownInitiated = True then

;  http://msdn.microsoft.com/en-us/library/aa376890(VS.85).aspx
Func Computer_Shutdown($hwnd, $uMsg, $wParam, $lParam)
    $b_ShutdownInitiated = True
        Case $lParam = $ENDSESSION_CLOSEAPP
            $wParam = True
        Case $lParam = $ENDSESSION_CRITICAL

        Case $lParam = $ENDSESSION_LOGOFF
            Return False

    Return  False

Thanks for any help

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  

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...