在 Python 中,使用 ctypes 将指针传递给指向 C 函数的结构指针

In Python using ctypes for passing pointer to struct pointer to C function

C 库中有:

typedef struct A *  B;
int create_b(B* b);

并使用 ctypes 我需要 Python 等同于:

B b;
create_b(&b)

结构 A 在 Python 中实现为 class A(ctypes.Structure)

所以,我尝试了:

b = ctypes.POINTER(A)
lib.create_b(ctypes.byref(b))

但它不起作用。类似的问题还有很多,不过none我试过了有帮助。

这一行

b = ctypes.POINTER(A)

只是将 b 设置为引用同一类型 ctypes.POINTER(A),真正需要的是用该类型构造的变量。该行末尾缺少括号:

b = ctypes.POINTER(A)()