我有一个快速的问题,我似乎无法澄清,尽管我的研究堆栈溢出和超越。我的问题涉及windows systemparametersinfo函数及其变量systemparametersinfow(unicode)和systemparametersinfo a(ansi)与python 3.x脚本的关系。
在我正在编写的一个python脚本中,我在何时使用这些变体方面遇到了两种不同的解释。This answer to a question表示,对于64位计算机,必须使用systemparametersinfow;而对于32位计算机,必须使用systemparametersinfoa,因此应该运行一个函数来确定脚本在哪个位计算机上运行。然而,another answer here(我已经看到更多的人支持这种类型的答案)和here说systemparametersinfow必须与python 3.x一起使用,因为它传递unicode字符串,而systemparametersinfoa则用于python 2.x及更低版本,因为它传递有利于ansi的字节字符串。
那么在这里什么是正确的答案,因为我需要以不同的方式继续我的脚本?再说一次,我使用的是Python3.5,所以第二个答案是合适的,但是,在使用SystemParametersInfoW和SystemParametersInfoA之间,机器的位是否存在任何事实?这是两个答案的混合还是我应该继续使用systemparametersinfow,而不管它是在32位还是64位计算机上使用?我甚至需要确定运行脚本的机器的位吗?谢谢你帮助澄清这个问题!
最佳答案
在内部,windows使用unicode。SystemParametersInfoA
函数将ANSI参数字符串转换为Unicode并在内部调用SystemParametersInfoW
。在python 2.x或3.x中,您可以从python调用32位或64位,通常您希望w版本传递和检索unicode字符串,因为windows是内部unicode。A版本可能会丢失信息。
在Python2或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
关于windows - Python-Windows SystemParametersInfoW与SystemParametersInfoA函数之间的区别,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44867820/