Jump to content

Detect Locking and Unlocking of a PC


WiSp
 Share

Recommended Posts

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

- 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]

Link to comment
Share on other sites

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]

Link to comment
Share on other sites

  • 2 months later...

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!

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...