1

I have a Python script that utilizes the ctypes library to pass parameters to Window's SystemParametersInfoW() function in order to change my Windows 10 desktop background. I'm able to successfully change the background, however the changes aren't being applied permanently. Once I shut the computer down it'll revert to my previous background on the next boot instead of the one applied from the script.

image_path = "C:\\Users\\Jay\\source\\repos\\Nasa_Background\\Nasa_Background\\bg.png"
SPI_SETDESKWALLPAPER = 0x0014
SPIF_UPDATEINIFILE = 0x0001
SPIF_SENDWININICHANGE = 0x0002

ctypes.windll.user32.SystemParametersInfoW.argtypes = [ctypes.c_uint, ctypes.c_uint, ctypes.c_void_p, ctypes.c_uint]
ctypes.windll.user32.SystemParametersInfoW.restype = ctypes.wintypes.BOOL
ctypes.windll.user32.SystemParametersInfoW(SPI_SETDESKWALLPAPER, 0, image_path, SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE)

According to this dev blog passing SPIF_UPDATEINIFILE should make the change permanent: https://devblogs.microsoft.com/oldnewthing/20160721-00/?p=93925

Everything else looks correct based on the docs: https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-systemparametersinfow

I've also tried this solution with no luck: How do I set the desktop background in python? (windows)

So I'm not sure what I'm missing here, it seems like it should permanently update the background but it's not.

martineau
  • 119,623
  • 25
  • 170
  • 301
user3605767
  • 75
  • 1
  • 7
  • Try [`IDesktopWallpaper::SetWallpaper`](https://learn.microsoft.com/en-us/windows/win32/api/shobjidl_core/nf-shobjidl_core-idesktopwallpaper-setwallpaper) instead. – IInspectable Apr 22 '22 at 20:39
  • That old API might only support bmp files? – Anders Apr 22 '22 at 23:31
  • Works for me (*.jpg* file). – CristiFati Apr 23 '22 at 00:14
  • Unfortunately, I wasn't able to get it to work with bmp or jpg. I'm not sure if this is related to my issue but whenever I pass `SPIF_SENDWININICHANGE` the script won't work from Windows Task Manager. It still works if I run it from VS, but within Task Manager it won't set the wallpaper unless `SPIF_SENDWININICHANGE` is removed. Even then, it'll set it but won't save it permanently. – user3605767 Apr 23 '22 at 01:22
  • Run from task manager, meaning elevated? Are you in the admin group? – Anders Apr 23 '22 at 12:17
  • I was able to run it from a *Cmd* window (*${PATH\_TO\_PYTHON} ${FILE}.py*) as a dummy user (with default privileges). – CristiFati Apr 23 '22 at 13:09
  • @Anders sorry I meant to say Task Scheduler, not Manager. But I was able to figure out what the issue was. It's because I was running the script from within a virtual environment. I didn't think that would be an issue since I always viewed venv's as something that only isolated Python, while everything else remained accessible per usual. But I guess the windowsini file could not be updated properly from within a venv. Once I ran the script from my global environment it worked fine. At least I'm assuming that's what it was. Does this sound right? Python version in venv was 3.7 vs 3.8 global. – user3605767 Apr 23 '22 at 22:34
  • Sounds about right yes. SystemParametersInfo is very old, it might even rely on the automatic .ini to registry mapping when writing. – Anders Apr 23 '22 at 23:40

0 Answers0