返回数组,ctypes

0 投票
1 回答
621 浏览
提问于 2025-04-18 10:21

我刚开始接触ctypes,还是个新手。我正在加载一个Windows的dll文件,想要读取GetDfuFileInfo函数返回的数组。其实我之所以用ctypes来加载这个文件,是因为我不知道怎么把dfu_file*这个指针传给GetDfuFileInfo函数。这真的能做到吗?

C++函数的使用示例:

dfu_file* TheFile = ReadDfuFile(const char*);
dfu_file_info* Info = GetDfuFileInfo(dfu_file*);

Python代码:

lib = ctypes.WinDLL('dfulib.dll')
func = lib.ReadDfuFile
func.restype = ctypes.c_void_p
func.argtypes = [ctypes.c_char_p]
func("file.dvu")

编辑

我还尝试了这个:

lib = ctypes.WinDLL('dfulib.dll')
func = lib.ReadDfuFile
func.restype = ctypes.c_void_p
func.argtypes = [ctypes.c_char_p]

FileInfo = lib.GetDfuFileInfo
FileInfo.argtypes = [ctypes.c_void_p]
FileInfo.restype = None

s = func(r'file.dvu')
s = ctypes.cast(s, ctypes.c_char_p)

print FileInfo(s.value)

结果出现了这个错误:

WindowsError: exception: access violation reading 0x00000016

任何帮助都非常感谢。

1 个回答

-1

示例代码如下,这段代码调用了来自kernel32.dll的 GetFileAttributesA 函数。

>>> from ctypes import *
>>> windll.LoadLibrary("kernel32.dll")
<WinDLL 'kernel32.dll', handle 77260000 at 2241ad0>
>>> windll.kernel32
<WinDLL 'kernel32', handle 77260000 at 225c2f0>
>>> windll.kernel32.GetFileAttributesA("c:/test/1.txt")
32

你可以参考 https://docs.python.org/2/library/ctypes.html 来获取更多详细信息。

撰写回答