Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Run before shutdown


  • Please log in to reply
15 replies to this topic
haichen
  • Members
  • 200 posts
  • Last active: Oct 20 2013 01:14 PM
  • Joined: 05 Feb 2007
After seeing this thread from Al for script execution before logoff/shutdown in the questionsection i tried to make a script. After some attempts I get this:

edit: I changed the script, because the old version worked not on all systems. I hope this will do it.

#Persistent
SetTimer, RunBeforeShutdown, Off
DllCall("kernel32.dll\SetProcessShutdownParameters", UInt, 0x4FF, UInt, 0)
OnMessage(0x11, "WM_QUERYENDSESSION")
Return

WM_QUERYENDSESSION(wParam, lParam)
  {
    static sdtry = 0
    ENDSESSION_Logoff = 0x80000000
    If (lParam & ENDSESSION_Logoff) ; User is logging off.
      EventType = Logoff
    Else     ; System is either shutting down or restarting. and
      EventType = Shutdown
    If (EventType = "Shutdown" and sdtry = 0){
      sdtry = 1
      SetTimer, RunBeforeShutdown, On
      Return false ; Tell the OS to Abort the Shutdown/Logoff.
    }
    Else If (EventType = "Shutdown" and sdtry = 1)
      Return true
  }


runBeforeShutdown:
  SetTimer, RunBeforeShutdown, Off
  ;-------your code below
  RunWait, c:\windows\notepad.exe,c:\windows
  MsgBox, system shuts down
  ;-------
  Shutdown,9 ; your choice what to do
  ExitApp
Return

Its a modificated script from the manual for OnMessage().
Seems to be impossible to get information what kind of shutdown is running. So you must do the choice.

tic
  • Members
  • 1925 posts
  • Last active: Today, 05:58 PM
  • Joined: 22 Apr 2007
OnExit, ExitSub
Return

ExitSub:
If A_ExitReason in Logoff,Shutdown
MsgBox, Shutting down
ExitApp
Return

Seems to be impossible to get information what kind of shutdown is running.


This is built into ahk as A_ExitReason

DeWild1
  • Members
  • 369 posts
  • Last active: Feb 28 2014 08:15 PM
  • Joined: 30 Apr 2006

OnExit, ExitSub
Return

ExitSub:
If A_ExitReason in Logoff,Shutdown
MsgBox, Shutting down
ExitApp
Return


Seems to be impossible to get information what kind of shutdown is running.


This is built into ahk as A_ExitReason


He may have meant that you can not tell if "shutdown" is reboot or shutdown.

haichen
  • Members
  • 200 posts
  • Last active: Oct 20 2013 01:14 PM
  • Joined: 05 Feb 2007
Yes, thanks.
The Problem is, that the script dont know what kind of shutdown is stopped.
You stop it and after running your code you must restart shutdown. But you dont know if it was reboot or normal shutdown.

tic
  • Members
  • 1925 posts
  • Last active: Today, 05:58 PM
  • Joined: 22 Apr 2007

Yes, thanks.
The Problem is, that the script dont know what kind of shutdown is stopped.
You stop it and after running your code you must restart shutdown. But you dont know if it was reboot or normal shutdown.


I dont think you do. On my machine it will continue doing whatever it was told to do.

haichen
  • Members
  • 200 posts
  • Last active: Oct 20 2013 01:14 PM
  • Joined: 05 Feb 2007
Hi tic, thanks for your reply.
Sorry for my english, but I dont understand what you mean. Does the shutdown not work? At my system (amd 1800, wxp) it works. After chosen shutdown or reboot notepad starts. Closing this, the messagebox appears. And clicking ok is followed by the final shutdown.

edit:
Ok I tried it on another system and it do not shutdown!

haichen
  • Members
  • 200 posts
  • Last active: Oct 20 2013 01:14 PM
  • Joined: 05 Feb 2007
I changed the script in the first post. I tested it at three different WinXP systems. Oh what a long time starting windows ...
Does it run now as it should?

DeWild1
  • Members
  • 369 posts
  • Last active: Feb 28 2014 08:15 PM
  • Joined: 30 Apr 2006
Another thing I do in combo with a shut down script..
#Persistent 
DetectHiddenWindows, On 
DetectHiddenText, On 
WinWaitActive,, Turn off computer 
WinKill, A


DeWild1
  • Members
  • 369 posts
  • Last active: Feb 28 2014 08:15 PM
  • Joined: 30 Apr 2006
Vista, darn Vista... The below does not work on Vista.. Any one know how to tell what win name of the grayed out screens that Vista pops up with for the little bit of extra help I do in the middle of this script?
Or what needs to be changed to make the rest of the code work on Vista?

#NoTrayIcon
#Persistent 

#Persistent
SetTimer, RunBeforeShutdown, Off
DllCall("kernel32.dll\SetProcessShutdownParameters", UInt, 0x4FF, UInt, 0)
OnMessage(0x11, "WM_QUERYENDSESSION")
Return

WM_QUERYENDSESSION(wParam, lParam)
  {
    static sdtry = 0
    ENDSESSION_Logoff = 0x80000000
    If (lParam & ENDSESSION_Logoff) ; User is logging off.
      EventType = Logoff
    Else     ; System is either shutting down or restarting. and
      EventType = Shutdown
    If (EventType = "Shutdown" and sdtry = 0){
      sdtry = 1
      SetTimer, RunBeforeShutdown, On
      Return false ; Tell the OS to Abort the Shutdown/Logoff.
    }
    Else If (EventType = "Shutdown" and sdtry = 1)
      Return true
  }

;///// a little extra help here... In XP it kills the shutdown then does whatever
DetectHiddenWindows, On 
DetectHiddenText, On 
WinWaitActive,, Turn off computer 
WinKill, A

runBeforeShutdown:
msgbox, hello 
  ExitApp
Return


haichen
  • Members
  • 200 posts
  • Last active: Oct 20 2013 01:14 PM
  • Joined: 05 Feb 2007
Have you seen this ? link
I dont have vista and so I cant help more (my knowledge is also limited).
This side may also help (German) link

DeWild1
  • Members
  • 369 posts
  • Last active: Feb 28 2014 08:15 PM
  • Joined: 30 Apr 2006

Have you seen this ? link
I dont have vista and so I cant help more (my knowledge is also limited).
This side may also help (German) link


Thank you, I will read, it when I am sober. :shock:

thenok
  • Members
  • 4 posts
  • Last active: Feb 27 2010 10:26 AM
  • Joined: 08 Apr 2009
I have windows7 and by some cool feature, wich i dont know how to disable
windows closes all my aplications if i do not click cancel in a about 10 seconds after the dialogue
Some aplications are still running bla bla
Force shutdown Cancel

Can anyone tell me how to disable my windows from doing force shutdown every time?
msconfig
run
control panel
anything

crxvfr
  • Members
  • 89 posts
  • Last active: Aug 24 2011 05:01 PM
  • Joined: 10 Mar 2006

I have windows7 and by some cool feature, wich i dont know how to disable
windows closes all my aplications if i do not click cancel in a about 10 seconds after the dialogue
Some aplications are still running bla bla
Force shutdown Cancel

Can anyone tell me how to disable my windows from doing force shutdown every time?
msconfig
run
control panel
anything


Several months later......
(I'm searching this topic for a solution I need, might help)
<!-- m -->http://www.thewindow... ... -windows-7<!-- m -->

braynyac
  • Members
  • 14 posts
  • Last active: Oct 19 2010 08:08 PM
  • Joined: 06 Oct 2010
Resurrecting the post...

We have need of preventing log off on a Windows 2008 R2 Remote Desktop Services (terminal server) Server. I have taken a look at the code, and attempted to run it, but it does not work. The message will pop up, but it does not stop the logoff process.

Here is the situation - our users connect to a linux server via PuTTY, and have a nasty habit of just logging off their Windows session while PuTTY is still up and active. This causes all sorts of little issues on our linux server, which does not like active processes running....so, I was able to create an AHK script to prevent users from clicking on the X to close out of PuTTY, which is another issue we have (thanks to MasterFocus's nice WinTrigger template, and SKAN's nice DisableCloseButton function). However, users can still kill PuTTY by logging off, as the log off process kills PuTTY rather than sending a nice "exit" or "logout" command.

I have looked high and low for a solution, and am running out of options. I can't disable the Log Off button on the Start Menu (which would be my first choice, if possible, as I have a logoff script which checks if PuTTY is open before logging off), and haven't been able to get a log off script to work correctly (they seem to run AFTER all the applications are closed, so not very useful).

I saw this, and thought - if I could intercept the logoff like this, that would work, but it's not working on Win7 / 2008 R2.

There was a link to an MS article: http://msdn.microsof...y/ms700677.aspx which I didn't understand, but I'm hoping some of you more knowledgeable coders might have an answer.

Thanks in advance, (and sorry for sort-of hijacking this thread),

Braynyac

braynyac
  • Members
  • 14 posts
  • Last active: Oct 19 2010 08:08 PM
  • Joined: 06 Oct 2010
Found this thread (http://www.autohotke...p=390343#390343) which *seemed* to have found a way to do what I wanted, but wasn't quite right...I played around a bit, and found AutoHotkey_L (Thanks Lexikos!), which worked correctly the VERY FIRST TIME I tried it!

Hope this helps,

~Braynyac