Jump to content

Prevent Shutdown/Sleep in Vista/7


Manko
 Share

Recommended Posts

HI!

I will start off by thanking Prog@ndy and others that have done examples of this for XP! Great work!

Now I had a need of this also in Vista/7 and found nothing here (Bad searching skills?) , the net had some but was really confused about how to do it and what different calls and techniques actually do... I had to sift through a lot of bogus claims and incomplete examples...

I have tested a lot and come up with this example code. Hope it will be useful!

;Global $ES_AWAYMODE_REQUIRED=0x00000040
Global $ES_CONTINUOUS=0x80000000
Global $ES_DISPLAY_REQUIRED=0x00000002
Global $ES_SYSTEM_REQUIRED=0x00000001
;Global $ES_USER_PRESENT=0x00000004
Global $PBT_APMSUSPEND = 0x0004
Global $SPI_SETSCREENSAVETIMEOUT=15
Global $WM_QUERYENDSESSION = 0x11
Global $hGUI=GUICreate("")
Global Const $WM_POWERBROADCAST = 0x0218
; We wanna be first in the queue when notifications come about shutdown/sleep...
If Not _SetProcessShutdownParameters(0xFFF) Then ; MSDN says maximum is 0x4FF, but it worked for me (Prog@ndy)
    If Not _SetProcessShutdownParameters(0x4FF) Then ; MSDN says this is reserved for System, but worked for me (Prog@ndy)
        _SetProcessShutdownParameters(0x3FF) ; highest not reserved number, if everything else does not work
    EndIf
EndIf
GUIRegisterMsg($WM_QUERYENDSESSION, "Cancel_Shutdown") ; enough for windows xp, but in Vista/7, we will add "Shutdownblockreason"...
; without this, computer will sometimes shutdown but sometimes be saved by the app_unresponsive screen after some or no apps close...
; The descriptive text should be short and to the point or users might ignore....
DllCall("User32.dll", "int", "ShutdownBlockReasonCreate", "int", $hGUI, "wstr", "We're not allowing shutdown, cause we damn well feel like it!")
; ***************** In Vista/7 there is no longer a way to prevent Sleep/hibernate instigated by user action, cause of the risk of backpack-attack.... ********* ;
GUIRegisterMsg($WM_POWERBROADCAST, "Cleanup") ; But we still have about 2secs to do some cleanup...
; This prevents computer from idling off... This tells the comp, this thread needs screen and system even if idle-timers expire...
DllCall("kernel32.dll", "int", "SetThreadExecutionState", "int", BitOR($ES_CONTINUOUS, $ES_DISPLAY_REQUIRED, $ES_SYSTEM_REQUIRED))
While 1
    sleep(10)
WEnd
Func Cancel_Shutdown($hWndGUI, $MsgID, $WParam, $LParam)
    ; DO NOT ADD A MSGBOX HERE
    ; Windows shows a not responding box after ~5 secs and allows to kill your app.
    Return False ; DO NOT SHUTDOWN
EndFunc
Func Cleanup($hwnd, $msg, $wparam, $lparam)
if $wparam=$PBT_APMSUSPEND then
  ; We have about 2 secs...
  ; Do some cleanup!
  ; ...or whatever
EndIf
Return 1
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

What we can and can't do:

We can prevent shutdown and we can prevent Sleep/Hibernate due to idle-timers, but we can't prevent userdriven Sleep/Hibernate cause Microsoft got afraid of getting sued for exploding backpacks... hrm... We have atleast been left with a 2sec window for doing some cleanup...

All effects will be negated by shutting app down...

/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

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...