Hey guys,
I want to create an AutoHotkey script that executes a Batch file and prevents Windows from Logoff/Shutdown before the Batch has finished.
Nothing permanent. Something like an unsaved document which is still open and you have to save and close it before logging off.
This AutoHotkey script (https://autohotkey.com/board/topic/2398 ... -shutdown/) seems to be the closest there is but I can't get it to work for my purpose.
Run Batch and prevent Windows from Logoff/Shutdown before completion
Re: Run Batch and prevent Windows from Logoff/Shutdown before completion
I created an scheduled task which gets triggered at Event 4647. Tested it with the System account and can confirm it works! What I want is for my short batch file (2-3 seconds to completion) at "C:\Test.bat" to run as the current user right before he logs off. It shouldn't matter if it is a proper logoff, shutdown or restart. I changed and tested the script multiple times but it just doesn't work so I tried it one last time with no modifications except the path of my batch file but it still doesn't work. Also tried to execute the AutoHotkey script manually before the logoff but it makes no difference. I would really appreciate some help because I can't get it done...
Code: Select all
#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:\Test.bat
MsgBox, system shuts down
;-------
Shutdown,9 ; your choice what to do
ExitApp
Return
Who is online
Users browsing this forum: No registered users and 145 guests