Jump to content
Sign in to follow this  

Check if workstation is locked (cross platform)

Recommended Posts



I was looking for a way to detect if the workstation is locked on all Windows platforms and could not find a working solution in this thread. The original thread seems to have diverted from its original topic, so I thought a new topic would be more appropriate.

So after reading this and this and this and this and this and this, I wrote the following which seems to work, and I'd like to hear your opinion (perhaps it can be improved).

Btw, IsWorkstationLockedModern() seems more resource consuming than IsWorkstationLockedLegacy() but if, for some reason, it is preferred over the latter, it may still be used on Windows 7 and Windows Server 2008 R2, by excluding them from IsLegacyOS(), and checking for SessionFlags==1 instead of 0 (due to a Windows bug; this is explained in the last "this" link).

Func IsWorkstationLocked()
    If (IsLegacyOS()) Then
        Return IsWorkstationLockedLegacy()
        Return IsWorkstationLockedModern()

Func IsLegacyOS()
    Return ((@OSVersion == "WIN_2008R2") OR (@OSVersion == "WIN_2008") OR (@OSVersion == "WIN_7") OR (@OSVersion == "WIN_VISTA") OR (@OSVersion == "WIN_2003") OR (@OSVersion == "WIN_XP") OR (@OSVersion == "WIN_XPe") OR (@OSVersion == "WIN_2000"))

Func IsWorkstationLockedLegacy()
    Local Const $DESKTOP_SWITCHDESKTOP = 0x100
    Local $hUser32dll = DllOpen("user32.dll")
    Local $hDesktop = DllCall($hUser32dll, "int", "OpenDesktop", "str", "Default", "int", 0, "int", 0, "int", $DESKTOP_SWITCHDESKTOP)
    If ((@error) OR ($hDesktop[0] == 0)) Then
        Return SetError(1, 0, False)
    Local $result = DllCall($hUser32dll, "int", "SwitchDesktop", "int", $hDesktop[0])
    Local $isLocked = ($result[0] == 0)
    DllCall($hUser32dll, "int", "CloseDesktop", "int", $hDesktop[0])    
    Return $isLocked

Func IsWorkstationLockedModern()
    Local Const $WTS_CURRENT_SESSION = -1
    Local Const $WTS_SESSION_INFO_EX = 25
    Local $hWtsapi32dll = DllOpen("Wtsapi32.dll")
    Local $result = DllCall($hWtsapi32dll, "int", "WTSQuerySessionInformation", "int", $WTS_CURRENT_SERVER_HANDLE, "int", $WTS_CURRENT_SESSION, "int", $WTS_SESSION_INFO_EX, "ptr*", 0, "dword*", 0)
    If ((@error) OR ($result[0] == 0)) Then
        Return SetError(1, 0, False)
    Local $buffer_ptr = $result[4]
    Local $buffer_size = $result[5]
    Local $buffer = DllStructCreate("uint64 SessionId;uint64 SessionState;int SessionFlags;byte[" & $buffer_size - 20 & "]", $buffer_ptr)
    Local $isLocked = (DllStructGetData($buffer, "SessionFlags") == 0)
    $buffer = 0 
    DllCall($hWtsapi32dll, "int", "WTSFreeMemory", "ptr", $buffer_ptr)
    Return $isLocked


  • Like 1
  • Thanks 1

Share this post

Link to post
Share on other sites

cool. maybe this should be in the Example Scripts section. Thank you for sharing that. it seems to work ok as you stated.

Edited by Earthshine

My resources are limited. You must ask the right questions


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