返回数组,ctypes

时间:2014-06-19 11:51:51

标签: python c++ ctypes

我是一个使用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 个答案:

答案 0 :(得分:-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了解详情