For 64 bit windows, use:
ctypes.windll.user32.SystemParametersInfoW
for 32 bit windows, use:
ctypes.windll.user32.SystemParametersInfoA
If you use the wrong one, you will get a black screen. You can find out which version your using in Control Panel -> System and Security -> System.
You could also make your script choose the correct one:
import struct
import ctypes
PATH = 'C:\\Users\\Patrick\\Desktop\\0200200220.jpg'
SPI_SETDESKWALLPAPER = 20
def is_64bit_windows():
"""Check if 64 bit Windows OS"""
return struct.calcsize('P') * 8 == 64
def changeBG(path):
"""Change background depending on bit size"""
if is_64bit_windows():
ctypes.windll.user32.SystemParametersInfoW(SPI_SETDESKWALLPAPER, 0, PATH, 3)
else:
ctypes.windll.user32.SystemParametersInfoA(SPI_SETDESKWALLPAPER, 0, PATH, 3)
changeBG(PATH)
Update:
I've made an oversight with the above. As @Mark Tolonen demonstrated in the comments, it depends on ANSI and UNICODE path strings, not the OS type.
If you use the byte strings paths, such as b'C:\\Users\\Patrick\\Desktop\\0200200220.jpg'
, use:
ctypes.windll.user32.SystemParametersInfoA
Otherwise you can use this for normal unicode paths:
ctypes.windll.user32.SystemParametersInfoW
This is also highlighted better with argtypes in @Mark Tolonen's answer, and this other answer.