ctypes用法

  • 封装c语言结构体:
​typedef struct metadata {
    int classes;
    char **names;
} metadata;

typedef struct strike {
  network *net;
  metadata names;
} strike;

​

对应python封装:

class METADATA(Structure):

    _fields_ = [("classes", c_int),

                ("names", POINTER(c_char_p))]

class STRIKE(Structure):

    _fields_ = [("net",POINTER(c_void_p)),

                ("data",METADATA)]

注意:在封装结构体时对应参数的顺序不可以随便改变,否则会出现异常。

  • 获取指针变量 
static unsigned char *data;    //设置全局限量
unsigned char *getdata()
{int size = 1024;
data = (unsigned char *)malloc(size);
....
return data;}
void free()
{free(data);}

对应ctypes封装 

handle.getdata.restype = POINTER(c_ubyte*size)
data = self.handle.getdata()
handle.free()

猜你喜欢

转载自blog.csdn.net/SecretGirl/article/details/121331185