Sign in to follow this  
Followers 0
PyR0

lid+program+sleep

9 posts in this topic

hello im somewhat of a newbie here but i was looking for a way to run a script to monitor the lid state of my laptop and the also check if a particular program is running. and respond to a given state with sleep mode. so basically like this"

close lid + the program is not running = put to sleep

close lid + the program is running = do nothing (no sleep)

i was looking as this example piece i found on the forum to see if it could be tweaked to change "LockWorkStation" to the sleep command (i dont know what it is) and then incorporate the check for the running program.

Global Const $WM_POWERBROADCAST = 0x0218
; GUID_LIDSWITCH_STATE_CHANGE GUID
; 0xBA3E0F4D, 0xB817, 0x4094, 0xA2, 0xD1, 0xD5, 0x63, 0x79, 0xE6, 0xA0, 0xF3
Global Const $GUID_LIDSWITCH_STATE_CHANGE = DllStructCreate("byte[16]")
DllStructSetData($GUID_LIDSWITCH_STATE_CHANGE, 1, Binary("0x4D0F3EBA17B89440A2D1D56379E6A0F3"))

; create gui and register notification
Global $GUI = GUICreate("")
GUIRegisterMsg($WM_POWERBROADCAST, "__WM_POWER")
Global $hPower = DllCall("user32.dll", "handle", "RegisterPowerSettingNotification", "handle", $GUI, "ptr", DllStructGetPtr($GUID_LIDSWITCH_STATE_CHANGE), "dword", 0)
GUISetState()

Do
Until GUIGetMsg() = -3

; unregister notification
DllCall("user32.dll", "bool", "UnregisterPowerSettingNotification", "handle", $hPower[0])

Func __WM_POWER($hwnd, $msg, $wparam, $lparam)
    #forceref $hwnd
    Local Const $PBT_POWERSETTINGCHANGE = 0x8013
    ; Data member is variable depending on notification
    ; the size of the Data member is returned in DataLength
    Local Const $POWERBROADCAST_SETTING = "byte[16];dword DataLength;byte Data"

    Switch $msg
        Case $WM_POWERBROADCAST
            Switch $wparam
                Case $PBT_POWERSETTINGCHANGE
                    Local $PBT_PSC = DllStructCreate($POWERBROADCAST_SETTING, $lparam)
                    ; GUID of registered notification is the first member of the structure
                    Switch DllStructGetData($PBT_PSC, 1)
                        Case DllStructGetData($GUID_LIDSWITCH_STATE_CHANGE, 1)
                            ; for this notification, Data is an int representing the lid state
                            Local $data = DllStructCreate("int", DllStructGetPtr($PBT_PSC, "Data"))
                            _LidStateChange(DllStructGetData($data, 1))
                            ;
                            Return 1
                    EndSwitch
            EndSwitch
    EndSwitch

    Return "GUI_RUNDEFMSG"
EndFunc

Func _LidStateChange($iState)
    Static $iPrevious = -1
    If $iPrevious = -1 Then
        ; first fire, current state
        $iPrevious = $iState
        Return
    ElseIf $iPrevious = $iState Then
        Return
    Else
        $iPrevious = $iState
    EndIf
    ; lid state:
    ; 0 -> closed
    ; 1 -> open
    Switch $iState
        Case 0
            ConsoleWrite("lid closed" & @CRLF)
            DllCall("user32.dll", "bool", "LockWorkStation")
        Case 1
            ConsoleWrite("lid opened" & @CRLF)
    EndSwitch
EndFunc

thanks for any help :)

Share this post


Link to post
Share on other sites



after looking at those functions i change the part where it says:

DllCall("user32.dll", "bool", "LockWorkStation")

so it now says

If NOT ProcessExists("uTorrent.exe") Then
            Shutdown(32) ;standby
    Endif

    If ProcessExists("uTorrent.exe") Then
            DllCall("user32.dll", "bool", "LockWorkStation")
    Endif

and that seems to be doing the trick. so thanks :)

here is the entire code if anyone wants it. it will sleep if you close the lid and utorrent is not running and it will lock the computer if you close the lid and utorrent is running

Global Const $WM_POWERBROADCAST = 0x0218
; GUID_LIDSWITCH_STATE_CHANGE GUID
; 0xBA3E0F4D, 0xB817, 0x4094, 0xA2, 0xD1, 0xD5, 0x63, 0x79, 0xE6, 0xA0, 0xF3
Global Const $GUID_LIDSWITCH_STATE_CHANGE = DllStructCreate("byte[16]")
DllStructSetData($GUID_LIDSWITCH_STATE_CHANGE, 1, Binary("0x4D0F3EBA17B89440A2D1D56379E6A0F3"))

; create gui and register notification
Global $GUI = GUICreate("")
GUIRegisterMsg($WM_POWERBROADCAST, "__WM_POWER")
Global $hPower = DllCall("user32.dll", "handle", "RegisterPowerSettingNotification", "handle", $GUI, "ptr", DllStructGetPtr($GUID_LIDSWITCH_STATE_CHANGE), "dword", 0)
GUISetState()

Do
Until GUIGetMsg() = -3

; unregister notification
DllCall("user32.dll", "bool", "UnregisterPowerSettingNotification", "handle", $hPower[0])

Func __WM_POWER($hwnd, $msg, $wparam, $lparam)
    #forceref $hwnd
    Local Const $PBT_POWERSETTINGCHANGE = 0x8013
    ; Data member is variable depending on notification
    ; the size of the Data member is returned in DataLength
    Local Const $POWERBROADCAST_SETTING = "byte[16];dword DataLength;byte Data"

    Switch $msg
        Case $WM_POWERBROADCAST
            Switch $wparam
                Case $PBT_POWERSETTINGCHANGE
                    Local $PBT_PSC = DllStructCreate($POWERBROADCAST_SETTING, $lparam)
                    ; GUID of registered notification is the first member of the structure
                    Switch DllStructGetData($PBT_PSC, 1)
                        Case DllStructGetData($GUID_LIDSWITCH_STATE_CHANGE, 1)
                            ; for this notification, Data is an int representing the lid state
                            Local $data = DllStructCreate("int", DllStructGetPtr($PBT_PSC, "Data"))
                            _LidStateChange(DllStructGetData($data, 1))
                            ;
                            Return 1
                    EndSwitch
            EndSwitch
    EndSwitch

    Return "GUI_RUNDEFMSG"
EndFunc

Func _LidStateChange($iState)
    Static $iPrevious = -1
    If $iPrevious = -1 Then
        ; first fire, current state
        $iPrevious = $iState
        Return
    ElseIf $iPrevious = $iState Then
        Return
    Else
        $iPrevious = $iState
    EndIf
    ; lid state:
    ; 0 -> closed
    ; 1 -> open
    Switch $iState
        Case 0
            ConsoleWrite("lid closed" & @CRLF)
            If NOT ProcessExists("uTorrent.exe") Then
            Shutdown(32) ;standby
            Endif
            If ProcessExists("uTorrent.exe") Then
            DllCall("user32.dll", "bool", "LockWorkStation")
            Endif
        Case 1
            ConsoleWrite("lid opened" & @CRLF)
    EndSwitch
EndFunc

now i just wish there was a way to not have that window that has to be open.

Share this post


Link to post
Share on other sites

Does the window really has to be visible? Did you try having it hidden? (comment/delete the GUISetState())

AH ha, so i just needed to put "GUISetState(@SW_HIDE)"

and i found i can even add "Opt("TrayIconHide", 1)"

and it is now completely invisible :) ill set it to start with windows and it will be perfect.

thanks a lot AdmiralManHairAlkex ;)

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Well that wasn't really what I meant (windows are hidden by default, so you could just skip GUISetState()), but I guess the result is the same.

I'm happy I could be of some service :)

Edited by AdmiralManHairAlkex

Share this post


Link to post
Share on other sites

ah i see, ill just take it out then :) thanks again

Share this post


Link to post
Share on other sites

I probably should be glad I got what I wanted working and quit while I'm ahead but...

if I could get some help one more time. I was wondering if there is a way to set this up to where if when I close the lid and in the case where it does put the laptop to sleep if there is a way to monitor it and if its been sleeping for 30 minutes put it in hibernate.

I'm not sure if this can be done though. since I would think the sleep mode would stop the script. is that correct?

i was thinking sleep(1800000) ;30min

Shutdown(64) ;hibernate

but that wouldnt work if the script was stopped plus id have to add a way to stop the timer if the lid opened.

any ideas? i wonder if i could use windows task scheduler to use sleep as a trigger..

what are your thoughts?

Share this post


Link to post
Share on other sites

is there a way to set this up so that after the computer is in sleep for 30 minutes it puts the computer in hibernate?

Share this post


Link to post
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
Sign in to follow this  
Followers 0