Using SPI_SETSCREENSAVESECURE doesn't enable password protection for the Windows screensaver

614 views Asked by At

When I run the following line of code, my screensaver's password protection isn't enabled. I've also checked the registry key that used to control password protection and it doesn't get toggled either.

    If SystemParametersInfo(SPI_SETSCREENSAVESECURE, 1, nullVar, SPIF_SENDWININICHANGE) = 0 Then
        Throw New WindowsDotHException(ErrorToString(Err.LastDllError()) & " (Error number " & Err.LastDllError & ")")
    End If

Context:

WindosDotHException is my way of handling error codes from SystemParametersInfo.

    ' Signatures for unmanaged calls
<DllImport("user32.dll", CharSet:=CharSet.Auto)> _
Private Shared Function SystemParametersInfo(uAction As Integer, uParam As Integer, ByRef lpvParam As Integer, flags As Integer) As Boolean
End Function

<DllImport("user32.dll", CharSet:=CharSet.Auto)> _
Private Shared Function SystemParametersInfo(uAction As Integer, uParam As Integer, ByRef lpvParam As Boolean, flags As Integer) As Boolean
End Function

<DllImport("user32.dll", CharSet:=CharSet.Auto)> _
Private Shared Function PostMessage(hWnd As IntPtr, wMsg As Integer, wParam As Integer, lParam As Integer) As Integer
End Function

<DllImport("user32.dll", CharSet:=CharSet.Auto)> _
Private Shared Function OpenDesktop(hDesktop As String, Flags As Integer, Inherit As Boolean, DesiredAccess As UInteger) As IntPtr
End Function

<DllImport("user32.dll", CharSet:=CharSet.Auto)> _
Private Shared Function CloseDesktop(hDesktop As IntPtr) As Boolean
End Function

<DllImport("user32.dll", CharSet:=CharSet.Auto)> _
Private Shared Function EnumDesktopWindows(hDesktop As IntPtr, callback As EnumDesktopWindowsProc, lParam As IntPtr) As Boolean
End Function

<DllImport("user32.dll", CharSet:=CharSet.Auto)> _
Private Shared Function IsWindowVisible(hWnd As IntPtr) As Boolean
End Function

<DllImport("user32.dll", CharSet:=CharSet.Auto)> _
Public Shared Function GetForegroundWindow() As IntPtr
End Function

' Callbacks
Private Delegate Function EnumDesktopWindowsProc(hDesktop As IntPtr, lParam As IntPtr) As Boolean

' Constants
Private Const SPI_GETSCREENSAVERACTIVE As Integer = 16
Private Const SPI_SETSCREENSAVERACTIVE As Integer = 17
Private Const SPI_GETSCREENSAVERTIMEOUT As Integer = 14
Private Const SPI_SETSCREENSAVERTIMEOUT As Integer = 15
Private Const SPI_GETSCREENSAVERRUNNING As Integer = 114
Private Const SPIF_SENDWININICHANGE As Integer = 2
Private Const SPI_SETSCREENSAVESECURE As Integer = 119

Private Const DESKTOP_WRITEOBJECTS As UInteger = &H80
Private Const DESKTOP_READOBJECTS As UInteger = &H1
Private Const WM_CLOSE As Integer = 16

Operating system: Windows 7 32-bit edition

Language: VB .Net

Things I've tried:

  • Rebooting the computer
  • logging off and logging back on after running the code

What can I do to enable password protection in Windows 7?

0

There are 0 answers