Python-Windows SystemParametersInfoW与SystemParametersInfoA函数之间的区别

J. *_*aro 1 windows unicode 32bit-64bit python-3.x

尽管我对Stack Overflow及其他方面进行了研究,但我有一个似乎无法澄清的快速问题。我的问题涉及Windows SystemParametersInfo函数及其与Python 3.x脚本相关的变体SystemParametersInfoW(Unicode)和SystemParametersInfoA(ANSI)。

在我编写的Python脚本中,我遇到了两种不同的解释来解释何时使用这些变体。这个问题的答案说,对于64位计算机,您必须使用SystemParametersInfoW,而对于32位计算机,您必须使用SystemParametersInfoA,因此您应该运行一个函数来确定脚本在哪台位计算机上运行。但是,这里还有另一个答案(我已经看到更多的人拥护这种答案),这里说SystemParametersInfoW 必须与Python 3.x一起使用,因为它传递Unicode字符串,而SystemParametersInfoA用于Python 2.x及以下版本。因为它传递了一个有利于ANSI的字节字符串。

那么,这里的正确答案是什么,因为我需要对脚本进行不同的处理?同样,我使用的是Python 3.5,因此第二个答案很合适,但是在使用SystemParametersInfoW和SystemParametersInfoA之间,机器的某些事实是否是一个因素?是这两个答案的混合,还是我应该继续使用SystemParametersInfoW,而不管它是否将在32位或64位计算机上使用?我什至需要确定运行脚本的计算机的哪个位?感谢您为澄清此问题所提供的帮助!

Mar*_*nen 5

Windows内部使用Unicode。该SystemParametersInfoA函数将ANSI参数字符串转换为Unicode并在内部进行调用SystemParametersInfoW。您可以在Python 2.x或3.x中从Python调用32位或64位。通常,由于Windows内部是Unicode,因此您通常希望W版本传递和检索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)
Run Code Online (Sandbox Code Playgroud)

输出(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
Run Code Online (Sandbox Code Playgroud)