【发布时间】:2020-12-14 18:08:27
【问题描述】:
我在 C 中有以下结构和函数声明:
typedef int (*callback)(struct instance *);
typedef struct instance {
int x;
callback f;
} instance;
在 Python 中使用 ctypes 定义回调的正确方法是什么?
我正在尝试通过以下方式在 Python 中声明结构:
class INSTANCE_STRUCT(ctypes.Structure):
_fields_ = [("x", c_int),
("f", c_void_p)]
所以基本上我使用c_void_p 将f 声明为空指针,并希望将其转换为函数。
我正在使用 malloc 在堆上的 C 源代码中创建结构,然后在 Python 中按如下方式访问它:
instance = ctypes.cast(pointer_to_structure, ctypes.POINTER(INSTANCE_STRUCT))
print(instance.contents.x)
print(instance.contents.f)
运行脚本会给我以下输出:
Initializing struct x=[4] with function f() result=[8] // this happens in C and is correct
4 // value of instance.x
140027207110960 // address of instance.f (?)
现在有了instance.f() 的地址,我想我需要以某种方式将其转换为python 方法。我试过这个:
def CALLBACK_FUNC(self, structure):
pass
callback = ctypes.cast(instance.contents.f, ctypes.POINTER(CALLBACK_FUNC))
但它只是抛出错误:
Traceback (most recent call last):
File "binding_test.py", line 19, in <module>
callback = ctypes.cast(instance.contents.f, ctypes.POINTER(callback_function))
TypeError: must be a ctypes type
考虑到回调函数应该将 INSTANCE_STRUCT 对象本身作为参数,有人知道在 Python 中取消引用 instance.f() 函数的方法是什么吗?
【问题讨论】: