Jump to content

This site uses cookies. By continuing to browse the site you are agreeing to our use of cookies. Find out more here. X
X


Photo

Standby (WM_POWERBROADCAST) Hooking


  • Please log in to reply
12 replies to this topic

#1 g_BonE

g_BonE

    Seeker

  • Active Members
  • 18 posts

Posted 21 November 2008 - 02:17 PM

hi folks:

this i what im currently using to check standby messages from windows and react on them:

AutoIt         
#include "au3_inc\GuiConstantsEx.au3" $logfile = @ScriptDir&"\mp_standby-hook.log" $delystdby = 30   ;seconds to delay standby $mes = "" Global $WM_POWERBROADCAST         = 536 Global $PBT_APMQUERYSUSPEND       = 0x0000 Global $PBT_APMQUERYSTANDBY       = 0x0001 Global $PBT_APMQUERYSUSPENDFAILED = 0x0002 Global $PBT_APMQUERYSTANDBYFAILED = 0x0003 Global $PBT_APMSUSPEND            = 0x0004 Global $PBT_APMSTANDBY            = 0x0005 Global $PBT_APMRESUMECRITICAL     = 0x0006 Global $PBT_APMRESUMESUSPEND      = 0x0007 Global $PBT_APMRESUMESTANDBY      = 0x0008 Global $PBT_APMBATTERYLOW         = 0x0009 Global $PBT_APMPOWERSTATUSCHANGE  = 0x000A Global $PBT_APMOEMEVENT           = 0x000B Global $PBT_APMRESUMEAUTOMATIC    = 0x0012 Global $PBTF_APMRESUMEFROMFAILURE = 0x00000001 Global $ES_DISPLAY_REQUIRED       = 0x00000002; zero the display's idle timer Global $ES_SYSTEM_REQUIRED        = 0x00000001; zero the system's idle timer Global $ES_CONTINUOUS             = 0x80000000; keep the display or system on (doesn't work?) $hGUI = GUICreate("Standby", 1,1,1,1,-1) GUIRegisterMsg($WM_POWERBROADCAST, "Standby")  ;hook into win32 pm API GUISetState(@SW_MINIMIZE,$hGUI) $g_szVersion = "MP_StandBy-Hook" If WinExists($g_szVersion) Then Exit ; It's already running AutoItWinSetTitle($g_szVersion) WriteToLog($g_szVersion&" started...") While 1     $GUIMsg = GUIGetMsg()     Switch $GUIMsg         Case $GUI_EVENT_CLOSE             ExitLoop     EndSwitch     Sleep (60000) ; Sleep for a minute WEnd Func Standby($hWnd, $Msg, $wParam, $lParam)     Switch $wParam         Case $PBT_APMQUERYSUSPEND         ; gets called first on standby request             WriteToLog("PBT_APMQUERYSUSPEND received...")             For $i=$delystdby to 1 Step -1                 DllCall("kernel32.dll","int","SetThreadExecutionState","int", $ES_SYSTEM_REQUIRED)                 WriteToLog("Delaying Standby: "&$i&" s.")                 Sleep(1000)             Next         Case $PBT_APMSUSPEND              ; gets called second on standby request             WriteToLog("PBT_APMSUSPEND received...")             WriteToLog("Entering Standby...")         Case $PBT_APMRESUMEAUTOMATIC      ; gets called first after resume             WriteToLog("PBT_APMRESUMEAUTOMATIC received...")             WriteToLog("Resuming from Standby..."         Case $PBT_APMRESUMESUSPEND        ; gets called second after resume             WriteToLog("PBT_APMRESUMESUSPEND received...")             WriteToLog("Checking MediaPortal...")             Sleep(5000)             If Not ProcessExists("mediaportal.exe") Then                 Run("c:\_mp\mediaportal.exe","c:\_mp")             EndIf             Sleep(5000)             WriteToLog("Running RTC updater...")             ShellExecute("timeupdate.au3","","c:\_tvtools\")             Sleep(5000)             WriteToLog("Faking user interaction...")             $pos = MouseGetPos()             MouseMove($pos[0]+50, $pos[1]+50)             MouseMove($pos[0]-50, $pos[1]-50)             MouseMove($pos[0], $pos[1])         Case $PBT_APMQUERYSTANDBY             WriteToLog("PBT_APMQUERYSTANDBY received...")         Case $PBT_APMQUERYSUSPENDFAILED             WriteToLog("PBT_APMQUERYSUSPENDFAILED received...")         Case $PBT_APMQUERYSTANDBYFAILED             WriteToLog("PBT_APMQUERYSTANDBYFAILED received...")         Case $PBT_APMRESUMECRITICAL             WriteToLog("PBT_APMRESUMECRITICAL received...")         Case $PBT_APMRESUMESTANDBY             WriteToLog("PBT_APMRESUMESTANDBY received...")         Case $PBT_APMSTANDBY             WriteToLog("PBT_APMSTANDBY received...")         Case $PBT_APMPOWERSTATUSCHANGE             WriteToLog("PBT_APMPOWERSTATUSCHANGE received...")         Case $PBT_APMOEMEVENT             WriteToLog("PBT_APMOEMEVENT received...")         Case Else             WriteToLog("Some unknown standby related message received... WTF?")     EndSwitch EndFunc Func WriteToLog($mes)     $file=FileOpen($logfile,1)     FileWrite($logfile,@MDAY&@MON&@YEAR&"-"&@HOUR&@MIN&@SEC&": "&$mes&@CRLF)     FileClose($logfile) Endfunc

...i could use some guide on optimizing this baby and also maybe some of you API gods might help on how i could send the BROADCAST_QUERY_DENY message instead of using SetThreadExecutionState if it's feasible? Also if someone can show me how i can react on System booting and shutting down events ?

Thanks in advance...
g_

Edited by g_BonE, 21 November 2008 - 02:23 PM.

...Fenster fährt mich Nüsse...







#2 g_BonE

g_BonE

    Seeker

  • Active Members
  • 18 posts

Posted 22 November 2008 - 09:34 AM

what... nobody ? and i thought its such a nice example of what can be done with autoit :mellow:

...i could use some guide on optimizing this baby and also maybe some of you API gods might help on how i could send the BROADCAST_QUERY_DENY message instead of using SetThreadExecutionState if it's feasible? Also if someone can show me how i can react on System booting and shutting down events ?

Thanks in advance...
g_


...Fenster fährt mich Nüsse...

#3 g_BonE

g_BonE

    Seeker

  • Active Members
  • 18 posts

Posted 22 November 2008 - 02:34 PM

To get the info that system is rebooting i now use a OnAutoItExit() function and look at the @ExitMethod to be equal to 4.

Func OnAutoItExit()     Switch @EXITMETHOD         Case 1             WriteToLog($g_szVersion&" exited by Close...")         Case 2             WriteToLog($g_szVersion&" exited by SysTray...")         Case 3             WriteToLog($g_szVersion&" exited by LogOff...")         Case 4             WriteToLog($g_szVersion&" exited by Reboot...")             WriteToLog("Rebooting system...")     EndSwitch EndFunc

...Fenster fährt mich Nüsse...

#4 g_BonE

g_BonE

    Seeker

  • Active Members
  • 18 posts

Posted 09 December 2008 - 11:27 AM

Just to bump this one up once more... i'm still looking for a way to send the BROADCAST_QUERY_DENY message via API. Anyone ?!?
...Fenster fährt mich Nüsse...

#5 rasim

rasim

    Gray Scripter

  • Active Members
  • PipPipPipPipPipPip
  • 1,708 posts

Posted 09 December 2008 - 12:25 PM

g_BonE
How to prevent from the suspend message?
Just return the $BROADCAST_QUERY_DENY value.
AutoIt         
Global Const $WM_POWERBROADCAST    = 0x0218 Global Const $PBT_APMQUERYSUSPEND  = 0x0000 Global Const $BROADCAST_QUERY_DENY = 0x424D5144 HotKeySet("{Pause}", "_Exit") $hGUI = GUICreate("Test GUI") $TestButton = GUICtrlCreateButton("Test", 60, 37, 75, 23) GUIRegisterMsg($WM_POWERBROADCAST, "WM_POWERBROADCAST") While 1     Sleep(100) WEnd Func WM_POWERBROADCAST($hWnd, $Msg, $wParam, $lParam)     Switch $wParam         Case $PBT_APMQUERYSUSPEND             Return $BROADCAST_QUERY_DENY     EndSwitch EndFunc Func _Exit()     Exit EndFunc

:)
OS: Windows XP SP3, AutoIt version: 3.3.0.0Posted Image My Projects: Free_Resources | Splitter | wgetGUI | UnRARIt | USBMon | CDROM-Control | Volume Serial Changer | WinTrayPosted Image My UDFs: _ScreenSetting | ListView_Progress | ContextHelp | ToolTip_UDF | UnRAR | Zip32 | BassMod | ShellTreeView | GuiHotKey | 7ZipPosted Image My Examples: TrayIcon_Click | SystemTray_Refresh | _ListView_Sort | CPUmonLike above scripts? Please rate the topic Posted Image

#6 ScriptingIT

ScriptingIT

    Seeker

  • New Members
  • 3 posts

Posted 14 December 2008 - 10:19 AM

The first bit of code is really useful thanks. Follow ups are good also. I'm interested in making an energy usage measuring tool. Happy to post anything useful anyone already working on such a project?

#7 g_BonE

g_BonE

    Seeker

  • Active Members
  • 18 posts

Posted 16 December 2008 - 02:58 PM

thanks rasim, that was exactly what i'd been looking for. where did you get the hex number for $BROADCAST_QUERY_DENY = 0x424D5144 from ?
...Fenster fährt mich Nüsse...

#8 passkalilo

passkalilo

    Seeker

  • Active Members
  • 24 posts

Posted 29 May 2009 - 01:30 PM

Very Thanks ,this is what i needed

#9 Raik

Raik

    Adventurer

  • Active Members
  • PipPip
  • 124 posts

Posted 02 December 2010 - 06:52 PM

@rasim
does this work too for pushing the powerbutton, or only for system-powersaving-events?
if someone pushes the powerbutton 4 seconds, its not preventable, right? (because a bios-routine is called)

AutoIt-Syntaxsheme for Proton & Phase5 * Firefox Addons by me (resizable Textarea 0.1d) (docked JS-Console 0.1.1)


#10 AdmiralClaws

AdmiralClaws

    Where is the fish?

  • Active Members
  • PipPipPipPipPipPip
  • 4,651 posts

Posted 02 December 2010 - 07:38 PM

@rasim
does this work too for pushing the powerbutton, or only for system-powersaving-events?
if someone pushes the powerbutton 4 seconds, its not preventable, right? (because a bios-routine is called)

Are you trying to be funny or something? Someone who was last seen 2 years ago is not going to answer.

#11 Raik

Raik

    Adventurer

  • Active Members
  • PipPip
  • 124 posts

Posted 02 December 2010 - 11:19 PM

Are you trying to be funny or something? Someone who was last seen 2 years ago is not going to answer.

oh :-O
maybe, someone other knows the answer? possibly you?

AutoIt-Syntaxsheme for Proton & Phase5 * Firefox Addons by me (resizable Textarea 0.1d) (docked JS-Console 0.1.1)


#12 AdmiralClaws

AdmiralClaws

    Where is the fish?

  • Active Members
  • PipPipPipPipPipPip
  • 4,651 posts

Posted 03 December 2010 - 08:57 AM

I agree that the 4 second thing is probably done by the motherboard somehow.

Don't know about a shirt press, why don't you try?

PS See when someone was last on line on the left side of their profile (click their name)

#13 Raik

Raik

    Adventurer

  • Active Members
  • PipPip
  • 124 posts

Posted 09 December 2010 - 10:16 PM

short press on powerbutton will trigger shutdown (depends on the settings). this i may supress.
only the long press i probatly can't.

AutoIt-Syntaxsheme for Proton & Phase5 * Firefox Addons by me (resizable Textarea 0.1d) (docked JS-Console 0.1.1)





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users