Jump to content

cancel shutdown


hot202
 Share

Recommended Posts

i have this script and i have tryed many on here but i cant find any that keep the program running when it stops the shut down is there anyway u can keep the autoit program running or a msgbox pop up saying if u shut down it will exit the autoit script and if u cancel it wont shut down and the autoit script wont close?

Opt("TrayIconHide", 1)

$WM_QUERYENDSESSION = 0x0011
GUIRegisterMsg($WM_QUERYENDSESSION, "Cancel_Shutdown")
GUICreate("PreventShutdownGUI")
GUISetSTate(@SW_HIDE)
Global $b_ShutdownInitiated = False


While 1
    If $b_ShutdownInitiated = True then
        ;Do stuff here
        ;do some more stuff here
        ;DoTheShutDownStuff();maybe do some more stuff here ;) 
        ;Shutdown();Maybe you want to shutdown the computer when done
        Exit;otherwise, you just exit this script
    EndIf
    sleep(10)
WEnd

Func Cancel_Shutdown($hWndGUI, $MsgID, $WParam, $LParam)
;~     run(@ScriptDir & "\killpgm.exe")
    $b_ShutdownInitiated = True
    Return False
EndFunc
Link to comment
Share on other sites

Something like this?

Opt("TrayIconHide", 1)

$WM_QUERYENDSESSION = 0x0011
GUIRegisterMsg($WM_QUERYENDSESSION, "Cancel_Shutdown")
GUICreate("PreventShutdownGUI")
GUISetSTate(@SW_HIDE)
Global $b_ShutdownInitiated = False


While 1
    If $b_ShutdownInitiated = True then $test=MsgBox(1,"Do you want to shutdown","OK to shutdown and Cancel to abort")
    If $test = 2 then sleep(10000) ;user chose not to shutdown
    Else Exit ;user chose to shutdown and the script exits
    EndIf
WEnd

Func Cancel_Shutdown($hWndGUI, $MsgID, $WParam, $LParam)
;~     run(@ScriptDir & "\killpgm.exe")
    $b_ShutdownInitiated = True
    Return False
EndFunc
Link to comment
Share on other sites

whatever Edited by MvGulik

"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Link to comment
Share on other sites

whatever Edited by MvGulik

"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Link to comment
Share on other sites

Well, if you do manage to detect a shutdown, then i suggest the following. When you detect the shutdown, automatically cancel the shutdown with the code mentioned above (Shutdown -a), and then if the user chooses to continue the shutdown, reinitiate the shutdown with a (Shutdown -s -t 0 - i think).

Good luck :mellow:

Link to comment
Share on other sites

  • 1 year later...

Hi,

First sorry for my bad English

I try this script on XP it works perfectly

On Vista/Seven it didn't works.

I read that we must use DllCall("User32.dll", "int", "ShutdownBlockReasonDestroy" / DllCall("User32.dll", "int", "ShutdownBlockReasonCreate"

Does someone got experience for "Aborting Shutdown" in Vista/Seven OS.

Thanks,

David

Link to comment
Share on other sites

#Include <ButtonConstants.au3>
#Include <GUIConstantsEx.au3>
#Include <WinAPIEx.au3>

Opt('MustDeclareVars', 1)

If _WinAPI_GetVersion() < '6.0' Then
    MsgBox(16, 'Error', 'Require Windows Vista or later.')
    Exit
EndIf

Global $hForm, $Msg, $Button, $Check

$hForm = GUICreate('MyGUI', 200, 200)
$Button = GUICtrlCreateButton('', 73, 62, 54, 54, $BS_ICON)
GUICtrlSetImage(-1, @SystemDir & '\shell32.dll', 45)
GUICtrlSetTip(-1, 'Log off ' & @UserName)
$Check = GUICtrlCreateCheckBox('Block Windows shutdown', 10, 173, 144, 21)
GUIRegisterMsg(0x0011, 'WM_QUERYENDSESSION')
GUISetState()

; Set the highest shutdown priority for the current process to prevent closure the other processes.
_WinAPI_SetProcessShutdownParameters(0x03FF)

While 1
    $Msg = GUIGetMsg()
    Switch $Msg
        Case $GUI_EVENT_CLOSE
            ExitLoop
        Case $Button
            Shutdown(0)
        Case $Check
            If GUICtrlRead($Check) = $GUI_CHECKED Then
                _WinAPI_ShutdownBlockReasonCreate($hForm, 'This application is blocking system shutdown because the saving critical data is in progress.')
            Else
                _WinAPI_ShutdownBlockReasonDestroy($hForm)
            EndIf
    EndSwitch
WEnd

Func WM_QUERYENDSESSION($hWnd, $Msg, $wParam, $lParam)
    Switch $hWnd
        Case $hForm
            If _WinAPI_ShutdownBlockReasonQuery($hForm) Then
                Return 0
            EndIf
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_QUERYENDSESSION

Link to comment
Share on other sites

Another example.

#Include <GUIConstantsEx.au3>
#Include <WinAPIEx.au3>

Global $Shutdown = False

$hForm = GUICreate('MyGUI', 400, 200)
$Label = GUICtrlCreateLabel('Saving data in progress...', 20, 74, 160, 14)
$Progress = GUICtrlCreateProgress(20, 90, 360, 20)
GUIRegisterMsg($WM_QUERYENDSESSION, 'WM_QUERYENDSESSION')
GUIRegisterMsg($WM_ENDSESSION, 'WM_ENDSESSION')
GUISetState()

_WinAPI_SetProcessShutdownParameters(0x03FF)

For $i = 0 To 100
    _WinAPI_ShutdownBlockReasonCreate($hForm, 'This application is blocking system shutdown because saving critical data is in progress... ' & $i & '%')
    GUICtrlSetData($Label, 'Saving data in progress... ' & $i & '%')
    GUICtrlSetData($Progress, $i)
    Sleep(500)
Next
_WinAPI_ShutdownBlockReasonDestroy($hForm)

If $Shutdown Then
    Exit
EndIf

Do
Until GUIGetMsg() = -3

Func WM_QUERYENDSESSION($hWnd, $Msg, $wParam, $lParam)
    Switch $hWnd
        Case $hForm
            If _WinAPI_ShutdownBlockReasonQuery($hForm) Then
                $Shutdown = True
                Return 0
            EndIf
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_QUERYENDSESSION

Func WM_ENDSESSION($hWnd, $Msg, $wParam, $lParam)
    Switch $hWnd
        Case $hForm
            If Not $wParam Then
                $Shutdown = False
                Return 0
            EndIf
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_ENDSESSION
Link to comment
Share on other sites

  • 10 months later...

Hello Friends, I played around with the script like that one from Yashied . Function with Vista Ultimate and Win7 pro. I got no result on Vista Home. Does anyone have an idea why is that?

Greetings

Andrew

Thanks! :bye:

Greetings

Andrew

 

Link to comment
Share on other sites

Found a new problem: If the Window which should receive the shutdown message and return 0 is minimized it does not block the the shut down.

Make a gui, you dont show to user, so it wont be minimized, and let it receive shutdown request. If you need, you can have other guis for other stuff...

Yes i rush things! (I sorta do small bursts inbetween doing nothing.) Things I have rushed and reRushed:* ProDLLer - Process manager - Unload viri modules (dll) and moore...* _WinAPI_ProcessListOWNER_WTS() - Get Processes owner list...* _WinAPI_GetCommandLineFromPID() - Get commandline of target process...* _WinAPI_ThreadsnProcesses() Much info if expanded - optional Indented "Parent/Child"-style Processlist. Moore to come... eventually...
Link to comment
Share on other sites

...or rather register these messages BEFORE you create any GUI and see what happens. :oops:

Autoit already have an invisible window, to handle some messages, created before any code in your script runs.

Yes i rush things! (I sorta do small bursts inbetween doing nothing.) Things I have rushed and reRushed:* ProDLLer - Process manager - Unload viri modules (dll) and moore...* _WinAPI_ProcessListOWNER_WTS() - Get Processes owner list...* _WinAPI_GetCommandLineFromPID() - Get commandline of target process...* _WinAPI_ThreadsnProcesses() Much info if expanded - optional Indented "Parent/Child"-style Processlist. Moore to come... eventually...
Link to comment
Share on other sites

Hi Manko, I have a hidden GUI already. But the problem is that Autoit generates a Trayicon by default what is OK in general. Now I figured out that regular registered messages are not affected. But when I use:

GUIRegisterMsg(0x0011, 'WM_QUERYENDSESSION')

; GUIRegisterMsg($WM_ENDSESSION, 'WM_ENDSESSION')

; Set the highest shutdown priority for the current process to prevent closure the other processes.

_WinAPI_SetProcessShutdownParameters(0x03FF)

_WinAPI_ShutdownBlockReasonCreate($GuiTaskMngr, 'Taskmanager blockiert Shutdown.')

and the Tray Icon ist clicked the _WinAPI_ShutdownBlockReasonCreate($GuiTaskMngr, 'Taskmanager blockiert Shutdown.')

is not more in action. It seems that clicking on Tray Icon (not pause) breaks theat function.

The scrict does not recognize anymore a WM_QUERYENDSESSION message.

So even the mailfunction when Gui is minimized an have the reason in stealing focus and/or message by Tray Icon.

But anyway I will try your proposal.

Any idea on this?

Greetings Andrew

Thanks! :bye:

Greetings

Andrew

 

Link to comment
Share on other sites

I would guess there is some problem with how autoit handles messages or there is some problems with your script being tied to handle one thing when you would rather handle something else... ?

The quick and dirty fix, (Not so dirty, but not the elegant "wisdom-enhancing" solution.) is to make a separate program for just dealing with stopping shutdown or similar, and do everything else in first program, communicating between them or starting/stopping as needed.

We would like no visible GUI and NO trayicon:

#NoTrayIcon

/Manko

Yes i rush things! (I sorta do small bursts inbetween doing nothing.) Things I have rushed and reRushed:* ProDLLer - Process manager - Unload viri modules (dll) and moore...* _WinAPI_ProcessListOWNER_WTS() - Get Processes owner list...* _WinAPI_GetCommandLineFromPID() - Get commandline of target process...* _WinAPI_ThreadsnProcesses() Much info if expanded - optional Indented "Parent/Child"-style Processlist. Moore to come... eventually...
Link to comment
Share on other sites

Hi Manko, THX for your comment. Your both ideas are helpful. First I will try to fix it by using an event of the tray icon to switch back to the main GUI. If that doesnt help I will use

#NoTrayIcon.

Have a nice day and Thank you.

Greetings Andrew

Thanks! :bye:

Greetings

Andrew

 

Link to comment
Share on other sites

Andrew, you should report status of your progress since others are reading your thread in search of answers.

Your thread just got refered to as a "NO-Success", and I believe that might NOT be true...

Yes i rush things! (I sorta do small bursts inbetween doing nothing.) Things I have rushed and reRushed:* ProDLLer - Process manager - Unload viri modules (dll) and moore...* _WinAPI_ProcessListOWNER_WTS() - Get Processes owner list...* _WinAPI_GetCommandLineFromPID() - Get commandline of target process...* _WinAPI_ThreadsnProcesses() Much info if expanded - optional Indented "Parent/Child"-style Processlist. Moore to come... eventually...
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...