使用ctypes打印结构的内容

时间:2018-07-27 08:01:32

标签: python ctypes

我正在尝试打印从c库作为指针返回的结构。 我无法获取内容。我想念什么吗?

test.c:

typedef struct test_s {
    int a;
    char b;
} test_t;

test_t t =  {
    .a = 100,
    .b = 200,
};

test_t* get_struct() {
    return &t;
}

test.py

import sys
import ctypes

from ctypes import *

lib = CDLL('./libtest.so')
class test(Structure):
    __fields__ = [("a", c_int), ("b", c_int)]

get_struct_func = lib.get_struct
get_struct_func.atgtypes = []
get_struct_func.restype = POINTER(test)

s = get_struct_func()
print(s.contents.a)


Traceback (most recent call last):
  File "test.py", line 16, in <module>
    print(s.contents.a)
AttributeError: 'test' object has no attribute 'a'

0 个答案:

没有答案
相关问题