I'd like to use a function that is defined in a DLL from Python. The value returned from the C++ function (get_version) is a struct
typedef struct myStruct {
size_t size;
char * buff;
} myStruct ;
The Python code is:
lib = CDLL(myDLL.dll)
lib.get_version
The question is how do I handle the returned value?
I've read Voo's answer and reading other posts, but I'm still struggling with this
I declared the struct class (Foo, from Voo's answer) and set the restype
The code now looks
class Foo(Structure):
_fields_ = [('size', c_size_t), ('buff', c_char_p)]
lib = CDLL(myDLL.dll)
lib.get_version
lib.get_version.restype = Foo._fields_
I get the following error TypeError: restype must be a type, a callable, or None
I read about this and if I set the restype not as a list, e.g.: c_char_p, the error doesn't appear
When I set the restype
lib.restype = Foo.fields
The error doesn't appear but the restype for get_version is not set correctly
When looking at the variables in debug:
lib.restype = list: [('size', ), ('buff', )]
lib.get_version.restype = PyCSimpleType:
Any help would be appreciated
You'll have to use the ctypes module. You'll just have to define the struct in your python code with ctypes.
Something like:
>>> from ctypes import *
>>> class Foo(Structure):
... _fields_ = [("size", c_size_t), ("buff", c_char_p)]
should do the trick. Then you just set the restype of your get_version method to your struct so that the interpreter knows what it does return and then you can use it as expected.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With