简体   繁体   English

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

[英]In Python using ctypes for passing pointer to struct pointer to C function

In a C library there's:在 C 库中有:

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

and using ctypes I need the Python equivalent to:并使用 ctypes 我需要等效于的 Python:

B b;
create_b(&b)

The struct A is implemented in Python as class A(ctypes.Structure) . struct A 在 Python 中实现为class A(ctypes.Structure)

So, I tried:所以,我试过:

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

but it's not working.但它不起作用。 There are many similar questions, but none I tried helped.有很多类似的问题,但我试过的都没有帮助。

This line这条线

b = ctypes.POINTER(A)

is just setting b to refer to the same type ctypes.POINTER(A) , where what is really wanted is a variable constructed with that type.只是将b设置为引用相同类型的ctypes.POINTER(A) ,其中真正想要的是用该类型构造的变量。 The line is missing the parentheses at the end:该行在末尾缺少括号:

b = ctypes.POINTER(A)()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM