Pronunciation / adjective
Bringing something good and unforseen.
    Skip Navigation Links > Tech Blog
   

Screen Saver Utility Program - Lock Windows and Start a ScreenSaver (Freeware)



 

  6/24/2009 7:38:16 AM

Screen Saver Utility Program - Lock Windows and Start a ScreenSaver (Freeware)

Permalink

There are two methods I know of to lock windows and "immediately" run a screensaver at the same time. The first method uses a batch file to run the screensaver. The second line of the batch file locks windows. But that line doesn't fire until after the screensaver is stopped. If a screensaver contains a way to be bypassed (some screensavers can be run in different modes such as "windowed") then a batch file to run a screensaver will not secure your machine. Another method which uses a .NET console application (command prompt program) to reset the screensaver's timeout value when windows locks and the sets it back again when windows unlocks is a secure way to lock your computer and run your screensaver right away. Using this method the screensaver automatically runs 1 minute after the computer is locked. I've created a freeware application that does just that.

To use the application first download its executable from here: Screen Utility Download (.zip) (For any programmers who would like the source code rather than the compiled .exe, I've placed it at the very end of this article. (It's written in VB.NET 3.5 - if you'd like a C# version feel free to email me I'd be happy to help translate it.)

Here's how to set things up:

  1. Unzip the file you downloaded and place the ScreenSaverUtility.exe in a "permanent" location. (Make note of where you save it.)
  2. Open windows task scheduler (The easiest way to do so in windows vista is to click the "start" button and then type taskschd.msc into the "search" box and hit enter. You may be prompted for whether or not to continue by vista's user account control, if you are click "Continue".)
  3. Task Scheduler lets you create folders to keep tasks sorted. This is completely optional but a good idea. So create a new folder to keep your screensaver tasks in by right clicking on the main folder and choosing "New Folder" from the menu. Repeat this process to create a folder structure to your liking.
    Screen Saver Utility How To - Screenshot 1
  4. Next right click the folder you'll be placing your tasks in and choose "Create Task" from the menu.
    Screen Saver Utility How To - Screenshot 2
  5. On the "General" tab of the create task window that opens enter a name for your task such as: "Set ScreenSaver Timeout on Windows Lock". Also make sure that the "Run only when user is logged on" radio button is selected.
    Screen Saver Utility How To - Screenshot 3
  6. Switch to the "Triggers" tab and click the "New" button.
    Screen Saver Utility How To - Screenshot 4

    In the window that opens use the drop down to set the task to run "On workstation lock" and then click the "OK" button.
    Screen Saver Utility How To - Screenshot 5
  7. Switch to the "Actions" tab and click the "New" button there. In the window that opens use the "Browse" button to select the "ScreenSaverUtility.exe" you unzipped earlier. In the "Add arguments (optional)" text box place a "/L" (without the quotes).
    Screen Saver Utility How To - Screenshot6
  8. Laptops Only On the "Condtions" tab clear the "Power Settings" checkboxes so the script will still run when running on batteries.
    Screen Saver Utility How To - Screenshot 7
  9. Now repeat the same process but create a task that resets the screensaver timeout when windows unlocks. Name the task something like: "Reset ScreenSaver Timeout on Windows Unlock".
  10. On the "Triggers" tab for this task set the task to run when the workstation unlocks.
    Screen Saver Utility How To - Screenshot 8
  11. On the "Actions" tab once again browse to the "ScreenSaverUtility.exe". But this time in the "Add arguments" text box place a: "/U 1800" Where 1800 is the number of seconds the screensaver timeout is set to. 1800 is 1/2 an hour. Change this value to whatever you'd like. If you only put in the "/U" and don't specify a time the screensaver timeout will default to 600 seconds (10 minutes).
    Screen Saver Utility How To - Screenshot 9

That's it. Now when you lock your computer (the easiest way is to press the "Windows Key" + L) your chosen screensaver will start in one minute. And when you unlock your computer the screensaver's timeout will be reset back to your chosen default value.

Source Code:

  1. Imports System.Runtime.InteropServices
  2. Module ScreenSaverUtility
  3. Private Const SPI_SETSCREENSAVETIMEOUT As Int32 = 15
  4. Private Const SPI_GETSCREENSAVETIMEOUT As Int32 = 14
  5. Private Const SPIF_UPDATEINIFILE As Int32 = &H1
  6. Private Const SPIF_SENDWININICHANGE As Int32 = &H2
  7. Private Declare Function SystemParametersInfo Lib "user32" Alias "SystemParametersInfoA" (ByVal uiAction As Int32, ByVal uiParam As Int32, ByVal pvParam As Int32, ByVal fWinIni As Int32) As Int32
  8. <Flags()> _
  9. Private Enum SHOW_WINDOW As Integer
  10. SW_HIDE = 0
  11. SW_SHOWNORMAL = 1
  12. SW_NORMAL = 1
  13. SW_SHOWMINIMIZED = 2
  14. SW_SHOWMAXIMIZED = 3
  15. SW_MAXIMIZE = 3
  16. SW_SHOWNOACTIVATE = 4
  17. SW_SHOW = 5
  18. SW_MINIMIZE = 6
  19. SW_SHOWMINNOACTIVE = 7
  20. SW_SHOWNA = 8
  21. SW_RESTORE = 9
  22. SW_SHOWDEFAULT = 10
  23. SW_FORCEMINIMIZE = 11
  24. SW_MAX = 11
  25. End Enum
  26. Private Declare Function ShowWindow Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal nCmdShow As SHOW_WINDOW) As Boolean
  27. Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
  28. Sub Main()
  29. Console.Title = "ScreenSaverUtility"
  30. Dim LockOrUnlock As String = String.Empty
  31. Dim ResetTime As Int32 = 600
  32. If My.Application.CommandLineArgs.Count > 0 Then
  33. LockOrUnlock = My.Application.CommandLineArgs(0).ToUpper().Replace("-", "/")
  34. End If
  35. If My.Application.CommandLineArgs.Count > 1 Then
  36. If (Not Int32.TryParse(My.Application.CommandLineArgs(1), ResetTime)) Then
  37. ResetTime = 600
  38. End If
  39. End If
  40. If (LockOrUnlock = "/?") Then
  41. DisplayHelp()
  42. ElseIf (LockOrUnlock = "/L") Then
  43. SetScreenSaverTimeout(60)
  44. LockWorkStation()
  45. ElseIf (LockOrUnlock = "/U") Then
  46. SetScreenSaverTimeout(ResetTime)
  47. Else
  48. SetScreenSaverTimeout(600)
  49. End If
  50. End Sub
  51. Public Function SetScreenSaverTimeout(ByVal timeout As Int32) As Boolean
  52. ShowWindow(FindWindow(Nothing, Console.Title), SHOW_WINDOW.SW_HIDE)
  53. If (timeout < 60) Or (timeout > 3600) Then Exit Function
  54. Dim Result As Int32 = SystemParametersInfo(SPI_SETSCREENSAVETIMEOUT, timeout, 0, SPIF_UPDATEINIFILE Or SPIF_SENDWININICHANGE)
  55. If Result = 0 Then Throw New Exception("Api return null")
  56. End Function
  57. <DllImport("user32.dll", SetLastError:=True)> _
  58. Private Function LockWorkStation() As <MarshalAs(UnmanagedType.Bool)> Boolean
  59. End Function
  60. Private Sub DisplayHelp()
  61. Dim HelpStringBuilder As New System.Text.StringBuilder()
  62. With HelpStringBuilder
  63. .AppendLine()
  64. .AppendLine("/L - Locks the computer and sets screensaver to start in 1 minute.")
  65. .AppendLine("/U <ScreenSaverTimeOutValue> - Resets the screensaver timeout to the given value (in seconds) - Timeout defaults to 600 seconds (10 minutes) if not specified.")
  66. .AppendLine()
  67. .AppendLine("Use:")
  68. .AppendLine("ScreenSaverUtility.exe /L")
  69. .AppendLine("ScreenSaverUtility.exe /U 1800")
  70. End With
  71. Console.Write(HelpStringBuilder.ToString())
  72. End Sub
  73. End Module

<June 2009>
SuMoTuWeThFrSa
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011


Microsoft Certified Professional   © 2013 Fortunate.  All rights reserved.
contact: justin@aboutfortunate.com