AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Shutdown Prevention Fix

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Bug Reports
View previous topic :: View next topic  
Author Message
psjw12



Joined: 11 May 2007
Posts: 25

PostPosted: Fri May 11, 2007 12:45 am    Post subject: Shutdown Prevention Fix Reply with quote

In the "OnMessage()" help section there is a sample script about how to prevent a computer shutdown or logoff.
There is one problem with it, all other processes are closed first before the shutdown can be blocked.
To make Windows attempt to close this script on shutdown before any other process do, place this dll call at the beginning of the script:

Code:
DllCall("kernel32.dll\SetProcessShutdownParameters", UInt, 0x4FF, UInt, 0)


This will cause the scripts process to be terminated first on shutdown or logoff.

If you run OnExit commands on shutdown this will cause the whole shutdown sequence to wait upon that script before Windows can continue to shutdown and end other processes.

If Windows is waiting for the script to terminate on shutdown you will recieve "End Task" prompt after 5 seconds.

To make a script the last process to terminate change "0x4FF" to "0x0FF".
Back to top
View user's profile Send private message
Chris
Site Admin


Joined: 02 Mar 2004
Posts: 10467

PostPosted: Fri May 11, 2007 1:35 am    Post subject: Reply with quote

I've revised that example to add the following:
Code:
; The following DllCall is optional: it tells the OS to shut down this script first (prior to all other applications).
; This call has no effect on Windows 9x.
DllCall("kernel32.dll\SetProcessShutdownParameters", UInt, 0x4FF, UInt, 0)
I've also taken the liberty of revising your post slightly to change "long" to "uint" (otherwise, the call probably won't work).

Thanks for improving the documentation.
Back to top
View user's profile Send private message Send e-mail
psjw12



Joined: 11 May 2007
Posts: 25

PostPosted: Fri May 11, 2007 7:33 pm    Post subject: My script Reply with quote

Code:
DllCall("kernel32.dll\SetProcessShutdownParameters", UInt, 0x4FF, UInt, 0, "int")

OnMessage(0x11, "WM_QUERYENDSESSION")
return

WM_QUERYENDSESSION(wParam, lParam)
{
    ENDSESSION_LOGOFF = 0x80000000
    if (lParam & ENDSESSION_LOGOFF) 
        EventType = Logoff
    else 
        EventType = Shutdown
    MsgBox, 4100, Confirm %EventType%, %EventType% is being attempted. Allow it?, 10
    IfMsgBox Yes
        return true
    IfMsgbox No
      {
      return false
      Msgbox, 4096 , %EventType% Blocked, %EventType% was blocked by user.
      }
   IfMsgbox Timeout
      {
      return false
      Msgbox, 4096 , %EventType% Auto Blocked, A %EventType% was automatically prevented.
      }
}


This is a shutdown preventer I made with the new DLL call but I can't get the message box to appear after the shutdown has been blocked, is there any way to do it other than draw a gui?
Back to top
View user's profile Send private message
toralf



Joined: 31 Jan 2005
Posts: 3842
Location: Bremen, Germany

PostPosted: Fri May 11, 2007 9:29 pm    Post subject: Reply with quote

after return no msgbox will be shown since the flow already left the function. you could show a tooltip before the return though.
_________________
Ciao
toralf
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Jon



Joined: 28 Apr 2004
Posts: 373

PostPosted: Thu May 17, 2007 4:31 pm    Post subject: Reply with quote

Thanks for posting this fix. I've just been trying to figure out how to prevent this issue.

Another problem I found is that windows will try to terminate the script if you don't return true or false soon enough. I'm working around this by returning false and then having the script shutdown the system depending on what the user clicks.

I've also used a timer so that a message-box is shown-

Code:
; The following DllCall is optional: it tells the OS to shut down this script first (prior to all other applications).
; This call has no effect on Windows 9x.

DllCall("kernel32.dll\SetProcessShutdownParameters", UInt, 0x4FF, UInt, 0)

OnMessage(0x11, "WM_QUERYENDSESSION")
return

WM_QUERYENDSESSION(wParam, lParam)
{

  ENDSESSION_LOGOFF = 0x80000000

  if (lParam & ENDSESSION_LOGOFF)
    Return
  else
  {
    SetTimer, Sure, 500
    return false
  }
}


Sure:

SetTimer, Sure, off

MsgBox, 4,, ShutDown in progress.  Allow it?

IfMsgBox Yes
{
  ShutDown, 1
  ExitApp
}
else
  return

Back to top
View user's profile Send private message Send e-mail
Guest






PostPosted: Thu May 17, 2007 10:29 pm    Post subject: Reply with quote

This is my shutdown preventer
Code:
#Persistent
#SingleInstance ignore
DllCall("kernel32.dll\SetProcessShutdownParameters", UInt, 0x4FF, UInt, 0, "int")

Menu,Tray,NoStandard
Menu, Tray, add, &Exit, Exit
Menu, Tray, Tip, Shutdown Stopper

OnMessage(0x11, "WM_QUERYENDSESSION")
return

WM_QUERYENDSESSION(wParam, lParam)
{
    ENDSESSION_LOGOFF = 0x80000000
    if (lParam & ENDSESSION_LOGOFF) 
        EventType = Logoff
    else
        EventType = Shutdown
    MsgBox, 4132, Confirm %EventType%, %EventType% is being attempted. Allow it?, 4.9
    IfMsgBox Yes
        return true
    IfMsgbox No
      {
      TrayTip , %EventType% Blocked, %EventType% was blocked by user., 30, 2
      return false
      }
   IfMsgbox Timeout
      {
      TrayTip ,  %EventType% Auto Blocked, %EventType% was automatically prevented at %A_hour%:%A_Min%:%A_Sec%., 864000, 2
      return false
      }
}

Exit:
ExitApp
Back to top
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Bug Reports All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group