Jump to content
Sign in to follow this  
traltixx

help with screen lock script in windows

Recommended Posts

traltixx

Hi,

I have a autoIT script (taken/modified from one of the scripts in this forum) that locks a screen unless a password is given or logged off.

My question is that is it possible to modify it so that it invokes the windows screensaver whenever it is inactive? Currently, it doesn't invoke the screensaver at all for some reason and I've already tried various eliminations and it seems that for some reason this script is preventing the screensaver from activating when there is no activity.

This is what it looks like:

; ----------------------------------------------------------------------------
; Screen Lock
;
; AutoIt Version: 3.2.0.1
; Author: Hallman \ CWorks
;
; HotKeys
; F9 = Close program   
; F11 = Enable ScreenLock
;
; ----------------------------------------------------------------------------

#include <GUIConstants.au3>
#include <string.au3>
#include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>
#include <EditConstants.au3>

Opt("TrayMenuMode",1)
Dim $Atempts = 0
Dim $Lock = 0
Dim $PassInput = ""
Dim $Label
Dim $Button1
Dim $ScreenyWindow = ""
Dim $PassWindow = ""
$Show_Controls_Timer = TimerInit()
Dim $Controls_Shown = 0


$PassWord = "password?!"
$logOffPassword = "farmoff"

Lock()

While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE And $Lock = 0
            Exit
            
        Case $msg = $Button1
            Lock()
            $Atempts = 0
                        Run("logoff.exe")
        Case $msg = $PassInput
            If GUICtrlRead($PassInput) == $PassWord Then
                Lock()
                MsgBox(0, "Atempts", "An incorrect password was entered " & $Atempts & " time(s).")
                $Atempts = 0
            Elseif GUICtrlRead($PassInput) == $logOffPassword Then
                Lock()
                $Atempts = 0
                Run("logoff.exe")
            Else            
                $Atempts += 1
                SplashMsg("Error", "Invalid Password", 220, 100)
            EndIf
           
        Case $msg = $GUI_EVENT_PRIMARYUP And $Lock = 1
            GUISetState(@SW_SHOW, $PassWindow)
            $Controls_Shown = 1
            $Show_Controls_Timer = TimerInit()
    EndSelect
   
    If TimerDiff($Show_Controls_Timer) > 10000 And $Controls_Shown = 1 Then
        HideWind()
    EndIf
   
    If WinExists("Windows Task Manager") And $Lock = 1 Then
        WinClose("Windows Task Manager")
        WinKill("Windows Task Manager")
    EndIf
   
    If WinActive($ScreenyWindow) = 0 And WinActive($PassWindow) = 0 And $Lock = 1 Then
        WinActivate($ScreenyWindow)
    EndIf
   
    If Not BitAND(WinGetState($ScreenyWindow, ""), 2) = 1 And $Lock = 1 Then
        GUISetState(@SW_SHOW)
    EndIf
   
    If $Lock = 1 And WinExists($ScreenyWindow) = 0 Then
        $ScreenyWindow = GUICreate("", @DesktopWidth, @DesktopHeight, -2, -2, BitOR($WS_POPUPWINDOW, $WS_EX_TOOLWINDOW))
        GUISwitch($ScreenyWindow)
        WinSetTrans($ScreenyWindow, "", 1)
        GUISetState(@SW_SHOW, $ScreenyWindow)
        WinSetOnTop($ScreenyWindow, "", 1)
        WinSetOnTop($PassWindow, "", 1)
    EndIf
WEnd

Func Lock()
    If $Lock = 0 Then
        RegWrite("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\System", "DisableTaskMgr", "REG_DWORD", 00000001)
        HotKeySet("{Esc}","HideWind")   
        HotKeySet("{F9}")   
        HotKeySet("{F11}")

        TraySetIcon("Shell32.dll", 47)
       
        $ScreenyWindow = GUICreate("", @DesktopWidth + 2, @DesktopHeight + 2, -2, -2, BitOR($WS_POPUPWINDOW, $WS_EX_TOOLWINDOW))
        GUISwitch($ScreenyWindow)
        WinSetTrans($ScreenyWindow, "", 1)
       
        Global $PassWindow = GUICreate("", 220, 130, -1, -1, BitOR($WS_POPUPWINDOW, $WS_EX_TOOLWINDOW))
        GUISwitch($PassWindow)
        GUISetState(@SW_HIDE)
        Global $Label = GUICtrlCreateLabel("The screen has been locked.", 10, 10, -1, 15)
       ;      GUICtrlSetColor(-1,0xff0000)
        Global $PassInput = GUICtrlCreateInput("Password", 10, 30, 200, 20, BitOR("",$ES_PASSWORD))
        Global $Label2 = GUICtrlCreateLabel("Type Password and hit Enter to Disable", 10, 55, -1, 15)
        Global $Label3 = GUICtrlCreateLabel("Or click the button below to logoff", 10, 75, -1, 15)
        Global $Button1 = GUICtrlCreateButton ("Log Off",  10, 95, 100)     
       ;      GUICtrlSetColor(-1,0xff0000)
        GUISetState(@SW_SHOW, $ScreenyWindow)
        WinSetOnTop($ScreenyWindow, "", 1)
        WinSetOnTop($PassWindow, "", 1)
        $Lock = 1
    Else
        GUIDelete($ScreenyWindow)
        GUIDelete($PassWindow)
        HotKeySet("{Esc}","HideWind")
        HotKeySet("{F9}", "close")
        HotKeySet("{F11}", "Lock")

        TraySetIcon("Shell32.dll", 44)
        RegWrite("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\System", "DisableTaskMgr", "REG_DWORD", 00000000)
        $Lock = 0
    EndIf
   
EndFunc  ;==>Lock

Func SplashMsg($S_Title = "", $S_Text = "", $S_Size_X = 300, $S_Size_Y = 300)
    SplashTextOn($S_Title, $S_Text & @CRLF & "Press Enter to close this window.", $S_Size_X, $S_Size_Y)
    HotKeySet("{ENTER}", "OffSplash")
EndFunc  ;==>SplashMsg

Func OffSplash()
    SplashOff()
    HotKeySet("{ENTER}")
EndFunc  ;==>OffSplash

Func close()
    Exit
EndFunc  ;==>close

Func HideWind()
    GUISetState(@SW_HIDE, $PassWindow)
    $Controls_Shown = 0
EndFunc

Thanks for any advice!

Share this post


Link to post
Share on other sites
Skruge

If $Lock = 1 And WinExists($ScreenyWindow) = 0 Then
Take a closer look at this line.

At this point in the code, your GUI doesn't exist. $ScreenyWindow is blank, so you're actually running WinExists(""), which will always be true. (Active Window)

Also, you're jumping right into a Message loop with no GUI created.

Both of these can be solved by creating the GUI before the loop.

As for invoking the screensaver, the current screensaver is stored in the registry here: HKEY_CURRENT_USER\Control Panel\Desktop\SCRNSAVE.EXE

Screensavers (.scr) are executable and should be run with a "/s" switch to activate.

Good luck!


[font="Tahoma"]"Tougher than the toughies and smarter than the smarties"[/font]

Share this post


Link to post
Share on other sites
traltixx

I dont understand, the function to create the GUI is called before the loop.

Anyway, I tried to make the GUI before the loop but the screensaver doesn't appear after some period of inactivity.

Thanks again for the help.

Share this post


Link to post
Share on other sites
Skruge

I dont understand, the function to create the GUI is called before the loop.

My bad, I was running the code in my head and missed the first Lock() call.

Couple of things I noticed:

- The "Screeny" GUI keeps getting activated, even over the password GUI. This makes it hard to type a password.

- If there are multiple monitors, only the primary is covered by the "Screeny" GUI.

I fixed these and added screensaver functionality (Triggered after 10 seconds of inactivity, the same time you hide the Password GUI).

Also, I don't like the transparent window because it makes the system appear non-responsive, so I made it visible. Feel free to WinSetTrans back to 1.

p.s. I prefer to create the GUIs up front (hidden), then just show/hide them. This eliminates the risk of creating duplicate GUIs and reading messages for a non-existent one.

It's a rare case, but after deleting a GUI, a window from another app may be created and re-use that handle, so it would trick your program into thinking that the window is your own.

CODE
; ----------------------------------------------------------------------------

; Screen Lock

;

; AutoIt Version: 3.2.0.1

; Author: Hallman \ CWorks

;

; HotKeys

; F9 = Close program

; F11 = Enable ScreenLock

;

; ----------------------------------------------------------------------------

#include <GUIConstants.au3>

#include <string.au3>

#include <GuiConstantsEx.au3>

#include <WindowsConstants.au3>

#include <EditConstants.au3>

Opt("TrayMenuMode", 1)

Dim $Atempts = 0

Dim $Lock = 0

Dim $PassInput = ""

Dim $Label

Dim $Button1

Dim $ScreenyWindow = ""

Dim $PassWindow = ""

$Show_Controls_Timer = TimerInit()

Dim $Controls_Shown = 0

Dim $a_Desktop

$PassWord = "password?!"

$logOffPassword = "farmoff"

ConsoleWrite("First Lock" & @CRLF)

Lock()

While 1

$msg = GUIGetMsg()

Select

Case $msg = $GUI_EVENT_CLOSE And $Lock = 0

Exit

Case $msg = $Button1

Lock()

$Atempts = 0

Run("logoff.exe")

Case $msg = $PassInput

If GUICtrlRead($PassInput) == $PassWord Then

$Controls_Shown = 0

Lock()

MsgBox(0, "Atempts", "An incorrect password was entered " & $Atempts & " time(s).", 30)

$Atempts = 0

ElseIf GUICtrlRead($PassInput) == $logOffPassword Then

Lock()

$Atempts = 0

Run("logoff.exe")

Else

$Atempts += 1

SplashMsg("Error", "Invalid Password", 220, 100)

EndIf

Case $msg = $GUI_EVENT_PRIMARYUP And $Lock = 1

GUISetState(@SW_SHOW, $PassWindow)

$Controls_Shown = 1

$Show_Controls_Timer = TimerInit()

EndSelect

If $Lock = 1 Then

If TimerDiff($Show_Controls_Timer) > 10000 And $Controls_Shown = 1 Then

HideWind()

_ScreenSaver()

EndIf

If WinExists("Windows Task Manager") Then

WinClose("Windows Task Manager")

WinKill("Windows Task Manager")

EndIf

If WinActive($PassWindow) = 0 Then

WinActivate($ScreenyWindow)

WinActivate($PassWindow)

EndIf

If Not BitAND(WinGetState($ScreenyWindow, ""), 2) = 1 Then

GUISetState(@SW_SHOW)

EndIf

If $Lock = 1 And WinExists($ScreenyWindow) = 0 Then

$a_Desktop = WinGetPos("[CLASS:Progman; INSTANCE:1]")

If IsArray($a_Desktop) Then

$ScreenyWindow = GUICreate("", $a_Desktop[2], $a_Desktop[3], $a_Desktop[0], $a_Desktop[1], BitOR($WS_POPUPWINDOW, $WS_EX_TOOLWINDOW))

Else

$ScreenyWindow = GUICreate("", @DesktopWidth + 2, @DesktopHeight + 2, -2, -2, BitOR($WS_POPUPWINDOW, $WS_EX_TOOLWINDOW))

EndIf

GUISwitch($ScreenyWindow)

GUISetBkColor(0x000000, $ScreenyWindow)

WinSetTrans($ScreenyWindow, "", 200)

GUISetState(@SW_SHOW, $ScreenyWindow)

WinSetOnTop($ScreenyWindow, "", 1)

WinSetOnTop($PassWindow, "", 1)

EndIf

EndIf

WEnd

Func Lock()

ConsoleWrite("Lock = " & $Lock & @CRLF)

If $Lock = 0 Then

RegWrite("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\System", "DisableTaskMgr", "REG_DWORD", 00000001)

HotKeySet("{Esc}", "HideWind")

HotKeySet("{F9}")

HotKeySet("{F11}")

TraySetIcon("Shell32.dll", 47)

$a_Desktop = WinGetPos("[CLASS:Progman; INSTANCE:1]")

If IsArray($a_Desktop) Then

$ScreenyWindow = GUICreate("", $a_Desktop[2], $a_Desktop[3], $a_Desktop[0], $a_Desktop[1], BitOR($WS_POPUPWINDOW, $WS_EX_TOOLWINDOW))

Else

$ScreenyWindow = GUICreate("", @DesktopWidth + 2, @DesktopHeight + 2, -2, -2, BitOR($WS_POPUPWINDOW, $WS_EX_TOOLWINDOW))

EndIf

GUISwitch($ScreenyWindow)

GUISetBkColor(0x000000, $ScreenyWindow)

WinSetTrans($ScreenyWindow, "", 200)

Global $PassWindow = GUICreate("", 220, 130, -1, -1, BitOR($WS_POPUPWINDOW, $WS_EX_TOOLWINDOW))

GUISwitch($PassWindow)

GUISetState(@SW_HIDE)

Global $Label = GUICtrlCreateLabel("The screen has been locked.", 10, 10, -1, 15)

; GUICtrlSetColor(-1,0xff0000)

Global $PassInput = GUICtrlCreateInput("Password", 10, 30, 200, 20, BitOR("", $ES_PASSWORD))

Global $Label2 = GUICtrlCreateLabel("Type Password and hit Enter to Disable", 10, 55, -1, 15)

Global $Label3 = GUICtrlCreateLabel("Or click the button below to logoff", 10, 75, -1, 15)

Global $Button1 = GUICtrlCreateButton("Log Off", 10, 95, 100)

; GUICtrlSetColor(-1,0xff0000)

GUISetState(@SW_SHOW, $ScreenyWindow)

WinSetOnTop($ScreenyWindow, "", 1)

WinSetOnTop($PassWindow, "", 1)

$Lock = 1

Else

GUIDelete($ScreenyWindow)

GUIDelete($PassWindow)

HotKeySet("{Esc}", "HideWind")

HotKeySet("{F9}", "close")

HotKeySet("{F11}", "Lock")

TraySetIcon("Shell32.dll", 44)

RegWrite("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\System", "DisableTaskMgr", "REG_DWORD", 00000000)

$Lock = 0

EndIf

EndFunc ;==>Lock

Func SplashMsg($S_Title = "", $S_Text = "", $S_Size_X = 300, $S_Size_Y = 300)

SplashTextOn($S_Title, $S_Text & @CRLF & "Press Enter to close this window.", $S_Size_X, $S_Size_Y)

HotKeySet("{ENTER}", "OffSplash")

EndFunc ;==>SplashMsg

Func OffSplash()

SplashOff()

HotKeySet("{ENTER}")

EndFunc ;==>OffSplash

Func close()

Exit

EndFunc ;==>close

Func HideWind()

GUISetState(@SW_HIDE, $PassWindow)

$Controls_Shown = 0

EndFunc ;==>HideWind

Func _ScreenSaver()

Local $sScreenSaver = RegRead("HKEY_CURRENT_USER\Control Panel\Desktop", "SCRNSAVE.EXE")

If ProcessExists($sScreenSaver) Then

ProcessWaitClose($sScreenSaver)

Else

RunWait($sScreenSaver & " /s")

EndIf

EndFunc ;==>_ScreenSaver


[font="Tahoma"]"Tougher than the toughies and smarter than the smarties"[/font]

Share this post


Link to post
Share on other sites
traltixx

Thanks for the improvements. I didn't think of multiple monitors.

However, the screensaver starts for 1 second and then dissapears. Is the setOnTop triggers disabling the screensaver?

Thanks again.

EDIT: I forgot to mention that I'm using windows XP SP 2 . Maybe it makes a difference. I dont know.

Edited by traltixx

Share this post


Link to post
Share on other sites
CodyBarrett

i have a screenlock in my sig... you ll need a INI to change the pass in it whough.. maybe it will help your situation

EDIT

its called "SCREEN FREEZE"

Edited by CodyBarrett

Share this post


Link to post
Share on other sites
traltixx

Thanks for the suggestion CodyBarrett, however your script (1) the screensaver does not activate if there is no activity and (2) the screen isn't transparent. I know how to change the (2) one but the (1) still puzzles me.

EDIT: My mistake Skruge. The default screensaver path doesn't exist on my machine. Stupid 64-bit windows. Thanks a bunch! It works like a charm now!

Edited by traltixx

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.