[英]using c structures from a shared library with ctypes in Python
我做了很多研究,没有发现任何问题……我是Python和Ctypes的新手,我正试图从共享库中调用函数。 到目前为止,还不错,但是这些函数将来自.so内部定义的结构的参数特定数据类型作为参数
我的问题是,我已经看过如何在Python中声明“类结构”的示例,但这就是我在.so中的内容
typedef struct noPDDE
{
void *x;
struct noPDDE *y;
struct noPDDE *z;
}NoPDDE,*pNoPDDE;
typedef struct PDDE
{
int tam;
pNoPDDE sup;
}PDDE;
我不知道如何将PDDE指针传递给函数。
任何帮助都是有用的。 非常感谢。
这是在ctypes中声明递归结构的方式:
from ctypes import (
Structure,
c_void_p,
POINTER,
c_int,
byref,
)
class noPDDE(Structure):
pass
noPDDE._fields_ = [
("x", c_void_p),
("y", POINTER(noPDDE)),
("z", POINTER(noPDDE)),
]
class PDDE(Structure):
_fields_ = [
("tam", c_int),
("sup", POINTER(noPDDE)),
]
foo = PDDE()
mylib.func_that_takes_pointer_to_pdde(byref(foo))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.