Jump to content

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

Wrapper to catch Power Management events (like standby)


  • Please log in to reply
13 replies to this topic
TheGood
  • Members
  • 589 posts
  • Last active: Mar 22 2014 03:22 PM
  • Joined: 30 Jul 2007
I made a nice little wrapper in order to catch Power Management Events. A few examples of events:
[*:26i6mfh9] Detect when the computer is trying to suspend.
[*:26i6mfh9] Detect when the computer tried to suspend, but failed.
[*:26i6mfh9] Detect when the computer resumes from a suspended state.
[*:26i6mfh9] Detect when the battery is running low
[*:26i6mfh9] Detect when the computer changes power source (eg. from battery to A/C)
/* TheGood
Wrapper to catch Power Management events

;http://msdn.microsoft.com/en-us/library/aa373162(VS.85).aspx
;SUPPORTED ONLY BY WINDOWS 2000 AND UP
WM_POWERBROADCAST           := 536          ;Notifies applications that a power-management event has occurred.
PBT_APMQUERYSUSPEND         := 0            ;Requests permission to suspend the computer. An application
                                            ;that grants permission should carry out preparations for the
                                            ;suspension before returning.
BROADCAST_QUERY_DENY        := 1112363332   ;Return this value to deny the request.
PBT_APMQUERYSUSPENDFAILED   := 2            ;Notifies applications that permission to suspend the computer
                                            ;was denied. This event is broadcast if any application or
                                            ;driver returned BROADCAST_QUERY_DENY to a previous
                                            ;PBT_APMQUERYSUSPEND event.
PBT_APMSUSPEND              := 4            ;Notifies applications that the computer is about to enter a
                                            ;suspended state. This event is typically broadcast when all
                                            ;applications and installable drivers have returned TRUE to a
                                            ;previous PBT_APMQUERYSUSPEND event.
PBT_APMRESUMECRITICAL       := 6            ;Notifies applications that the system has resumed operation.
                                            ;This event can indicate that some or all applications did not
                                            ;receive a PBT_APMSUSPEND event. For example, this event can be
                                            ;broadcast after a critical suspension caused by a failing
                                            ;battery.
PBT_APMRESUMESUSPEND        := 7            ;Notifies applications that the system has resumed operation
                                            ;after being suspended.
PBT_APMBATTERYLOW           := 9            ;Notifies applications that the battery power is low.
PBT_APMPOWERSTATUSCHANGE    := 10           ;Notifies applications of a change in the power status of the
                                            ;computer, such as a switch from battery power to A/C. The
                                            ;system also broadcasts this event when remaining battery power
                                            ;slips below the threshold specified by the user or if the
                                            ;battery power changes by a specified percentage.
PBT_APMOEMEVENT             := 11           ;Notifies applications that the APM BIOS has signaled an APM
                                            ;OEM event.
PBT_APMRESUMEAUTOMATIC      := 18           ;Notifies applications that the computer has woken up
                                            ;automatically to handle an event. An application will not
                                            ;generally respond unless it is handling the event, because
                                            ;the user is not present.
*/

OnMessage(536, "OnPBMsg")     ;WM_POWERBROADCAST
Return

OnPBMsg(wParam, lParam, msg, hwnd) {
	If (wParam = 0) {	;PBT_APMQUERYSUSPEND
		If (lParam & 1)	;Check action flag
			MsgBox The computer is trying to suspend, and user interaction is permitted.
		Else MsgBox The computer is trying to suspend, and no user interaction is allowed.
		;Return TRUE to grant the request, or BROADCAST_QUERY_DENY to deny it.
	} Else If (wParam = 2)	;PBT_APMQUERYSUSPENDFAILED
		MsgBox The computer tried to suspend, but failed.
	Else If (wParam = 4)	;PBT_APMSUSPEND
		MsgBox The computer is about to enter a suspended state.
	Else If (wParam = 6)	;PBT_APMRESUMECRITICAL
		MsgBox The computer is now resuming from a suspended state, which may have taken place unexpectedly.
	Else If (wParam = 7)	;PBT_APMRESUMESUSPEND
		MsgBox The computer is now resuming from a suspended state.
	Else If (wParam = 9)	;PBT_APMBATTERYLOW
		MsgBox The computer battery is running low.
	Else If (wParam = 10)	;PBT_APMPOWERSTATUSCHANGE
		MsgBox The computer power status has changed.
	Else If (wParam = 11)	;PBT_APMOEMEVENT
		MsgBox The APM BIOS has signaled an APM OEM event with event code %lParam%
	Else If (wParam = 18)	;PBT_APMRESUMEAUTOMATIC
		MsgBox The computer is now automatically resuming from a suspended state.
	
	;Must return True after message is processed
	Return True
}
Edit: Replaced the constants with their actual values.

Frankie
  • Members
  • 2930 posts
  • Last active: Feb 05 2015 02:49 PM
  • Joined: 02 Nov 2008
Nice. Havn't tried it out yet but Im planing on making a GDI+ low battery warning with this.
aboutscriptappsscripts
Request Video Tutorials Here or View Current Tutorials on YouTube
Any code ⇈ above ⇈ requires AutoHotkey_L to run

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005
Nice.. Couple of suggestions.
1) It is better not to use MsgBox within Message Monitor Functions. I would suggest TrayTip.
2) The functions has to return a True as suggested by MSDN Doc.
kWo4Lk1.png

Zer07even
  • Members
  • 19 posts
  • Last active: Dec 01 2009 08:32 AM
  • Joined: 01 Aug 2008
Maybe just me but absolutely nothing happens on vista. msgbox never shows
Posted Image

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005
@Zer07even

Replace
OnMessage(WM_POWERBROADCAST, "OnPBMsg")
with
OnMessage( (WM_POWERBROADCAST:=[color=red]0x218[/color]), "OnPBMsg")

kWo4Lk1.png

Zer07even
  • Members
  • 19 posts
  • Last active: Dec 01 2009 08:32 AM
  • Joined: 01 Aug 2008
Working beautifully! I tried to write something like this a while back but i could never get it working, now I know why.

Thnx
Posted Image

TheGood
  • Members
  • 589 posts
  • Last active: Mar 22 2014 03:22 PM
  • Joined: 30 Jul 2007

Nice.. Couple of suggestions.
1) It is better not to use MsgBox within Message Monitor Functions. I would suggest TrayTip.

True. I was just using MsgBox to show people where their code would go. It's not meant to be implemented that way. Maybe I should just put a comment there instead?

2) The functions has to return a True as suggested by MSDN Doc.

As far as I can tell, only PBT_APMQUERYSUSPEND requires a return value. The others simply ignore it (MSDN says "No return value.").

TheGood
  • Members
  • 589 posts
  • Last active: Mar 22 2014 03:22 PM
  • Joined: 30 Jul 2007

@Zer07even

Replace

OnMessage(WM_POWERBROADCAST, "OnPBMsg")
with
OnMessage( (WM_POWERBROADCAST:=[color=red]0x218[/color]), "OnPBMsg")


THanks! I forgot to change the constant for its actual value for that one :)
Updated post!

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005

As far as I can tell, only PBT_APMQUERYSUSPEND requires a return value. The others simply ignore it (MSDN says "No return value.").


WM_POWERBROADCAST Message

Return Value
An application should return TRUE if it processes this message.

Windows Server 2003, Windows XP, and Windows 2000: An application can return BROADCAST_QUERY_DENY to deny a PBT_APMQUERYSUSPEND or PBT_APMQUERYSUSPENDFAILED request.



TheGood
  • Members
  • 589 posts
  • Last active: Mar 22 2014 03:22 PM
  • Joined: 30 Jul 2007
OK, I updated the post. Thanks! :D

Beery
  • Members
  • 6 posts
  • Last active: Mar 19 2011 07:00 PM
  • Joined: 04 Mar 2006
Very useful!

A question. I'm getting multiple triggers when plugging my laptop of of AC power - is this expected? I was expecting only one event.

Thanks,
Beery

TheGood
  • Members
  • 589 posts
  • Last active: Mar 22 2014 03:22 PM
  • Joined: 30 Jul 2007
Not sure. I don't have a laptop to test it.
Is it the same event multiple times or different events each time?

theInFan
  • Members
  • 5 posts
  • Last active: Dec 19 2010 05:34 PM
  • Joined: 17 Oct 2009
Its the same event.
I don't know about Beery, but when I plug into AC the power status event triggers twice.
But only one trigger when I unplug and go to battery power.

It might be because there are two states when plugging into AC power. According to Windows 7, those two states are:
"plugged in, not charging"
"plugged in, charging"
Each state would trigger the power status event, creating this "double trigger" situation.

Beery
  • Members
  • 6 posts
  • Last active: Mar 19 2011 07:00 PM
  • Joined: 04 Mar 2006
Hi,

I've switched to a Windows 7 machine and (at least) the standby and resume do not trigger (PBT_APMSUSPEND and PBT_APMRESUMESUSPEND).

btw, PBT_APMPOWERSTATUSCHANGE seems to work.

Any idea?

Thanks,
Beery