我正在尝试通过GetFileVersionInfoSizeW和VerQueryValueW获取文件的版本。我把版本的一部分打印出来了,但没有全部打印出来。它在文件版本的每个字符之间也有一些奇怪的空格。有人知道它出了什么问题吗?
我猜测这与python3的Unicode解释有关,因为我必须更改原始的VerQueryValueA和python2 (https://stackoverflow.com/a/38924793/7144869)中正常运行的GetFileVersionInfoSizeA和VerQueryValueA的GetFileVersionInfoSizeW和VerQueryValueW。
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())发布于 2017-03-05 14:14:34
这些函数返回微软所谓的"Unicode“字符串,但它实际上是ctypes.wstring可以转换的编码UTF-16LE。l.value是UTF16字符的计数,而不是字节的计数,所以使用下面的代码来正确解码它。您不需要像现在这样对结果执行.decode()操作。
return wstring_at(r.value, l.value)https://stackoverflow.com/questions/42604493
复制相似问题