Jump to content

IsWorkStationLocked() & IsScreenSaverRunning()


Zinthose
 Share

Recommended Posts

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

Link to comment
Share on other sites

@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
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...