Python - Windows SystemParametersInfoW 与 SystemParametersInfoA 函数之间的区别 [英] Python - Difference Between Windows SystemParametersInfoW vs SystemParametersInfoA Function

查看:23
本文介绍了Python - Windows SystemParametersInfoW 与 SystemParametersInfoA 函数之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a quick question that I cannot seem to clarify, despite my research on Stack Overflow and beyond. My questions involves the Windows SystemParametersInfo function with its variants SystemParametersInfoW (Unicode) and SystemParametersInfoA (ANSI) in relation to a Python 3.x script.

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

In a Python script I am writing, I came across two different explanations into when to use these variants. This answer to a question says that for 64-bit machines you must use SystemParametersInfoW while for 32-bit machines you must use SystemParametersInfoA, thus you should run a function to determine which bit machine the script is running on. However, another answer here (and I've seen more people advocate for this type of answer) and here says that SystemParametersInfoW must be used with Python 3.x since it passes a Unicode string while SystemParametersInfoA is used for Python 2.x and below since it passes a byte string conducive with ANSI.

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

So what is the right answer here as I would need to proceed forward differently with my script? Again, I am using Python 3.5 so it would make sense that the second answer fits, however is there any truth in the bit of the machine being a factor between using SystemParametersInfoW and SystemParametersInfoA? Is it a mixture of both answers or should I go ahead and use SystemParametersInfoW regardless of whether it will be used on a 32 or 64 bit machine? Do I even need to determine the bit of the machine the script is running on? Thank you for your help in clarifying this issue!

推荐答案

在内部,Windows 使用 Unicode.SystemParametersInfoA 函数将 ANSI 参数字符串转换为 Unicode 并在内部调用 SystemParametersInfoW.您可以在 Python 2.x 或 3.x 中从 32 位或 64 位 Python 调用.通常您希望 W 版本传递和检索 Unicode 字符串,因为 Windows 内部是 Unicode.A 版本可能会丢失信息.

Internally, Windows uses Unicode. The SystemParametersInfoA function converts ANSI parameter strings to Unicode and internally calls SystemParametersInfoW. You can call either from Python whether 32- or 64-bit, in Python 2.x or 3.x. Usually you want the W version to pass and retrieve Unicode strings since Windows is internally Unicode. The A version can lose information.

适用于 Python 2 或 3、32 位或 64 位的示例.注意,W 版本返回缓冲区中的 Unicode 字符串,而 A 版本返回一个字节字符串.

Example that works in Python 2 or 3, 32- or 64-bit. Note that the W version returns a Unicode string in the buffer, while the A version returns a byte string.

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 位):

Output (Python 2.X 32-bit and Python 3.X 64-bit):

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:windowswebwallpaper	heme1img1.jpg
c:windowswebwallpaper	heme1img1.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:windowswebwallpaper	heme1img1.jpg

这篇关于Python - Windows SystemParametersInfoW 与 SystemParametersInfoA 函数之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆