Advertisement
Guest User

Shutdown Blocker - AutoIt

a guest
Apr 5th, 2012
770
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
AutoIt 2.45 KB | None | 0 0
  1. ;; Written in AutoIt - www.autoitscript.com
  2.  
  3. Global $hWnd, Const $WM_QUERYENDSESSION = 0x11
  4.  
  5. ; Register Shutdown Message Handler - see
  6. ; http://msdn.microsoft.com/en-us/library/windows/desktop/aa376890%28v=vs.85%29.aspx
  7. GUIRegisterMsg($WM_QUERYENDSESSION, "Cancel_Shutdown")
  8.  
  9. ; Create Block-Reason
  10. $hWnd = GUICreate("PreventShutdownGUI")
  11. _ShutdownBlockReasonCreate($hWnd, "Sorry, AutoIt > Windows")
  12.  
  13. ; Register Cleanup Function
  14. OnAutoItExitRegister('_Cleanup')
  15.  
  16. ; Show Info in Taskbar
  17. TrayTip("Shutdown Blocker - Started", "Denies shutdowns from now on.", 20)
  18.  
  19. ; Idle around..
  20. While Sleep(1000)
  21. WEnd
  22.  
  23.  
  24. Func Cancel_Shutdown($hWndGUI, $MsgID, $WParam, $LParam)
  25.     ; Since we should return to the operating system asap,
  26.     ; we register an adlib function, wich will be called in 50ms.
  27.     AdlibRegister("Cancel_ShutdownResolver", 50)
  28.     Return False
  29. EndFunc   ;==>Cancel_Shutdown
  30.  
  31. Func Cancel_ShutdownResolver()
  32.     Local $iOldOption. $hWnd
  33.  
  34.     $iOldOption = Opt("WinTitleMatchMode", 4)
  35.     $hWnd = WinGetHandle("[CLASS:BlockedShutdownResolver]")
  36.     Opt("WinTitleMatchMode", $iOldOption)
  37.  
  38.  
  39.     If $hWnd Then
  40.         ; unregister adlib function.
  41.         AdlibUnRegister("Cancel_ShutdownResolver")
  42.  
  43.         ; Click the cancel button.
  44.         If @OSVersion = "WIN_7" Then ControlClick($hWnd, "", "[CLASS:Button; INSTANCE:1]")
  45.         If @OSVersion <> "WIN_7" Then ControlClick($hWnd, "", "[CLASS:Button; INSTANCE:2]")
  46.  
  47.         ; Show Info in Tray
  48.         TrayTip("Shutdown Blocker - Shutdown denied", "Denied shutdown successfull!", 5)
  49.  
  50.         ; ===================================|
  51.         ; Call your user defined function... |
  52.         _YourFunction() ; <================|
  53.         ; ===================================|
  54.     EndIf
  55. EndFunc   ;==>Cancel_ShutdownResolver
  56.  
  57. Func _YourFunction()
  58.     ;MsgBox(0,"","Do whatever you want here...")
  59. EndFunc   ;==>_YourFunction
  60.  
  61.  
  62. Func _Cleanup()
  63.     _ShutdownBlockReasonDestroy($hWnd)
  64. EndFunc   ;==>_Cleanup
  65.  
  66.  
  67.  
  68.  
  69.  
  70. Func _ShutdownBlockReasonCreate($hWnd, $wStr)
  71.     ; http://msdn.microsoft.com/en-us/library/ms...28VS.85%29.aspx
  72.     ; Prog@ndy
  73.     Local $aResult = DllCall("User32.dll", "int", "ShutdownBlockReasonCreate", "hwnd", $hWnd, "wstr", $wStr)
  74.     If @error Then Return SetError(1, 0, 0)
  75.     Return $aResult[0]
  76. EndFunc   ;==>_ShutdownBlockReasonCreate
  77.  
  78. Func _ShutdownBlockReasonDestroy($hWnd)
  79.     Local $aResult = DllCall("User32.dll", "int", "ShutdownBlockReasonDestroy", "hwnd", $hWnd)
  80.     If @error Then Return SetError(1, 0, 0)
  81.     Return $aResult[0]
  82. EndFunc   ;==>_ShutdownBlockReasonDestroy
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement