相关疑难解决方法(0)

用内省打印ctypes"Structure"的所有字段

test.c的:

#include <stdio.h>
#include <stdlib.h>

struct s {
    char a;
    int b;
    float c;
    double d;
};

struct s *create_struct()
{
    struct s *res = malloc(sizeof(struct s));
    res->a = 1; res->b = 2; res->c = 3.0f; res->d = 4.0;
    return res;
}
Run Code Online (Sandbox Code Playgroud)

test.py:

from ctypes import *

class S(Structure):
    _fields_ = [
        ('a', c_byte),
        ('b', c_int),
        ('c', c_float),
        ('d', c_double)
    ]

lib = CDLL('./test.so')

create_struct = lib.create_struct
create_struct.restype = POINTER(S)
create_struct.argtypes = []

s_ptr = create_struct()
s = s_ptr.contents

print …
Run Code Online (Sandbox Code Playgroud)

python reflection ctypes

11
推荐指数
2
解决办法
1万
查看次数

标签 统计

ctypes ×1

python ×1

reflection ×1