ice94 has asked for the wisdom of the Perl Monks concerning the following question:

Hello to you, enlightened monks. First of all, the technical environment : Consider the following snippet :
use strict; use warnings; use Win32::API; use Win32::TieRegistry; sub setWPUsingAPI { my($pic) = @_; my $SPI_SETDESKWALLPAPER = 20; my $SPIF_UPDATEANDSENDINI = 3; my $syspinf = Win32::API->new('user32','SystemParametersInfo', 'II +PI', 'I') or die "Could not import function SystemParametersInfo.\n"; $syspinf->Call($SPI_SETDESKWALLPAPER, 0, $pic, $SPIF_UPDATEANDSEND +INI); } sub setWPUsingRegistry { my($pic) = @_; $Registry->Delimiter('/'); $Registry->{"HKEY_CURRENT_USER/Control Panel/Desktop//TileWallpape +r"} = 1; $Registry->{"HKEY_CURRENT_USER/Control Panel/Desktop//WallpaperSty +le"} = 1; $Registry->{"HKEY_CURRENT_USER/Control Panel/Desktop//Wallpaper"} += $pic; } my $wallpaper = $ARGV[0]; setWPUsingAPI($wallpaper); setWPUsingRegistry($wallpaper);
The previous script (called test.pl) is called through a scheduled task w/ the following options :

When running the scheduled task, a taskeng.exe pops up briefly and the wallpaper is displayed correctly w/ the registry keys TileWallpaper, WallpaperStyle and Wallpaper all correctly set.

But if I change the run mode to Run whether the user is logged on or not to avoid any pop up, the registry keys are correctly set according to the setWPUsingRegistry subroutine but the setWPUsingAPI subroutine is not applied.

I thought there was an issue w/ the account running the scheduled task in Run whether the user is logged on or not mode (which is AFAIK SYSTEM) but I don't know how to set up the task to circumvent this issue (maybe a runas but then, what would be the point of the Run w/ highest privileges option ?).

I even wrapped the whole code in a batch file but to no avail.

Do you wise monks see where the problem could be ? Thanks in advance.

Replies are listed 'Best First'.
Re: Trigger refresh after changing wallpaper when called from scheduled task
by silent11 (Vicar) on Mar 27, 2015 at 16:47 UTC
    I had the below code running daily with no issues. I'm not sure what "refresh" you're referring to, or why you have to play with the registry. I ran this from a scheduled task or by hand and the background would refresh instantly

    Usage:
    perl wallpaper.pl bg.jpg
    wallpaper.pl:
    use Win32::API; use constant SPI_SETDESKWALLPAPER => 20; use constant SPIF_UPDATEANDSENDINI => 3; use constant NULL => 0; my $syspinf = Win32::API->new('user32','SystemParametersInfo', [I,I,P, +I], I) or die "Could not import function.\n"; $syspinf->Call(SPI_SETDESKWALLPAPER, 0, $ARGV[0], SPIF_UPDATEANDSENDIN +I);
    This script was part of a larger series of scripts that would pull down RSS data, pull the next 2 days of meetings from outlook, then overlay it at specific coordinates (with GD) on top of my background image... then I'd run the following where bg.jpg was the dynamically generated image.

    Update: As for suppressing the cmd window... my script ran at 5am so I never saw it, but the .bat that the scripts are wrapped in begins with @echo off. Also, I thought there was an option in windows task scheduler to run as "Hidden".

Re: Trigger refresh after changing wallpaper when called from scheduled task
by dasgar (Priest) on Mar 27, 2015 at 18:53 UTC

    If you don't want to see the console pop up when the script runs, you might find useful information at How to hide/inhibit the console window when launching a Perl script on Windows?. First suggestion listed about leverage Win32::GUI is what I was thinking of.

    One note of caution about having script that's trying to do something with the wallpaper and have it set for "Run whether the user is logged on or not". If no user is logged in, there is no GUI environment. (Learned this the hard way some time ago when I was trying to automate GUI based programs.) So if Task Scheduler tries to run this type of script with no user logged in, you might not get the results that you expect.

Re: Trigger refresh after changing wallpaper when called from scheduled task
by ice94 (Novice) on Mar 28, 2015 at 13:34 UTC

    I ended running the scheduled task in Run only when user is logged on mode but w/ wperl as an interpreter instead of good old perl.

    The code is launched, the wallpaper is set and the registry entries modified w/o a single command window poping up.

    BTW, the registry editing is to activate tiling for the wallpaper, mandatory in case of a composed wallpaper on multiple monitors setup.

    Thank you all for you help, especially the info about the Win32::GUI module.