Jump to content

Activate Screen Saver after Locking Workstation


Recommended Posts

Is there a way in AutoIt to lock the screen and activate the current screen saver WITHOUT downloading and using 3rd party software?  Currently, I run MonitorES which starts the screen saver after I lock my workstation but I would like to be able to do it via AutoIt alone.  Basically, I'm looking for a way to ditch MonitorES, which runs continuously in the background.

I am aware of the AutoIt user function _WinAPI_LockWorkStation and it does work with MonitorES but I'd like a purely AutoIt solution, if possible.  If this is not possible, could this functionality be added to a future version of AutoIt?

Link to comment
Share on other sites

When I leave my workstation, I press a certain key combination which locks the computer.  MonitorES detects this condition and starts the current screensaver.  Without MonitorES, it would remain on the lock screen until such time as the screen saver is scheduled to start - about 10 minutes under our current user policy. 

It would be ideal if a parameter could be added to _WinAPI_LockWorkStation that would start the screen saver - the default, of course, would be to not start the screen saver in order to maintain backwards compatibility.

Link to comment
Share on other sites

Looking at the source for MonitorES, I believe the functionality should be very similar to the following (but it no workie and I can't see why :blink:):

;#AutoIt3Wrapper_Change2CUI=y
#include <SendMessage.au3>

Local Const $HWND_BROADCAST = 0xFFFF
Local Const $WM_SYSCOMMAND = 0x112
Local Const $SC_SCREENSAVE = 0xF140

Global $bStartScreenSaver = 0

Global Const $NOTIFY_FOR_THIS_SESSION = 0x0
Global Const $NOTIFY_FOR_ALL_SESSIONS = 0x1
Global Const $WM_WTSSESSION_CHANGE = 0x2B1

Global Const $WTS_CONSOLE_CONNECT = 0x1
Global Const $WTS_CONSOLE_DISCONNECT = 0x2
Global Const $WTS_REMOTE_CONNECT = 0x3
Global Const $WTS_REMOTE_DISCONNECT = 0x4
Global Const $WTS_SESSION_LOGON = 0x5
Global Const $WTS_SESSION_LOGOFF = 0x6
Global Const $WTS_SESSION_LOCK = 0x7
Global Const $WTS_SESSION_UNLOCK = 0x8
Global Const $WTS_SESSION_REMOTE_CONTROL = 0x9

Global $hGUI = GUICreate("WM_SESSION_CHANGE_MONITOR", 200, 200); GUI to receive notification
;GUISetState()

GUIRegisterMsg($WM_WTSSESSION_CHANGE, "_WM_WTSSESSION_CHANGE")
DllCall("Wtsapi32.dll", "int", "WTSRegisterSessionNotification", "hwnd", $hGUI, "dword", $NOTIFY_FOR_THIS_SESSION)

While 1
    If GUIGetMsg() = -3 Then ExitLoop
    If $bStartScreenSaver Then
        $bStartScreenSaver = 0
        ScreenSaver()
    EndIf
WEnd

DllCall("Wtsapi32.dll", "int", "WTSUnRegisterSessionNotification", "hwnd", $hGUI)

Func _WM_WTSSESSION_CHANGE($hWnd, $iMsgID, $wParam, $lParam)
    If $iMsgID <> $WM_WTSSESSION_CHANGE Then Return 0
    Local $sMsg = @CRLF & @HOUR & ":" & @Min & ":" & @SEC & " = "
    Switch $wParam
        Case $WTS_CONSOLE_CONNECT
            $sMsg &= "A session was connected to the console terminal."
        Case $WTS_CONSOLE_DISCONNECT
            $sMsg &= "A session was disconnected from the console terminal."
        Case $WTS_REMOTE_CONNECT
            $sMsg &= "A session was connected to the remote terminal."
        Case $WTS_REMOTE_DISCONNECT
            $sMsg &= "A session was disconnected from the remote terminal."
        Case $WTS_SESSION_LOGON
            $sMsg &= "A user has logged on to the session."
        Case $WTS_SESSION_LOGOFF
            $sMsg &= "A user has logged off the session."
        Case $WTS_SESSION_LOCK
            $sMsg &= "A session has been locked."
            $bStartScreenSaver = 1
        Case $WTS_SESSION_UNLOCK
            $sMsg &= "A session has been unlocked."
        Case $WTS_SESSION_REMOTE_CONTROL
            $sMsg &= "A session has changed its remote controlled status."
    EndSwitch
    $sMsg &= @CRLF & "$hWnd = " & $hWnd & @CRLF & _
            "$iMsgID = 0x" & Hex($iMsgID, 8) & @CRLF & _
            "$wParam = " & $wParam & @CRLF & _
            "$lParam = " & $lParam & @CRLF
    Return ConsoleWrite($sMsg & @CRLF)
EndFunc

Func ScreenSaver()
    ConsoleWrite("Starting Screensaver..." & @CRLF)
    sleep(1000)
    _SendMessage($HWND_BROADCAST, $WM_SYSCOMMAND, $SC_SCREENSAVE, 0)
EndFunc

 

Link to comment
Share on other sites

All I do is check "On resume, display logon screen" in my screen saver settings. 

This effectively locks the computer if your screen saver starts.

My script is them simply, launch screensaver.

#Include <WinAPI.au3>
#Include <WindowsConstants.au3>

Global Const $SC_SCREENSAVE = 0XF140
_SendMessage(_WinAPI_GetDesktopWindow(), $WM_SYSCOMMAND, $SC_SCREENSAVE, 0)

 

Link to comment
Share on other sites

  • 2 weeks later...

Spudw2k: A noble effort - thanks.

JohnOne: I was aware of the LockWorkStation function but, without MonitorES, it simply locks the workstation without activating the screensaver.

ViciousXUSMC: Your code works great - this resolves my issue.  Thanks.  I added it to WinAPISys.au3 in my copy of AutoIT under your name as _WinAPI_StartScreenSaver, no return value.  I should probably save a copy of WinAPISys in case it gets wiped out by a future AutoIT upgrade.  Thanks again.

Edited by Shadeclan
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...