我正在尝试用python ctypes
执行windll.version.GetFileVersionInfoSizeA()
。我正在执行以下代码:
_GetFileVersionInfoSizeA = ctypes.windll.version.GetFileVersionInfoSizeA
_GetFileVersionInfoSizeA.argtypes = [ctypes.c_char_p, ctypes.c_void_p]
_GetFileVersionInfoSizeA.restype = ctypes.c_uint32
_GetFileVersionInfoSizeA.errcheck = RaiseIfZero # RaiseIfZero is a function to raise error
# lptstrFilename is the file path
dwLen = _GetFileVersionInfoSizeA(lptstrFilename, None)
这段代码在Python2中运行得非常好,但在Python3.8中不起作用。它给出以下错误:
argument 1: <class 'TypeError'>: wrong type
根据msdn docfor GetFileVersionInfoSizeA
,第二个参数应该是:
"A pointer to a variable that the function sets to zero."
我尝试了下面的代码,但它给出了与以前相同的错误。你知道吗
dwLen = _GetFileVersionInfoSizeA(lptstrFilename, LPVOID)
我不知道我错过了什么。
注意-这是我第一次使用ctypes
。你知道吗
在Python中,3个字符串默认为Unicode。即使在Python2中,使用Unicode字符串也是最好的,因此Windows API的W版本也是Unicode。所以要严格按照文档调用API:
注意,第二个参数没有被记录为接受nullptr(Python中的a.k.a
None
),因此它应该是一个有效的引用。你知道吗要调用函数的ANSI(A)版本,请传递一个以默认ANSI编码正确编码的字节字符串,例如
'test.exe'.encode('ansi')
,但请注意,非ASCII文件名会引起麻烦,使用Unicode(W)版本可以减轻这种麻烦。你知道吗在python2中,可以使用两种类型来表示字符串。字符串和Unicode字符串。因此
c_char_p
ctypes类型用于表示python2字符串类型,c_wchar_p
ctypes类型用于表示python2 Unicode字符串类型。你知道吗但是在python3中只有一种字符串类型。因此,
c_wchar_p
ctypes类型用于表示python3string
类型,c_char_p
ctypes类型用于表示python3bytes
类型。你知道吗您可以在^{} 和^{} 文档中找到基本的数据类型。你知道吗
所以你可以
相关问题 更多 >
编程相关推荐