假设c直接跟在b后面,事实并非如此。编译器将在该结构中填充几个字节(x86上为2)以对齐c。在
正确的方法是在ctypes中声明结构的一对一映射:from ctypes import *
class object_t(Structure):
_fields_ = [
('a', c_uint8),
('b', c_uint8),
('c', c_uint32),
('d', POINTER(c_uint8)),
]
不,您可以获得任何成员的值,而不是这种类型。在
C示例库:
^{pr2}$
从Python中使用它:from ctypes import *
class object_t(Structure):
_fields_ = [
('a', c_uint8),
('b', c_uint8),
('c', c_uint32),
('d', POINTER(c_uint8)),
]
callback_t = CFUNCTYPE(None, POINTER(object_t))
lib = CDLL('./file.dll')
func1 = lib.func1
func1.argtypes = None
func1.restype = POINTER(object_t)
func2 = lib.func2
func2.argtypes = [callback_t]
func2.restype = None
ret = func1()
a = ret.contents.a
b = ret.contents.b
c = ret.contents.c
d = ret.contents.d
def mycallback(obj):
a = obj.contents.a
b = obj.contents.b
c = obj.contents.c
d = obj.contents.d
func2(callback_t(mycallback))