我正在尝试将ctypes结构传递到DLL。问题是DLL从结构中读取的数据是垃圾。这不是我在ctypes中实现的第一个结构,但它比我所做的其他结构更复杂。在
结构定义如下:
typedef struct {
double L, x, y;
} CieLxy;
typedef struct {
int ledCur[3];
CieLxy targetCie, modelCie, measuredCie;
} I1VpCal;
typedef struct {
I1VpCal i1CalArray[8][7];
} I1CalArray;
具有以下测试功能:
^{pr2}$在Python中,我有以下ctypes定义:
class CieLxy(ctypes.Structure):
_fields_ = [("L", ctypes.c_double),
("x", ctypes.c_double),
("y", ctypes.c_double)]
class I1VpCal(ctypes.Structure):
_fields_ = [("ledCur", ctypes.c_int * 3),
("targetCie", CieLxy),
("modelCie", CieLxy),
("measuredCie", CieLxy)]
class I1CalArray(ctypes.Structure):
_fields_ = [("i1CalArray", 8*(7*I1VpCal))]
Foo = Dll['foo']
# Foo.argtypes = [ctypes.POINTER(I1CalArray)]
Foo.argtypes = [ctypes.c_void_p]
Foo.restype = ctypes.c_int
def foo( i1CalArray):
# p_i1CalArray = ctypes.POINTER(I1CalArray)
# Foo(p_I1CalArray.from_address(ctypes.addressof(i1CalArray)))
# Foo(i1CalArray)
Foo(ctypes.byref(i1CalArray))
当我运行以下代码时:
i1CalArray_struct = I1CalArray()
i1CalArray = i1CalArray_struct.i1CalArray
for i in range(8):
for j in range(7):
temp = i1CalArray[i][j].ledCur[0] = i+j
temp = i1CalArray[i][j].measuredCie.L = float(i+j)
foo(i1CalArray_struct)
它打印的数据是垃圾。它看起来更像是地址。在
in dll, i1CalArray[0][0].ledCur[0] = 2620124
in dll, i1CalArray[0][0].measuredCie.L = -166230287672847070000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000
in dll, i1CalArray[0][1].ledCur[0] = 31662056
in dll, i1CalArray[0][1].measuredCie.L = 0.000000
in dll, i1CalArray[0][2].ledCur[0] = 36890704
in dll, i1CalArray[0][2].measuredCie.L = 0.000000
in dll, i1CalArray[0][3].ledCur[0] = 488321588
in dll, i1CalArray[0][3].measuredCie.L = 0.000000
in dll, i1CalArray[0][4].ledCur[0] = 0
如果有任何帮助,我们将不胜感激。我读过很多帖子,尝试过不同的解决方案,但结果总是一样的。在
使用Python2.7,适用于Windows、Mac和Linux。 谢谢
多亏了这条评论,我才看清了错误所在。我没有正确使用byref变量。见评论。当代码正确使用byref时,假设它正常工作。在
编辑
按照评论中的要求,这里是我最后使用的。在
相关问题 更多 >
编程相关推荐