1
votes

I'm trying to pass a ctypes structure to a DLL. The problem is that the data read from the structure by the DLL is junk. This isn't the first structure that I've implemented in ctypes, but it's more complex than others that I've done.

The structure is defined as follows:

typedef struct {
    double L, x, y;
} CieLxy;


typedef struct {
    int ledCur[3];
    CieLxy targetCie, modelCie, measuredCie;
} I1VpCal;


typedef struct {
    I1VpCal i1CalArray[8][7];
} I1CalArray;

With the following test function:

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;
}

In Python I have the following ctypes definitions:

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))

When I run the following code:

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)

The data it prints is junk. It looks more like addresses.

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

Any help with this would be really appreciated. I have read many posts and tried different solutions, but the result is always the same.

Using Python 2.7, for Windows, Mac, and Linux. Thanks

1
My mistake, I removed stuff to avoid confusion. I missed that one. - BaldDude
Thanks, your answer made me see my mistake! I was using the wrong object on the byref(). Do you want to provide the answer so that I can vote for you? - BaldDude
Foo.argtypes = [ctypes.POINTER(I1CalArray)] is correct, for a literal implementation. Then call foo(i1CalArray_struct), which calls Foo(ctypes.byref(i1CalArray)). That should work. It works for me when I test both 32-bit and 64-bit builds created with VS 2010. - Eryk Sun
It does work. Thanks. - BaldDude
You can add an answer later. - Eryk Sun

1 Answers

1
votes

Thanks to the comment, I was able to see the mistake. I wasn't using the byref variable properly. See comments. When using the byref variable properly, the code works as it is suppose to.


Edit

As requested in the comment, here is what I used in the end.

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