AutoHotkey Community

It is currently May 27th, 2012, 12:05 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: January 17th, 2011, 6:24 pm 
Offline

Joined: December 8th, 2009, 1:31 am
Posts: 5
I'm trying to detect when my laptop's lid is closed so I can disable a certain device to save power. There's an old inconclusive topic on this, but only one of the posts (that linked one) bothered to use the RegisterPowerSettingNotification function to capture a specific power event, and its example doesn't work!

RegisterPowerSettingNotification takes an hWnd that will receive the specific power event when fired; the GUID of that event; and a flag. I want to detect GUID_LIDCLOSE_ACTION, but nothing happens. Any guesses on what's wrong?

Code:
Gui, Add, Text, , lid-detector
Gui, Show, , lid-detector
lidDetectorHwnd := WinExist("lid-detector")
Gui, Hide

DllCall("RegisterPowerSettingNotification", "UInt", lidDetectorHwnd, "str", "5ca83367-6e45-459f-a27b-476b1d01c936", "UInt", 0)
MsgBox %Errorlevel%

OnMessage(0x218, "func_WM_POWERBROADCAST")

func_WM_POWERBROADCAST(wParam, lParam)
{
If (wParam = 0)
  Msgbox I closed my lid!
Return
}

Thanks :)


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 18th, 2011, 2:46 am 
Offline

Joined: April 8th, 2009, 7:49 pm
Posts: 6074
Location: San Diego, California
Maybe this will help, doesn't need a gui
I started with msgbox & removing power, but didn't want to hit OK on msgbox
So I changed to using tooltip
But I wasn't sure if the program would survive a sleep or hibernate, so I changed to FileAppend.

I haven't yet tried a forced shutdown with the power button. :wink:

Code:
fred0=0   ; this is how many events have occurred


OnMessage(0x218, "func_WM_POWERBROADCAST")

tooltip, lid detector reloaded`r`n Press F1 to look at events

FormatTime, TimeString

fileappend,  program reloaded  A_TickCount %A_TickCount% TimeString %TimeString%`r`n, power_log.txt


return

func_WM_POWERBROADCAST(wParam, lParam)
{
  global

  fred0++
  fred%fred0%:=wParam   ; store the current value in an array
  george%fred0%:=lParam ; store the current value in an array

  fileappend,  wParam %wParam% lParam %lParam%  A_TickCount %A_TickCount% TimeString %TimeString%`r`n, power_log.txt

  tooltip,  hi!!!! %wParam% %lParam% %fred0%`r`n Press F1 to look at events; show how many events hae occurred

;
;If (wParam = 0)
;  Msgbox I closed my lid!
;Return
}


esc::exitapp

f1::listvars

f12::reload


Quote:
program reloaded A_TickCount 84949611 TimeString 5:30 PM Monday, January 17, 2011
program reloaded A_TickCount 84956600 TimeString 5:30 PM Monday, January 17, 2011
program reloaded A_TickCount 84957099 TimeString 5:30 PM Monday, January 17, 2011
wParam 10 lParam 0 A_TickCount 84972309 TimeString 5:30 PM Monday, January 17, 2011 ; unplug
wParam 10 lParam 0 A_TickCount 84973728 TimeString 5:30 PM Monday, January 17, 2011 ; insert power plug
wParam 4 lParam 0 A_TickCount 85014336 TimeString 5:30 PM Monday, January 17, 2011 ; lid close
wParam 18 lParam 0 A_TickCount 85053476 TimeString 5:30 PM Monday, January 17, 2011 ; lid open
wParam 4 lParam 0 A_TickCount 85110214 TimeString 5:30 PM Monday, January 17, 2011 : i dunno what this was
wParam 18 lParam 0 A_TickCount 85256480 TimeString 5:30 PM Monday, January 17, 2011 : i dunno what this was


Report this post
Top
 Profile  
Reply with quote  
PostPosted: January 18th, 2011, 12:09 pm 
Offline

Joined: February 7th, 2009, 5:37 am
Posts: 43
You have wrong parameter.
DigiMarco wrote:
Code:
DllCall("RegisterPowerSettingNotification", "UInt", lidDetectorHwnd, "str", "5ca83367-6e45-459f-a27b-476b1d01c936", "UInt", 0)

RegisterPowerSettingNotification Function
Code:
HPOWERNOTIFY WINAPI RegisterPowerSettingNotification(
  __in  HANDLE hRecipient,
  __in  LPCGUID PowerSettingGuid,
  __in  DWORD Flags
);

An LPCGUID is not a string. This is a pointer to a GUID structure.
Code:
typedef struct _GUID {
  DWORD Data1;
  WORD  Data2;
  WORD  Data3;
  BYTE  Data4[8];
} GUID;

Data1 Specifies the first 8 hexadecimal digits of the GUID.
Data2 Specifies the first group of 4 hexadecimal digits.
Data3 Specifies the second group of 4 hexadecimal digits.
Data4 Array of 8 bytes. The first 2 bytes contain the third group of 4 hexadecimal digits. The remaining 6 bytes contain the final 12 hexadecimal digits.

You can call CLSIDFromString Function (COM)
Code:
OnExit, ExitSub

Gui, +LastFound
hRecipient := WinExist()

String := "{5ca83367-6e45-459f-a27b-476b1d01c936}" ; LIDACTION
VarSetCapacity(PowerSettingGuid, 16, 0)
DllCall("ole32\CLSIDFromString", "UInt", &String, "UInt", &PowerSettingGuid)
hPowerNotify := DllCall("RegisterPowerSettingNotification", "UInt", hRecipient, "UInt", &PowerSettingGuid, "UInt", 0)

OnMessage(0x0218, "WM_POWERBROADCAST")

Return


ExitSub:
DllCall("UnregisterPowerSettingNotification", "UInt", hPowerNotify) ; Unregisters the power setting notification.
ExitApp

WM_POWERBROADCAST(wParam, lParam)
{
   ; Do what you want
   Return, true ; An application should return TRUE if it processes this message.
}
or create structure step-by-step
Code:
OnExit, ExitSub

Gui, +LastFound
hRecipient := WinExist()

String := "5ca83367-6e45-459f-a27b-476b1d01c936" ; LIDACTION
VarSetCapacity(PowerSettingGuid, 16, 0)
NumPut("0x" . SubStr(String, 1,  8), PowerSettingGuid, 0,  "UInt")   ; DWORD Data1
NumPut("0x" . SubStr(String, 10, 4), PowerSettingGuid, 4,  "UShort") ; WORD  Data2
NumPut("0x" . SubStr(String, 15, 4), PowerSettingGuid, 6,  "UShort") ; WORD  Data3
NumPut("0x" . SubStr(String, 20, 2), PowerSettingGuid, 8,  "UChar")  ; BYTE  Data4[1]
NumPut("0x" . SubStr(String, 22, 2), PowerSettingGuid, 9,  "UChar")  ; BYTE  Data4[2]
NumPut("0x" . SubStr(String, 25, 2), PowerSettingGuid, 10, "UChar")  ; BYTE  Data4[3]
NumPut("0x" . SubStr(String, 27, 2), PowerSettingGuid, 11, "UChar")  ; BYTE  Data4[4]
NumPut("0x" . SubStr(String, 29, 2), PowerSettingGuid, 12, "UChar")  ; BYTE  Data4[5]
NumPut("0x" . SubStr(String, 31, 2), PowerSettingGuid, 13, "UChar")  ; BYTE  Data4[6]
NumPut("0x" . SubStr(String, 33, 2), PowerSettingGuid, 14, "UChar")  ; BYTE  Data4[7]
NumPut("0x" . SubStr(String, 35, 2), PowerSettingGuid, 15, "UChar")  ; BYTE  Data4[8]
hPowerNotify := DllCall("RegisterPowerSettingNotification", "UInt", hRecipient, "UInt", &PowerSettingGuid, "UInt", 0)

OnMessage(0x0218, "WM_POWERBROADCAST")

Return


ExitSub:
DllCall("UnregisterPowerSettingNotification", "UInt", hPowerNotify) ; Unregisters the power setting notification.
ExitApp

WM_POWERBROADCAST(wParam, lParam)
{
   ; Do what you want
   Return, true ; An application should return TRUE if it processes this message.
}


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: Bing [Bot], BrandonHotkey, Google Feedfetcher, Klark92, Yahoo [Bot] and 18 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group