我有一个这样的 C 结构:
typedef struct {
my_domain_type_t type;
my_domain_union_t u;
my_domain_int32_list_t list;
} my_domain_value_t;
typedef struct {
int32_t min;
int32_t max;
} my_domain_int32_range_t;
我想从 ctypes 调用的 C 函数:
int64_t myData::get_min(const my_domain_value_t &value)
{
int min_value = 0;
my_domain_type_t dt = value.type;
if (dt == 0)
{
my_domain_int32_range_t range = value.u.range;
min_value = range.min;
printf("min_value=%d\n", min_value);
}
return min_value;
}
ctypes 定义:
class myDomainInt32RangeT(Structure):
_fields_ = [ ('min', c_long),
('max', c_long) ]
class myDomainUnionT(Union):
_fields_ = [ ('range', myDomainInt32RangeT ) ]
class myDomainValueT(Structure):
_fields_ = [ ('type', c_int ),
('u', myDomainUnionT ),
('list', myDomainInt32ListT ) ]
class myData(object):
def __init__(self):
self.object = myDataX.myData_new()
def get_min(self, arg1):
myDataX.myData_get_min.argtypes = [ c_void_p, POINTER(myDomainValueT) ]
myDataX.myData_get_min.restype = c_longlong
return myDataX.mydata_get_min(self.object, arg1)
Python代码:
mydataY = myData()
domainRange = myDomainInt32RangeT()
domainRange.min = c_long(3)
domainRange.max = c_long(5)
domainUnion = myDomainUnionT()
domainUnion.range = domainRange
domainValue = myDomainValueT()
domainValue.type = 0
domainValue.u = domainUnion
domainValue.list = myDomainInt32ListT()
b = mydataY.get_min( byref(domainValue) )
print(b)
我期望 min_value 的值为 3,但我一直得到 0。C 代码也打印 0。看起来联合没有正确设置/传输。
我做错什么了 ?。
TIA,
约翰