Jump to content
Sign in to follow this  
WiSp

Detect Locking and Unlocking of a PC

Recommended Posts

WiSp

I trying to find an example of a script that detects when a PC is unlocked and then maximize itself. Is there a way for autoit to detect a locked and unlocked PC and launch events based on this?

Share this post


Link to post
Share on other sites
Monamo

I trying to find an example of a script that detects when a PC is unlocked and then maximize itself. Is there a way for autoit to detect a locked and unlocked PC and launch events based on this?

Here's the function that I use (typically in a While... Wend loop, which should suit your requested usage well):

Global Const $DESKTOP_SWITCHDESKTOP = 0x100

Func _CheckLocked()
    $hLockedDLL = DllOpen("user32.dll")
    $hDesktop = DllCall($hLockedDLL, "int", "OpenDesktop", "str", "Default", "int", 0, "int", 0, "int", $DESKTOP_SWITCHDESKTOP)
    $ret = DllCall($hLockedDLL, "int", "SwitchDesktop", "int", $hDesktop[0])
    DllCall($hLockedDLL, "int", "CloseDesktop", "int", $hDesktop[0])

    If $ret[0] = 0 Then
        $iLocked = 1
    ElseIf $ret[0] = 1 Then
        $iLocked = 0
    EndIf
    
    DllClose($hLockedDLL)
    
    If $iLocked Then
        Return 1
    Else
        Return 0
    EndIf
EndFunc

Edit: Added the Global Constant needed by the function

Edited by Monamo
  • Like 1

- MoChr(77)& Chr(97)& Chr(100)& Chr(101)& Chr(32)& Chr(121)& Chr(97)& Chr(32)& Chr(108)& Chr(111)& Chr(111)& Chr(107)-------I've told you 100,000 times not to exaggerate!-------Don't make me hit you with my cigarette hand...-------My scripts:Random Episode Selector, Keyboard MouseMover, CopyPath v2.1, SmartRename for XP,Window Tracer[sup]New![/sup]

Share this post


Link to post
Share on other sites
WiSp

Could I place the function call in the GUI's while loop or is that inadvisable?

Share this post


Link to post
Share on other sites
Monamo

Could I place the function call in the GUI's while loop or is that inadvisable?

Should be fine (just remember that the Constant declaration should be before the loop, not in it). I typically use it when I require interaction from a user for a specific piece of code before continuing, as there's no point in displaying the message while the system is locked.

- MoChr(77)& Chr(97)& Chr(100)& Chr(101)& Chr(32)& Chr(121)& Chr(97)& Chr(32)& Chr(108)& Chr(111)& Chr(111)& Chr(107)-------I've told you 100,000 times not to exaggerate!-------Don't make me hit you with my cigarette hand...-------My scripts:Random Episode Selector, Keyboard MouseMover, CopyPath v2.1, SmartRename for XP,Window Tracer[sup]New![/sup]

Share this post


Link to post
Share on other sites
WiSp

Thank you, I'll give it a shot.

Share this post


Link to post
Share on other sites
YoniB

Here's the function that I use (typically in a While... Wend loop, which should suit your requested usage well):

Global Const $DESKTOP_SWITCHDESKTOP = 0x100

Func _CheckLocked()
    $hLockedDLL = DllOpen("user32.dll")
    $hDesktop = DllCall($hLockedDLL, "int", "OpenDesktop", "str", "Default", "int", 0, "int", 0, "int", $DESKTOP_SWITCHDESKTOP)
    $ret = DllCall($hLockedDLL, "int", "SwitchDesktop", "int", $hDesktop[0])
    DllCall($hLockedDLL, "int", "CloseDesktop", "int", $hDesktop[0])

    If $ret[0] = 0 Then
        $iLocked = 1
    ElseIf $ret[0] = 1 Then
        $iLocked = 0
    EndIf
    
    DllClose($hLockedDLL)
    
    If $iLocked Then
        Return 1
    Else
        Return 0
    EndIf
EndFunc

Edit: Added the Global Constant needed by the function

Hi Monamo!

I tried your code and it's working great for local unlocked desktop.

But when someone connect remotely (by Remote Desktop), the status is 'Unlocked' even if he closed the session (without manually unlocking). This status supposed to be 'Locked' (no one connected!).

Do you know why it behaving like that?

Do you have any solution idea?

Thanks a lot!

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  

×