Jump to content
Sign in to follow this  
Zinthose

IsWorkStationLocked() & IsScreenSaverRunning()

Recommended Posts

Zinthose

Here are some useful functions I pieced together and thought I'd share. >_<

DllOpen("user32.dll")

While 1
    Sleep(2000)
    ConsoleWrite(" IsWorkStationLocked = " & IsWorkStationLocked() & @CRLF)
    ConsoleWrite("IsScreenSaverRunning = " & IsScreenSaverRunning() & @CRLF)
WEnd

Func IsWorkStationLocked()
    Local $Desktop, $Return
    
    $Return = DllCall("user32.dll", "long", "OpenDesktopA", "str", "Default", "dword", 0, "ubyte", 0, "dword", 0x100)
    $Desktop = $Return[0]
    
    If $Desktop = 0 Then
        Return True
    EndIf
    
    $Return = DllCall("user32.dll", "long", "SwitchDesktop", "dword", $Desktop)
    DllCall("user32.dll", "long", "CloseDesktop", "dword", $Desktop)
    
    If $Return[0] = 0 Then
        Return True
    Else 
        Return False
    EndIf
EndFunc

Func IsScreenSaverRunning()
    Local $Desktop, $Return, $Result
    
    $Result = DllStructCreate("uint")
    $Return = DllCall("user32.dll", "ubyte", "SystemParametersInfoA", "uint", 0x72, "uint", 0, "ptr", DllStructGetPtr($Result), "uint", 0)
    If $Return[0] = 0 Then Return SetError(, 0, False)
    
    Return DllStructGetData($Result, 1) <> 0
EndFunc

--- TTFN

Share this post


Link to post
Share on other sites
wibni

Hi,

would it be possible to unlock a workstation with your script or is this something AutoIt can't do?

Share this post


Link to post
Share on other sites
Yashied
SoulSHepard

@Zinthose,

just what i was thinking about. saw the functions and while case looped a logger arround it for the console as an addendum

; workstation idle logger for windows
; Functions : IsWorkStationLocked() and IsScreenSaverRunning() by Zinthose
; crappy case while loop code by S0ulSHepard
;

#include <Date.au3>

DllOpen("user32.dll")

$actiondate =  _NowCalc()
$timehaspastmin = 0
$timehaspasthour =0
$tmpString1=0
$tmpString2=0
$prediction="was working for"

While 1
    
        $actiontrigger = False
        
    $file = FileOpen("lockstatus.txt", 1)
    If $file = -1 Then
        MsgBox(0, "Error", "Unable to open file.")
        Exit
    EndIf
    
    $tmpString1=IsWorkStationLocked()
    $tmpString2=IsScreenSaverRunning()

    Sleep(10000)

    if $tmpString1 <> IsWorkStationLocked() Then
        $actiontrigger = True
    
    EndIf
    
    if  $tmpString2 <> IsScreenSaverRunning() Then
        $actiontrigger = True
    
    EndIf
    
    if $actiontrigger = True Then
        $timehaspastsec = _Datediff ( 's' , $actiondate , _NowCalc())
        $timehaspastmin = Round($timehaspastsec / 60,1)
        $timehaspasthour = Round($timehaspastmin /60,1)
        
        select
            case $timehaspasthour >= 1
                $timetype="hour(s)"
                $timehaspast=$timehaspasthour
            case $timehaspastmin >= 1
                $timetype="minute(s)"
                $timehaspast=$timehaspastmin
            case Else
                $timetype="second(s)"
                $timehaspast=$timehaspastsec
        EndSelect
        
        select 
        case IsScreenSaverRunning() = False and IsWorkStationLocked() = False   
            $prediction = "was away for"
        case IsScreenSaverRunning() = True and IsWorkStationLocked() = False
            $prediction = "was working for"
        case IsScreenSaverRunning() = True and IsWorkStationLocked() = True
            $prediction = "(really idle now) was watching my screen for "
        case IsScreenSaverRunning() = False and IsWorkStationLocked() = True
            $prediction = "was working for"
        EndSelect
        
        $outputstring =  _NowCalc() & " - " & "scrsaver=" & IsScreenSaverRunning() & ",Locked=" & IsWorkStationLocked() & ", " & $prediction & " " & $timehaspast & " " & $timetype & @CRLF
        FileWrite($file , $outputstring )
        ConsoleWrite ( $outputstring)
        $actiondate =  _NowCalc()
    EndIf
    
    FileClose($file)
WEnd

Func IsWorkStationLocked()
    Local $Desktop, $Return
    
    $Return = DllCall("user32.dll", "long", "OpenDesktopA", "str", "Default", "dword", 0, "ubyte", 0, "dword", 0x100)
    $Desktop = $Return[0]
    
    If $Desktop = 0 Then
        Return True
    EndIf
    
    $Return = DllCall("user32.dll", "long", "SwitchDesktop", "dword", $Desktop)
    DllCall("user32.dll", "long", "CloseDesktop", "dword", $Desktop)
    
    If $Return[0] = 0 Then
        Return True
    Else 
        Return False
    EndIf
EndFunc

Func IsScreenSaverRunning()
    Local $Desktop, $Return, $Result
    
    $Result = DllStructCreate("uint")
    $Return = DllCall("user32.dll", "ubyte", "SystemParametersInfoA", "uint", 0x72, "uint", 0, "ptr", DllStructGetPtr($Result), "uint", 0)
    If $Return[0] = 0 Then Return SetError( 0, False)
    
    Return DllStructGetData($Result, 1) <> 0
EndFunc
Edited by SoulSHepard

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.