您使用c_uint8,它是8位的,而您的结构使用int,在ctypesc_int中,通常是32位。在
你的结构应该是:class nestedStru(Structure):
_fields_ = [
("One", c_int),
("Two", c_int)
]
class mainStru(Structure):
_fields_ = [
("First", c_int),
("Poniter", POINTER(nestedStru))
]
这是一个测试库:
^{pr2}$
Python客户端:#!python
from ctypes import *
class nestedStru(Structure):
_fields_ = [
("One", c_int),
("Two", c_int)
]
class mainStru(Structure):
_fields_ = [
("First", c_int),
("Poniter", POINTER(nestedStru))
]
if __name__ == '__main__':
obj = mainStru()
obj.First = 0
obj.Poniter = (nestedStru * 3)((1, 11), (2, 22), (3, 33))
func = CDLL('./lib.dll').func
func.argtypes = [POINTER(mainStru), c_size_t]
func.restype = None
func(obj, 3)
现在它工作得很好:> gcc -Wall lib.c -o lib.dll -shared
> python file.py
1, 11
2, 22
3, 33
>