Windows 在内部使用 Unicode。这SystemParametersInfoA
函数将 ANSI 参数字符串转换为 Unicode 并内部调用SystemParametersInfoW
。您可以从 Python 调用,无论是 32 位还是 64 位、Python 2.x 或 3.x。通常您希望 W 版本传递和检索 Unicode 字符串,因为 Windows 内部是 Unicode。 A 版本可能会丢失信息。
适用于 Python 2 或 3、32 位或 64 位的示例。请注意,W 版本在缓冲区中返回 Unicode 字符串,而 A 版本返回字节字符串。
from __future__ import print_function
from ctypes import *
import sys
print(sys.version)
SPI_GETDESKWALLPAPER = 0x0073
dll = WinDLL('user32')
buf = create_string_buffer(200)
ubuf = create_unicode_buffer(200)
if dll.SystemParametersInfoA(SPI_GETDESKWALLPAPER,200,buf,0):
print(buf.value)
if dll.SystemParametersInfoW(SPI_GETDESKWALLPAPER,200,ubuf,0):
print(ubuf.value)
输出(Python 2.X 32 位和 Python 3.X 64 位):
C:\>py -2 test.py
2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 20:42:59) [MSC v.1500 32 bit (Intel)]
c:\windows\web\wallpaper\theme1\img1.jpg
c:\windows\web\wallpaper\theme1\img1.jpg
C:\>py -3 test.py
3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 18:41:36) [MSC v.1900 64 bit (AMD64)]
b'c:\\windows\\web\\wallpaper\\theme1\\img1.jpg'
c:\windows\web\wallpaper\theme1\img1.jpg