Hello,

I am having trouble using ctypes to call a Dll function. In short, the function accepts a pointer to an array of structures....like:

int arrayFiller(
	USHORT sensorID,
	void *pRecord,
	int	recordSize
	);

I have generated a pythonified version of the function by importing the Dll etc. We will call this function "pyArrayFiller"

To call pyArrayFiller, I need to generate the python version of the `void *pRecord` which is a pointer to an array of four structures as defined below:

The c-code for the structure looks like this:

typedef struct tagMyStruct
  {
	double	x;
	double	y;
	double	z;
        double	a;
	double	e;
	double	r;
	double	time;
	USHORT	quality;		
  } MyStruct;

So I constructed a python version of the same structure:

class pyMyStructure(ctypes.Structure):
        _fields_ = [("x", ctypes.c_double),
                    ("y", ctypes.c_double),
                    ("z", ctypes.c_double),
                    ("a", ctypes.c_double),                      
                    ("e", ctypes.c_double),
                    ("r", ctypes.c_double),
                    ("time", ctypes.c_double),
                    ("quality", ctypes.c_double)]

Now, I would like to:

i.) create an array of 4 pyMyStruct()`s
ii.) generate a pointer to this array to pass to the 'pyArrayFiller' function.

Any tips? If this is a terrible/insufficient explanation, I can try again.

Thanks!

Recommended Answers

All 2 Replies

I think you create an array like this

arr = (pyMyStructure * 4)()

but I'm a beginner in ctypes. Perhaps you can pass ctypes.pointer(arr) to your function.

Hi!

Yeah Gribouillis - the solution I ended up with was along those lines:

recordPointerType = structures.pyDoubleMeasuresRecord * 4 
            
record = recordPointerType() 

recordPointer = ctypes.pointer(record)

I liked yours better, so that's what ended up in the code. Thanks Gribouillis!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.