Skip to content
Advertisement

How to set arbitrary size array in ctypes?

So I have a function imported from a .dll library that takes in a pointer to a struct of the form:

JavaScript

The idea of function is that if A is NULL, then function(D*) will update A with the size of the required buffer. Hence, if on the other hand B is an array of size A, then function(D*) will return the filled array with the A bytes.

In trying to import the C function with ctypes, I mimic the code:

JavaScript

But when I attempt in python to run the function twice, I get a type error that LP_c_byte_p_250 doesn’t work with LP_c_byte (sorry, I am on mobile and may not have the names quite right).

JavaScript

How do I set struct up so that ctypes doesn’t prevent any sized array from occupying variable B?

Just to point out, if I skip the first function call and redefine my struct and argtypes explicitly, then I do get it to work.

JavaScript

So clearly I can recreate my struct and reimport my function every time I have a different size, but that doesn’t seem right. What am I doing wrong?

Advertisement

Answer

It appears the missing ingredient was to use the function cast in ctypes. It seems as though ctypes does some generic casting itself automatically in simple cases, but when presented something more difficult (like a struct built of two other stucts), it doesn’t seem to automatically cast.

JavaScript

This method even appears to work through a c_void_p; in which case you can forcibly use C functions without needing to explicitly define the exact structure they need in ctypes.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement