Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why won't this ctypes code work with Python 3.3 but will work with Python 2.7?

So I'm trying to make a Python 3.3 program to change the Windows desktop background using the ctypes module. I've tested the following code in Python 2.7, and it worked perfectly. But it just won't work with Python 3.3! I'm using Windows 7. Here's the code:

import ctypes
SPI_SETDESKTOPWALLPAPER=20
ctypes.windll.user32.SystemParametersInfoA(SPI_SETDESKTOPWALLPAPER, 0,"C:/Users/Public/Pictures/Sample Pictures/Penguins.jpg", 3)
like image 990
Luke Dinkler Avatar asked Dec 31 '14 20:12

Luke Dinkler


1 Answers

SystemParametersInfoA requires a 8-bit ANSI encoded input string as a parameter, which is known as mbcs encoding in Python.

You will have to use SystemParametersInfoW in python3. This is because SystemParametersInfoW takes in a UTF-16 wide string (which is wchar_t * in C) and the ctypes library automatically converts this passed unicode argument into c_wchar_p.

Refer the documentation for more details.

like image 79
Bhargav Rao Avatar answered Sep 27 '22 21:09

Bhargav Rao