Skip to content Skip to sidebar Skip to footer

Ctypes: How Do I Define An Array Of A Structure As A Field Of Another Structure?

I have a C structure that looks like the following: typedef struct _DXYZ { DXYZSTATE State[]; } DXYZ, *PDXYZ Essentially, an array of DXYZSTATE, of unknown size. When I try to

Solution 1:

Here's a way but it isn't pretty. ctypes doesn't do variable arrays in a structure so to access the variable data requires some casting.

test.c Implements a test function returning the variable structure data. In this case I hard-coded a return array of size 4 but it could be any size.

#include<stdlib.h>typedefstructSTATE {
    int a;
    int b;
} STATE;

typedefstructDXYZ {
    int count;
    STATE state[];
} DXYZ, *PDXYZ;

__declspec(dllexport) PDXYZ get(void){
    PDXYZ pDxyz = malloc(sizeof(DXYZ) + sizeof(STATE) * 4);
    pDxyz->count = 4;
    pDxyz->state[0].a = 1;
    pDxyz->state[0].b = 2;
    pDxyz->state[1].a = 3;
    pDxyz->state[1].b = 4;
    pDxyz->state[2].a = 5;
    pDxyz->state[2].b = 6;
    pDxyz->state[3].a = 7;
    pDxyz->state[3].b = 8;
    return pDxyz;
}

__declspec(dllexport) voidmyfree(PDXYZ pDxyz){
    free(pDxyz);
}

test.py

from ctypes import *
import struct

classState(Structure):
    _fields_ = [('a',c_int),
                ('b',c_int)]

classDXYZ(Structure):
    _fields_ = [('count',c_int),      # Number of state objects
                ('state',State * 0)]  # Zero-sized array# Set the correct arguments and return type for the DLL functions.
dll = CDLL('test')
dll.get.argtypes = None
dll.get.restype = POINTER(DXYZ)
dll.myfree.argtypes = POINTER(DXYZ),
dll.myfree.restype = None

pd = dll.get()    # Get the returned pointer
d = pd.contents   # Dereference it.print('count =',d.count)
# Cast a pointer to the zero-sized array to the correct size and dereference it.
s = cast(byref(d.state),POINTER(State * d.count)).contents

for c in s:
    print(c.a,c.b)

dll.myfree(pd)

Output:

count = 4
1 2
3 4
5 6
7 8

Post a Comment for "Ctypes: How Do I Define An Array Of A Structure As A Field Of Another Structure?"