Jump to content

Intercepting a "reboot" command and convert it to "shutdown"?


Roman
 Share

Recommended Posts

Hi,

Yes it is.

#include <WindowsConstants.au3>

Global $blQuit = False

#region GUI
$GUI = GUICreate("QUERYENDSESSION EVENT")

GUIRegisterMsg($WM_QUERYENDSESSION, "WM_QUERYENDSESSION")
GUIRegisterMsg($WM_ENDSESSION, "WM_ENDSESSION")
GUIRegisterMsg($WM_POWERBROADCAST, "WM_POWERBROADCAST")

GUISetState(@SW_HIDE, $GUI)
#endregion GUI

_SetProcessShutdownParameters(0xFFF) ;set highest notification level
_ThreadExecutionState_Set()

While 1
If $blQuit Then ExitLoop
Sleep(1000)
WEnd

Shutdown(1) ;convert to normal shutdown

Exit 0

Func WM_ENDSESSION($hWnd, $iMsg, $wlParam, $ilParam)
Return False
EndFunc ;==>WM_ENDSESSION

Func WM_QUERYENDSESSION($hWnd, $iMsg, $wlParam, $ilParam)
$blQuit = True
Return False ;block endsession
EndFunc ;==>WM_QUERYENDSESSION

Func WM_POWERBROADCAST($hWnd, $iMsg, $wlParam, $ilParam)
Return 1
EndFunc ;==>WM_POWERBROADCAST

Func _ThreadExecutionState_Set()
;The ES_AWAYMODE_REQUIRED value should be used only when absolutely necessary by media applications that require the system
;to perform background tasks such as recording television content or streaming media to other devices while the system appears to be sleeping
Local $aResult = DllCall("kernel32.dll", "int", "SetThreadExecutionState", "int", BitOR($ES_CONTINUOUS, $ES_SYSTEM_REQUIRED, $ES_AWAYMODE_REQUIRED))
If @error Then Return SetError(1, 0, 0)

Return $aResult[0]
EndFunc ;==>_ThreadExecutionState_Set

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 ;==>_SetProcessShutdownParameters

Br, FireFox.

Edited by FireFox
Link to comment
Share on other sites

Hi

Thanks! But I get some AutoIt errors (some variables "...possibly used before declaration")?

I would be very interested in your script, although I don't understand why do you use "GUI commands"? Maybe I didn't pose my question very exactly:

I want to intercept every and any Windows reboot, never mind if it is initiated by user, an application or by the system itself, e.g. after an application installation or after a reboot initiated by microsoft update(s).

Kind regards,

Roman.

Edited by roman
Link to comment
Share on other sites

Because Windows broadcasts the message WM_QUERYENDSESSION to all windows, hence why you need a GUI. Also learn how to debug correctly please, if you had searched the help file for those variables you would have found that WindowsConstants.au3 needs to be included.

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

It doesn't run as well with WindowsConstants.au3 included, but never mind; I may be an AutoIt dummy - that's true - and simply hoped to get some fast help in this forum.

I apologize for my ignorance and will do now some research and debugging for my own.

Regards,

Roman.

Link to comment
Share on other sites

You're not a dummy, add these to the top of the script as well. Also you did get some fast help.

Global Const $ES_AWAYMODE_REQUIRED = 0x00000040
Global Const $ES_CONTINUOUS = 0x80000000
Global Const $ES_SYSTEM_REQUIRED = 0x00000001

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

My bad... just stripped some parts of a project and removed the Constants.

@rudi

If you mean shutdown by pressing the power button for few seconds then it's not possible, otherwise it is.

Edited by FireFox
Link to comment
Share on other sites

No idea without doing some research. I would presume No, but that's a presumption.

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

Sorry for hijacking this thread but I wanted to know whether it is possible to intercept a process kill. E.g. when my script without GUI is running via Task Scheduler and somebody presses the stop button in TS for that particular process (my script) how can I intercept it and write it to a log file?

The interessting part is the interception!

Br,

UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

Hi Firefox.

@rudi

If you mean shutdown by pressing the power button for few seconds then it's not possible, otherwise it is.

Well, I was wondering about that nice "Shutdown -r -t 0 -f", but ACPI initiated "shutdown" might be a forced one as well. A "Brute" poweroff using the powerbutton itself is propably "un-interceptable" -- on the other hand: That signal is going "through" the motherboard, so there might be a chance, to reset the "hold-time-counter" there.

Regards, Rudi.

Edited by rudi

Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Link to comment
Share on other sites

Well, I was wondering about that nice "Shutdown -r -t 0 -f", but ACPI initiated "shutdown" might be a forced one as well.

Try it yourself :), but I'm almsot sure that It will be intercepted otherwise setting the notification level would be useless.

A "Brute" poweroff using the powerbutton itself is propably "un-interceptable" -- on the other hand: That signal is going "through" the motherboard, so there might be a chance, to reset the "hold-time-counter" there.

That's what I meant by "it's not possible", so good catch.

Edit : @UEZ

This is a good question, If you think about it I would say It's not possible to intercept it with the process being killed (have you ever seen a process unkillable when you have all the permissions to kill it?) but with another one it's another question. (then it's useless [ProcessExists etc])

Br, FireFox.

Edited by FireFox
Link to comment
Share on other sites

Well, I don't want to run an additional script to monitor the main script. Is there any triggering from the TS to the any winapi or the scripts that says "I'm going to stop you now". In this case you can do the last actions before it will shutdown the process.

Br,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

@UEZ,

After some searches, I have not found what you were looking for; I would say almost :

If you click on the button "End task" after selecting the window in the TaskManager then it will send a $WM_CLOSE message to the GUI; if you block this message by returning False, then the TaskManager will think that the application is not responding so it will suggest you to kill the application, which is not very good.

So I have found an alternative, when the GUI receive the $WM_CLOSE message, the script execute another instance and close the first one, so there is always one instance running.

Instead of that you could make your last actions and exit normally.

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Opt("GUIOnEventMode", 1)

#region GUI
GUICreate("toto")
GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")

GUIRegisterMsg($WM_CLOSE, "WM_CLOSE")
GUISetState()
#endregion GUI

While 1
Sleep(100)
WEnd

Func WM_CLOSE($hWnd, $iMsg, $wParam, $lParam)
Switch $iMsg
Case $WM_CLOSE
ShellExecute(@ScriptFullPath)
EndSwitch

_Exit()
EndFunc ;==>WM_CLOSE

Func _Exit()
Exit 0
EndFunc ;==>_Exit

Br, FireFox.

Edited by FireFox
Link to comment
Share on other sites

Thank you very much FireFox!

Creating a dummy hidden window and GUIRegisterMsg($WM_CLOSE, "WM_CLOSE") did the trick!

Br,

UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

Good find Firefox

Spoiler

“Hello, ladies, look at your man, now back to me, now back at your man, now back to me. Sadly, he isn’t me, but if he stopped using ladies scented body wash and switched to Old Spice, he could smell like he’s me. Look down, back up, where are you? You’re on a boat with the man your man could smell like. What’s in your hand, back at me. I have it, it’s an oyster with two tickets to that thing you love. Look again, the tickets are now diamonds. Anything is possible when your man smells like Old Spice and not a lady. I’m on a horse.”

 

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