[英]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.