首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >VerQueryValueW问题python 3

VerQueryValueW问题python 3
EN

Stack Overflow用户
提问于 2017-03-05 12:21:14
回答 1查看 274关注 0票数 0

我正在尝试通过GetFileVersionInfoSizeW和VerQueryValueW获取文件的版本。我把版本的一部分打印出来了,但没有全部打印出来。它在文件版本的每个字符之间也有一些奇怪的空格。有人知道它出了什么问题吗?

我猜测这与python3的Unicode解释有关,因为我必须更改原始的VerQueryValueA和python2 (https://stackoverflow.com/a/38924793/7144869)中正常运行的GetFileVersionInfoSizeA和VerQueryValueA的GetFileVersionInfoSizeW和VerQueryValueW。

代码语言:javascript
复制
import array
from ctypes import *

def get_file_info(filename):
"""
Extract information from a file.
"""
# Get size needed for buffer (0 if no info)
size = windll.version.GetFileVersionInfoSizeW(filename, None)
# If no info in file -> empty string
if not size:
    return 'Failed'
# Create buffer
res = create_string_buffer(size)
# Load file informations into buffer res
windll.version.GetFileVersionInfoW(filename, None, size, res)
r = c_uint()
l = c_uint()
# Look for codepages
windll.version.VerQueryValueW(res, '\\VarFileInfo\\Translation',
                              byref(r), byref(l))
# If no codepage -> empty string
if not l.value:
    return ''
# Take the first codepage (what else ?)
codepages = array.array('H', string_at(r.value, l.value))
codepage = tuple(codepages[:2].tolist())
# Extract information
windll.version.VerQueryValueW(res, ('\\StringFileInfo\\%04x%04x\\'
+ 'FileVersion') % codepage, byref(r), byref(l))
return string_at(r.value, l.value)

print (get_file_info(r'C:\WINDOWS\system32\calc.exe').decode())
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-03-05 14:14:34

这些函数返回微软所谓的"Unicode“字符串,但它实际上是ctypes.wstring可以转换的编码UTF-16LE。l.value是UTF16字符的计数,而不是字节的计数,所以使用下面的代码来正确解码它。您不需要像现在这样对结果执行.decode()操作。

代码语言:javascript
复制
return wstring_at(r.value, l.value)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42604493

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档