2

what I am trying to do is change the desktop wallpaper in windows. To do that, I use the following code:

import ctypes
import Image

pathToBmp = "PATH TO BMP FILE"
SPI_SETDESKWALLPAPER = 20  
ctypes.windll.user32.SystemParametersInfoA(SPI_SETDESKWALLPAPER, 0, pathToBmp , 0) 

this works when I run the .py file, this works when I convert it using py2exe and run the exe under the current user, but when I run the exe as SYSTEM, the current user background does not change.

This ofcourse was to be expected. But I don't know how to solve it.

By the way, it does not matter if any of your solutions changes the current user background or all the users' backgrounds.

Thank you for your time.

user1952084
  • 67
  • 2
  • 9
  • You shouldn't use `from ... import *`, it is not a good style for coding as while debugging etc. one needs to search the modules if a variable is not defined in the code.. – pradyunsg Jan 20 '13 at 16:26
  • @paddila I changed the code so I don't use from ... import * anymore. It still has the same outcome. – user1952084 Jan 20 '13 at 16:39
  • I provided answer with example for similar SO question. [change desktop background](http://stackoverflow.com/questions/1977694/change-desktop-background/37669111#37669111) – Vlad Bezden Jun 07 '16 at 13:41

1 Answers1

4

How about creating a value key in the registry at:

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run

This will change the background when ever the user login.

To try it, write this script, name it for example SetDesktopBackground.py, any where you like:

#!python

from ctypes import *
from os import path

SPI_SETDESKWALLPAPER = 0x14
SPIF_UPDATEINIFILE   = 0x1

lpszImage = path.join(path.dirname(path.realpath(__file__)), 'your_image.jpg')

SystemParametersInfo = windll.user32.SystemParametersInfoA

SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, lpszImage, SPIF_UPDATEINIFILE)

Dont forgot to put some image, your_image.jpg, in the same directory. Then open the registery editor:

Start > Search > type regedit.exe

Then go to the path:

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run

Right click and choose New > String Value and type any name you like for this value.

Right click on this new value and choose Modify, in the Data Value field write:

"C:\Python26\pythonw.exe" "C:\Path\To\SetDesktopBackground.py"

To test it, logout and login again. The background should change when ever this user login.

That was the manual way to do it, you can use _winreg in your application to create the value during the installation:

#!python

from _winreg import *
from sys import executable
from os import path

subkey  = 'Software\\Microsoft\\Windows\\CurrentVersion\\Run'
script  = 'C:\\Path\\To\\SetDesktopBackground.py'
pythonw = path.join(path.dirname(executable), 'pythonw.exe')

hKey = OpenKey(HKEY_CURRENT_USER, subkey, 0, KEY_SET_VALUE)

SetValueEx(hKey, 'MyApp', 0, REG_SZ, '"{0}" "{1}"'.format(pythonw, script))

CloseKey(hKey)
  • 1
    `__file__` won't work in py2exe generated executable. `sys.argv[0]` could be used instead. – jfs Apr 07 '14 at 09:14