Python 和 Ctypes : Wrong data when reading structure passed as parameter

标签 python c ctypes

我正在尝试将 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;

具有以下测试功能:

int foo(I1CalArray* p_i1CalArray)
{
    int i, j;

    for (i=0; i<8; i++)
    {
        for (j=0; j<7; j++)
        {
            printf("in dll, i1CalArray[%d][%d].ledCur[0] = %d\n", 
                   i, j, p_i1CalArray->i1CalArray[i][j].ledCur[0]);
            printf("in dll, i1CalArray[%d][%d].measuredCie.L = %f\n", 
                   i, j, p_i1CalArray->i1CalArray[i][j].measuredCie.L);
        }    
    }
    return 0;
}

在 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

如能提供任何帮助,我们将不胜感激。我阅读了很多帖子并尝试了不同的解决方案,但结果总是一样。

使用 Python 2.7,适用于 Windows、Mac 和 Linux。 谢谢

最佳答案

多亏了评论,我才能够看到错误。我没有正确使用 byref 变量。看评论。当正确使用 byref 变量时,代码会按预期工作。


编辑

按照评论中的要求,这是我最后使用的。

class I1CalArray(ctypes.Structure):
    _fields_ = [("i1CalArray", 8*(7*I1VpCal))]


Foo = Dll['foo']
Foo.argtypes = [ctypes.POINTER(I1CalArray)] 
def foo( i1CalArray):
    Foo(ctypes.byref(i1CalArray))   
    return i1CalArray

关于Python 和 Ctypes : Wrong data when reading structure passed as parameter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28283058/

相关文章:

Python pysft/paramiko 'EOF during negotiation' 错误

c - 子进程是否执行这两个打印?

python - 如何在 Python 中将字节数组值转换为 float

python - 如何在不设置第 23 位的情况下在 python 中创建自定义 NaN(单精度)?

python - 为什么 Python 3 shell 中的文本格式与生成的文本文件不同?

python - 有与 MATLAB 的 vct2mtx 等效的 Python 吗?

c - 使用链表的优先级队列

c - 命名和未命名信号量的用法

Python 使用 ctypes 传递 char * 数组并填充结果

python - 如何检查 GUI 应用程序是否在 Ubuntu 上运行?