user1952084
user1952084

Reputation: 67

Change wallpaper in Python for user while being system

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.

Upvotes: 2

Views: 5169

Answers (1)

user1129665
user1129665

Reputation:

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)

Upvotes: 4

Related Questions