【发布时间】:2010-11-24 16:35:39
【问题描述】:
我有一个包含 C 函数的 DLL,其原型如下:
int c_read_block(uint32 addr, uint32 *buf, uint32 num);
我想使用 ctypes 从 Python 调用它。该函数需要一个指向一块内存的指针,它将结果写入其中。我不知道如何构造和传递这么一大块内存。 ctypes 文档帮助不大。
构造一个数组并将其传递给“byref”,如下所示:
cresult = (c_ulong * num)() err = self.c_read_block(addr, byref(cresult), num)给出这个错误信息:
ArgumentError: argument 3: <type 'exceptions.TypeError'>: expected LP_c_ulong instance instead of pointer to c_ulong_Array_2
我猜这是因为 Python ulong 数组与 c uint32 数组完全不同。我应该使用create_char_string。如果是这样,我如何说服 Python 将该缓冲区“转换”为 LP_c_ulong?
【问题讨论】:
标签: python arrays pointers ctypes