在Python中使用结构体中的函数指针调用函数

1 投票
1 回答
706 浏览
提问于 2025-04-17 20:37

我有一个结构体,内容如下:

class Exprs(Structure):
  _fields_ = [("func",POINTER(CMPFUNC)),
           other members]

CMPFUNC= CFUNCTYPE(ExprArg, POINTER(Exprs), ExprArgList,c_int)// Prototype

这里的 e 是 POINTER(Exprs) 类型的。

当我尝试调用 e.contents.func(e, eArgList,0) 时,出现了一个错误:

LP_CfunctionType 对象不可调用

我已经使用了合适的 ctypes 类型转换。请帮我解决这个错误。

1 个回答

0

函数原型类型的构造函数直接接受一个地址。你可能只需要在结构定义中去掉 POINTER 这个说明符。你写的内容其实是等价于:

struct Exprs {
    ExprArg (**func)(Exprs *, ExprArgList, int);
};

如果这正是你想要的,那么可以使用 e.contents.func.contents(e, eArgList,0)

撰写回答