Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

Things are not as simple as they seems at first. -- Edward Thorp


computers / alt.windows7.general / Re: Setting screensaver from command-line [SOLVED]

Re: Setting screensaver from command-line [SOLVED]

<uhav5s$nrm6$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/computers/article-flat.php?id=7197&group=alt.windows7.general#7197

  copy link   Newsgroups: alt.windows7.general
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: java@evij.com.invalid (Java Jive)
Newsgroups: alt.windows7.general
Subject: Re: Setting screensaver from command-line [SOLVED]
Date: Wed, 25 Oct 2023 12:46:03 +0100
Organization: A noiseless patient Spider
Lines: 291
Message-ID: <uhav5s$nrm6$1@dont-email.me>
References: <ugh5rq$k0fu$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Wed, 25 Oct 2023 11:46:04 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="600267c8360b36a1c6cad8b4e7d7b168";
logging-data="782022"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19uks1C0s65HkF8n3Th6et6hAbJC+znoNA="
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:68.0) Gecko/20100101
Thunderbird/68.4.2
Cancel-Lock: sha1:WFNUHehxPYTEyooX8Y2eXRUGKVY=
In-Reply-To: <ugh5rq$k0fu$1@dont-email.me>
Content-Language: en-GB
 by: Java Jive - Wed, 25 Oct 2023 11:46 UTC

On 15/10/2023 18:00, Java Jive wrote:
>

As a reminder ...

> I have created the BATch file appended below, which is intended to
> provide a quick and painless way to change the screensaver and power
> modes from their normal settings, Blank & Stay Awake, to a disabled
> state, None and Presentation, and back again.  This is easy enough to do
> via the relevant dialog boxes, but is irritatingly fiddly because some
> of the settings are under Personalisation, Screensaver and others under
> Power, hence the desire for a quicker and simpler method.
>
> The power setting change between Stay Awake and Presentation appears to
> be working correctly, but the screensaver change is failing.  If I run,
> say ...
>     Screensave /0
> .... but then launch the control panel for the screensaver, the choice
> of screensaver is correctly None, but the option "On resume, display
> logon screen" is still checked and this means that the screen will still
> go to the logon screen when the timer expires.  Conversely if I clear
> the option and do ...
>     Screensave /1
> .... and then launch the screensaver dialog again, the option remains
> unchecked, which means that if the timer expires, or I hibernate the
> machine, I'm not prompted for a password, which of course is insecure.
>
> All online sources that I've read suggest that the following registry
> settings should be enough to set the screensaver ...
>
> With Blank screensaver:
>
> [HKEY_CURRENT_USER\Control Panel\Desktop]
> "ScreenSaveActive"="1"
> "ScreenSaverIsSecure"="1"
> "SCRNSAVE.EXE"="C:\\Windows\\system32\\scrnsave.scr"
>
> With no screensaver:
>
> [HKEY_CURRENT_USER\Control Panel\Desktop]
> "ScreenSaveActive"="0"
> "ScreenSaverIsSecure"="0"
>
> ie: "SCRNSAVE.EXE" setting deleted
>
> .... and those are exactly what my BATch file sets.  Further, a before
> and after comparison of the entire registry after changing just the
> screensaver lock setting manually suggests that the ScreenSaverIsSecure
> setting is the only thing of any seeming relevance which is changed by
> altering this setting:
>
> [HKU\<ID>\Control Panel\Desktop]

BTW, ditto for [HKCU\Control Panel\Desktop], but an entire registry dump
doesn't show HKCU as we see it in Regedit, only as HKU\<ID>.

> "ScreenSaverIsSecure"="1"
>
> ; As expected, value changes to "0"

[Snip further detail included originally for completeness' sake on the
off chance of it being relevant, but now shown to be irrelevant]

> Now I'm wondering if it is necessary somehow to have the BATch file
> alert the system, presumably the Explorer instance that is running the
> Desktop, that the user registry hive has changed and it needs to reload
> it.  I could kill that Explorer instance, actually it would have to be
> all Explorer instances, and relaunch Explorer, but doing that is way too
> complicated for a "quicker and simpler" method, and actually pointless
> because it would be easier just to continue altering the settings manually.

As discussed most helpfully by JJ, something like the above is indeed
necessary. The BATch file is almost unchanged, the single new line to
achieve the above, by calling a powershell script, is commented below,
and said powershell script is discussed and appended below it [as
always, beware unintended line wrap in both] ...

Working BATch file:
@ECHO OFF
Rem ScreenSave.bat
Rem ==============
Rem
Rem Program to turn on & off the screen saver and power screen blanking
options

Rem Non-empty string here or /0 or /1 leading parameter turns the screen
saver off and on respectively
SET Switch="1"
Rem Non-empty string here or /D leading parameter gives debugging messages
SET Debug=""

:NextPar
IF /i "%1" EQU "/0" (
SET Switch="0"
SHIFT
GOTO NextPar
)
IF /i "%1" EQU "/1" (
SET Switch="1"
SHIFT
GOTO NextPar
)
IF /i "%1" EQU "/D" (
SET Debug="Y"
SHIFT
GOTO NextPar
)

IF %Switch%=="0" GOTO Off

:On
Rem Set screensaver to 'Blank'
ECHO Setting ScreenSaver to 'Blank'
reg add "HKEY_CURRENT_USER\Control Panel\Desktop" /v SCRNSAVE.EXE /t
REG_SZ /d C:\Windows\system32\scrnsave.scr /f > NUL 2>&1
reg add "HKEY_CURRENT_USER\Control Panel\Desktop" /v ScreenSaverIsSecure
/t REG_SZ /d 1 /f > NUL 2>&1
reg add "HKEY_CURRENT_USER\Control Panel\Desktop" /v ScreenSaveActive /t
REG_SZ /d 1 /f > NUL 2>&1
Rem Set power scheme to 'Stay Awake'
CALL :FindPowerGUID "Stay Awake" PName GUID
ECHO Setting Power Scheme to '%PName%'
powercfg /setactive %GUID%
GOTO Quit

:Off
Rem Set screensaver to 'None'
ECHO Setting ScreenSaver to 'None'
reg delete "HKEY_CURRENT_USER\Control Panel\Desktop" /v SCRNSAVE.EXE /f
> NUL 2>&1
reg add "HKEY_CURRENT_USER\Control Panel\Desktop" /v ScreenSaverIsSecure
/t REG_SZ /d 0 /f > NUL 2>&1
reg add "HKEY_CURRENT_USER\Control Panel\Desktop" /v ScreenSaveActive /t
REG_SZ /d 0 /f > NUL 2>&1
Rem Set power scheme to 'Presentation'
CALL :FindPowerGUID "Presentation" PName GUID
ECHO Setting Power Scheme to '%PName%'
powercfg /setactive %GUID%
GOTO Quit

:FindPowerGUID
SETLOCAL EnableDelayedExpansion
for /f "usebackq tokens=1-4*" %%A in (`powercfg /list ^| find /i %1`) do (
IF %Debug% NEQ "" Echo %%D %%E
SET _Name=%%E
SET _GUID=%%D
IF %Debug% NEQ "" Echo _Name="!_Name!"
SET _Name="!_Name:(=!"
SET _Name=!_Name:"=!
IF %Debug% NEQ "" Echo _Name="!_Name!"
SET _Name="!_Name:)=!"
SET _Name=!_Name:"=!
IF %Debug% NEQ "" Echo _Name="!_Name!"
SET _Name="!_Name: *=!"
SET _Name=!_Name:"=!
IF %Debug% NEQ "" Echo _Name="!_Name!"
)
ENDLOCAL & (
IF %Debug% NEQ "" (
Echo SET %~2="%_Name%"
Echo SET %~3="%_GUID%" & Echo.
)
SET "%~2=%_Name%"
SET "%~3=%_GUID%"
)
SET _Name=
SET _GUID=
EXIT /B

:Quit
REM Line below is only significant change from previous version
powershell -ExecutionPolicy ByPass -File
C:\Programs\Utilities\WMSettingsChange.ps1
SET Switch=
SET Debug=
SET PName=
SET GUID=

ECHO.

As far as the powershell script goes, previously I posted the following,
but no-one, including myself, noticed the embarrassingly basic bug -
it includes only a function definition, not a call to the function so
defined, and consequently does nothing. Duh! Sorry about that, my
excuse is that it's been quite a while since I last did any serious
programming.

> WMSettingsChange.ps1:
> =====================
>
> Add-Type -Namespace Win32 -Name NativeMethods -MemberDefinition @"
> [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
> public static extern IntPtr SendMessageTimeout(IntPtr hWnd, uint Msg,
> UIntPtr wParam, string lParam, uint fuFlags, uint uTimeout, out UIntPtr
> lpdwResult);
> "@
>
> function Send-SettingChange {
> $HWND_BROADCAST = [IntPtr] 0xffff;
> $WM_SETTINGCHANGE = 0x1a;
> $result = [UIntPtr]::Zero
>
> [void] ([Win32.Nativemethods]::SendMessageTimeout($HWND_BROADCAST,
> $WM_SETTINGCHANGE, [UIntPtr]::Zero, "HKEY_CURRENT_USER\Control
> Panel\Desktop", 2, 5000, [ref] $result))
> }
>
> For the registry key name I tried all of (as strings in quotes, an error
> was generated when I tried the last without)...
>
> HKEY_CURRENT_USER\Control Panel\Desktop
> HKEY_CURRENT_USER
> Control Panel
> Desktop
> NULL
>
> .... as per this documentation by Microsoft ...
>
> https://learn.microsoft.com/en-us/windows/win32/winmsg/wm-settingchange
>
> .... but none made any difference, the setting remained stubbornly enabled.

As soon as I included a function call ...

Send-SettingChange;

.... all hell broke loose, but eventually I was able to remove all the
errors except one (ignore the line number, because by then other things
had changed from the above):

Cannot convert argument "6", with value:
"System.Management.Automation.PSReference", for "SendMessageTimeout" to
type "
System.UIntPtr": "Cannot convert the
"System.Management.Automation.PSReference" value of type
"System.Management.Automa
tion.PSReference" to type "System.UIntPtr"."
At C:\Programs\Utilities\WMSettingsChange.ps1:40 char:42
+ [void] ([Win32.W32N]::SendMessageTimeout <<<< ($HWND_BROADCAST,
$WM_SETTINGCHANGE, [UIntPtr]::Zero, $SPI_SCREENSA
VESECURE, 2, 5000, [System.Management.Automation.PSReference] $result));
+ CategoryInfo : NotSpecified: (:) [], MethodException
+ FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument

Counting from 0, parameter 6 is actually the 7th, the optional return
value, yet confusingly the above error message mentions the timeout.
AFAICT, the message is describing that the attempted cast for the return
value is somehow illegal.

However, before discovering the omission of the function call, I'd been
trying other things, including extending the above script to call ...
SystemParametersInfo
.... to set the lock screen setting, and it turned out that this on its
own is sufficient to get the setting to change. Here's a powershell
script that finally works. It reads from the registry the value set by
the BATch file script and sets it into the live user profile (commented
out lines were used for debugging; $result = 1 means success):

Add-Type -Namespace Win32 -Name W32N -MemberDefinition @"
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern int SystemParametersInfo(
int uAction,
int uParam,
int lpvParam,
int fuWinIni
);
"@;

function Send-SettingChange {
# Set-PSDebug -Trace 1
$result = 0x0000;
$SPI_SCREENSAVESECURE = 0x0077;
$SPIF_SENDWININICHANGE = 0x0002;
$REG_SCREENSAVESECURE = (Get-ItemProperty -Path "HKCU:\Control
Panel\Desktop").ScreenSaverIsSecure;
# "ScreenSaverIsSecure = $REG_SCREENSAVESECURE";
$result = ([Win32.W32N]::SystemParametersInfo($SPI_SCREENSAVESECURE,
$REG_SCREENSAVESECURE, 0x0000, $SPIF_SENDWININICHANGE ));
# "SystemParametersInfo: Result = $result";
# Set-PSDebug -Trace 0
}

Send-SettingChange;

--

Fake news kills!

I may be contacted via the contact address given on my website:
www.macfh.co.uk

SubjectRepliesAuthor
o Setting screensaver from command-line

By: Java Jive on Sun, 15 Oct 2023

8Java Jive
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor