Jump to content

Query if Shutdown initiated and $WM_ENDSESSION happens too late


Recommended Posts

Hi,

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
    AutoItSetOption("TrayIconHide",1)

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

    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
            Exit
        EndIf
    WEnd
EndIf

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

        Case $lParam = $ENDSESSION_LOGOFF
            MsgBox(0,"$ENDSESSION_LOGOFF","Computer_LOGOFF")
            Return False
    EndSelect

    MsgBox(0,"Computer_Shutdown","Computer_Shutdown")
    Return  False
EndFunc

Thanks for any help

Link to comment
Share on other sites

Link to comment
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
 Share

  • Recently Browsing   0 members

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