Jump to content

Recommended Posts

Posted (edited)

I'm trying to make a script to configure the screen saver.

My problem is that I'm not entirely sure if I got the SystemParametersInfo constants right. Haven't gotten around to testing the script yet since I'm wary on what could happen if I feed unintentional data to the wrong system parameter set or query.

Can someone help me confirm this?

Credit goes to Volly and WallpaperSet which I based this script on.

#include <WinAPI.au3>
Opt("MustDeclareVars", 1)

_ScreenSaverSet("C:\WINDOWS\system32\ssstars.scr", True, True, 60)

; ===================================================================
; ScreenSaverSet($path, $active = True, $secure = False, $timeout = 600)
;
; Sets the screen saver to the specified file.
; Parameters:
;   $path - IN - The path to the screen saver file. Can be empty string if $active is false.
;   $active - IN/OPTIONAL - Sets the screen saver active state.
;   $secure - IN/OPTIONAL - Sets the password protect on resume.
;   $timeout - IN/OPTIONAL - Sets the idle timeout for screen saver.
; Returns:
;   Success - Returns 1.
;   Failure - Returns 0 and sets @extended depending on error(s). Errors values are cumulative.
;       1 - File in $path does not exist.
;       2 - Failed to set SPI_SETSCREENSAVEACTIVE.
;       4 - Failed to set SPI_SETSCREENSAVESECURE.
;       8 - Failed to set SPI_SETSCREENSAVETIMEOUT.
; ===================================================================
Func _ScreenSaverSet($path, $active = True, $secure = False, $timeout = 600)
    ;registry key for screen saver settings
    Local Const $regkey = "HKEY_CURRENT_USER\Control Panel\Desktop"
    ;constants for SystemParametersInfo
    Local Const $SPI_SETSCREENSAVEACTIVE = 0x0011
    Local Const $SPI_SETSCREENSAVESECURE = 0x0077
    Local Const $SPI_SETSCREENSAVETIMEOUT = 15
    Local Const $SPIF_UPDATEINIFILE = 1
    Local Const $SPIF_SENDCHANGE = 2
    Local Const $WININI = BitOR($SPIF_UPDATEINIFILE, $SPIF_SENDCHANGE)
    ;results of _WinAPI_SystemParametersInfo calls
    Local $result1
    Local $result2
    Local $result3

    ;if activating valid file
    If $active == True And FileExists($path) Then
        ;set screen saver file in registry
        If $active Then
            RegWrite($regkey, "SCRNSAVE.EXE", "REG_SZ", $path)
        EndIf

        ;set screen saver to active
        If $active Then
            $result1 = _WinAPI_SystemParametersInfo($SPI_SETSCREENSAVEACTIVE, 1, 0, $WININI)
        Else
            $result1 = _WinAPI_SystemParametersInfo($SPI_SETSCREENSAVEACTIVE, 0, 0, $WININI)
        EndIf

        ;set prompt password on resume
        If $secure Then
            $result2 = _WinAPI_SystemParametersInfo($SPI_SETSCREENSAVESECURE, 1, 0, $WININI)
        Else
            $result2 = _WinAPI_SystemParametersInfo($SPI_SETSCREENSAVESECURE, 0, 0, $WININI)
        EndIf
        
        ;set idle timeout
        If $timeout > 60 Then
            $result3 = _WinAPI_SystemParametersInfo($SPI_SETSCREENSAVETIMEOUT, Int($timeout / 60) * 60, 0, $WININI)
        Else
            $result3 = _WinAPI_SystemParametersInfo($SPI_SETSCREENSAVETIMEOUT, 60, 0, $WININI)
        EndIf
        
        ;if success on all calls
        If $result1 And $result2 And $result3 Then
            Return 1
        EndIf

        ;set error flags
        Local $extended = 0
        If Not $result1 Then
            $extended += 2
        EndIf
        If Not $result2 Then
            $extended += 4
        EndIf
        If Not $result3 Then
            $extended += 8
        EndIf

        Return SetError(1, $extended, 0)
        
    Else
        ;if deactivating screen saver
        If Not $active Then
            $result1 = _WinAPI_SystemParametersInfo($SPI_SETSCREENSAVEACTIVE, 0, 0, $WININI)

            ;if success on call
            If $result1 Then
                Return 1
            Else
                Return SetError(1, 2, 0)
            EndIf
        EndIf
    EndIf
    ;file does not exist
    Return SetError(1, 1, 0)
EndFunc
Edited by omikron48
Posted (edited)

Figured it out myself (with aid from google).

Tested on Windows XP 32-bit, not too extensively though.

#include <WinAPI.au3>
Opt("MustDeclareVars", 1)

Local $return = _ScreenSaverSet("C:\WINDOWS\system32\logon.scr", True, False, 10)

; ===================================================================
; ScreenSaverSet($path, $active = True, $secure = False, $timeout = 600)
;
; Sets the screen saver to the specified file.
; Parameters:
;   $path - IN - The path to the screen saver file. Can be empty string if $active is false.
;   $active - IN/OPTIONAL - Sets the screen saver active state.
;   $secure - IN/OPTIONAL - Sets the password protect on resume.
;   $timeout - IN/OPTIONAL - Sets the idle timeout for screen saver.
; Returns:
;   Success - Returns 1.
;   Failure - Returns 0 and sets @error to 1 and @extended depending on error(s). Errors values are cumulative.
;       1  - File in $path does not exist.
;       2  - Invalid timeout value.
;       4  - Failed to set SPI_SETSCREENSAVEACTIVE.
;       8  - Failed to set registry entry ScreenSaverIsSecure.
;       16 - Failed to set SPI_SETSCREENSAVETIMEOUT.
;       32 - Failed to delete registry entry SCRNSAVE.EXE
; ===================================================================
Func _ScreenSaverSet($path, $active = True, $secure = False, $timeout = 10)
    ;registry key for screen saver settings
    Local Const $regkey = "HKEY_CURRENT_USER\Control Panel\Desktop"
    ;constants for SystemParametersInfo
    Local Const $SPI_SETSCREENSAVEACTIVE = 0x0011
    Local Const $SPI_SETSCREENSAVETIMEOUT = 0x000F
    Local Const $SPIF_UPDATEINIFILE = 1
    Local Const $SPIF_SENDCHANGE = 2
    Local Const $WININI = BitOR($SPIF_UPDATEINIFILE, $SPIF_SENDCHANGE)
    ;results of _WinAPI_SystemParametersInfo calls
    Local $result1
    Local $result2
    Local $result3

    ;if activating valid file
    If $active == True And FileExists($path) Then
        ;if invalid timeout
        If Not IsInt($timeout) Then
            SetError(1, 2, 0)
        EndIf
        
        ;set screen saver file in registry
        If $active Then
            RegWrite($regkey, "SCRNSAVE.EXE", "REG_SZ", $path)
        EndIf

        ;set screen saver to active
        If $active Then
            $result1 = _WinAPI_SystemParametersInfo($SPI_SETSCREENSAVEACTIVE, 1, 0, $WININI)
        Else
            $result1 = _WinAPI_SystemParametersInfo($SPI_SETSCREENSAVEACTIVE, 0, 0, $WININI)
        EndIf

        If $secure Then
            $result2 = RegWrite($regkey, "ScreenSaverIsSecure", "REG_SZ", 1)
        Else
            $result2 = RegWrite($regkey, "ScreenSaverIsSecure", "REG_SZ", 0)
        EndIf

        ;set idle timeout
        If $timeout > 1 Then
            $result3 = _WinAPI_SystemParametersInfo($SPI_SETSCREENSAVETIMEOUT, Int($timeout) * 60, 0, $WININI)
        Else
            $result3 = _WinAPI_SystemParametersInfo($SPI_SETSCREENSAVETIMEOUT, 60, 0, $WININI)
        EndIf
        
        ;if success on all calls
        If $result1 And $result2 == 1 And $result3 Then
            Return 1
        EndIf

        ;set error flags
        Local $extended = 0
        If Not $result1 Then
            $extended += 4
        EndIf
        If $result2 == 0 Then
            $extended += 8
        EndIf
        If Not $result3 Then
            $extended += 16
        EndIf

        Return SetError(1, $extended, 0)
        
    Else
        ;if deactivating screen saver
        If Not $active Then
            $result1 = _WinAPI_SystemParametersInfo($SPI_SETSCREENSAVEACTIVE, 0, 0, $WININI)

            ;if success on call
            If $result1 Then
                ;remove screen saver file entry in registry
                If RegDelete($regkey, "SCRNSAVE.EXE") == 1 Then
                    Return 1
                Else
                    SetError(1, 32, 0)
                EndIf
            Else
                Return SetError(1, 4, 0)
            EndIf
        EndIf
    EndIf
    ;file does not exist
    Return SetError(1, 1, 0)
EndFunc ;_ScreenSaverSet()
Edited by omikron48
  • 1 year later...

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...