C++ DLL returning a pointer called from Python -


i trying access c++ dll python (i new python). overcame many calling convention issues , got run without compile/linking error. when print returning array c++ dll in python shows random initialized values. looks values not correctly returned.

my c++ code looks this.

double dll_export __cdecl *function1(int arg1, int arg2, double arg3[],int arg4,double arg5,double arg6,double arg7, double arg8) {              double *areas = new double[7];          ....calculations          return areas; } 

my python code looks follows:

import ctypes  calcdll = ctypes.cdll("calcroutines.dll")  arg3_py=(ctypes.c_double*15)(1.926,1.0383,0.00008,0.00102435,0.0101,0.0,0.002,0.0254,102,1,0.001046153,0.001046153,0.001046153,0.001046153,20) dummy = ctypes.c_double(0.0)  calcdll.function1.restype = ctypes.c_double*7 areas = calcdll.function1(ctypes.c_int(1),ctypes.c_int(6),arg3_py,ctypes.c_int(0),dummy,dummy,dummy,dummy)  num in hxareas:     print("\t", num) 

the output of print statement below:

     2.4768722583947873e-306      3.252195577561737e+202      2.559357001198207e-306      5e-324      2.560791130833573e-306      3e-323      2.5621383435212475e-306 

any suggestion on doing wrong appreciated.

instead of

calcdll.function1.restype = ctypes.c_double * 7 

there should

calcdll.function1.restype = ctypes.pointer(ctypes.c_double) 

and then

areas = calcdll.function1(ctypes.c_int(1), ctypes.c_int(6), arg3_py,                           ctypes.c_int(0), dummy, dummy, dummy, dummy)  in range(7):     print("\t", areas[i]) 

i'm not sure ctypes in case of 'ctypes.c_double * 7', if tries extract 7 double stack or what.

tested with

double * function1(int arg1, int arg2, double arg3[],                    int arg4, double arg5, double arg6,                    double arg7, double arg8) {     double * areas = malloc(sizeof(double) * 7);     int i;      for(i=0; i<7; i++) {         areas[i] = i;     }      return areas; } 

the values in array printed correctly restype = ctypes.pointer(ctypes.c_double)


Comments