Search code examples
pythonc++cpointersctypes

Transfer pointer from dll (c/c++) to python


I have a DLL written in C++ ( it can be rewritten in C ) and python code where I import the DLL with cdll.LoadLibrary(). Then I specify a function from DLL and it's agrument types. Then I call the function which returns double *. The pointer points to memory allocated somewhere in the heap — the function does some math and stores the result in 3500 x 8 array ( I allocate memory with new[] and don't free it in DLL function. I supposed to use that DLL with C++ code so I could free the memory somewhere in it ).

The question is. Is there a way I can receive double * from DLL and work with array, on which the pointer points? As I know, python has no implementation for pointers. Thank you!

P.S. As I know, default return type for DLL functions in Python is int. So can I work with 3500 x 8 data using it's int adress?


Solution

  • you should try dll.your_function.restype = ctypes.POINTER(ctypes.c_double)