在Python中对ctypes使用共享库中的c结构

2024-04-19 04:21:39 发布

您现在位置: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指针传递给函数。在

任何帮助都是有用的。谢谢。在


Tags: 函数声明参数so定义ctypes结构struct
1条回答
网友
1楼 · 发布于 2024-04-19 04:21:39

这是在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))

相关问题 更多 >