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

标签 python windows unicode ctypes 32bit-64bit

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

在我编写的 Python 脚本中,我遇到了关于何时使用这些变体的两种不同解释。 This answer to a question说对于 64 位机器,您必须使用 SystemParametersInfoW,而对于 32 位机器,您必须使用 SystemParametersInfoA,因此您应该运行一个函数来确定脚本在哪个位机器上运行。然而,another answer here (而且我看到更多人提倡这种类型的答案)和 here说 SystemParametersInfoW 必须 与 Python 3.x 一起使用,因为它传递一个 Unicode 字符串,而 SystemParametersInfoA 用于 Python 2.x 及以下版本,因为它传递一个有利于 ANSI 的字节字符串。

那么这里的正确答案是什么,因为我需要以不同的方式继续我的脚本?同样,我使用的是 Python 3.5,所以第二个答案适合是有道理的,但是机器的位是否是使用 SystemParametersInfoW 和 SystemParametersInfoA 之间的一个因素?它是两个答案的混合,还是我应该继续使用 SystemParametersInfoW 而不管它是在 32 位还是 64 位机器上使用?我什至需要确定运行脚本的机器的位吗?感谢您帮助澄清这个问题!

最佳答案

在内部,Windows 使用 Unicode。 SystemParametersInfoA 函数将 ANSI 参数字符串转换为 Unicode 并在内部调用 SystemParametersInfoW。无论是 32 位还是 64 位,您都可以在 Python 2.x 或 3.x 中从 Python 调用。通常您希望 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

关于Python - Windows SystemParametersInfoW 与 SystemParametersInfoA 函数之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44867820/

相关文章:

python - 来自 x、y 值的 Matplotlib 直方图,日期时间月份作为 bin

c# - Visual C# 2010 速成版 : Same Form But Different Size on Win7 and WinXP

unicode - Demotic 脚本是否以 Unicode 表示?

python - 如何在 python 中只比较日期(而不是时间)

python - 我在 for 循环和 return 语句中一直犯的一个错误

python - 返回 aws lambda 中的变量

python - Windows 等效于此 Makefile

windows - 在 Windows 上使用 git 时出现警告和 fatal error ?

java - 对于超过 3.0 的 Unicode 版本,如何将 Java 字符串转换为 xml 实体?

c++ - 如何测试字符串是否具有特定的 unicode 字符?