Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ctypes reference double pointer

Tags:

python

ctypes

I have a C function that the doxygen looks like this:

long function( classA * obj1,
               classB * obj2,
               classC ** obj3,
               long value,
               const char * name1,
               const char * name2,
               classD ** obj4,
               classD ** obj5,
               classD ** obj6 
               )    

I need to call it from python and get the 3 last arguments to pass to another dll. Currently I can call the DLL fine, but if I try to pass the last 3 arguments to the other function I get an error like this:

WindowsError: exception: access violation writing 0x0000000000000000

I am probably doing something wrong in the ctypes side of things and I would appreciate some light on how to use it...

The second DLL looks like this:

int function2(ClassD *  obj4)   

And currently I "assemble" the last 3 arguments like this:

  temp=ctypes.c_long*1
  obj = temp(0)

Also, is there any way of going from ctypes to swig? Apparently I can do the other way around using the long function.

like image 756
Mac Avatar asked Oct 16 '12 02:10

Mac


1 Answers

I had to do some extra digging to figure out how C-types works and the following seems to work. For function I needed to create an array:

arg = (ctypes.POINTER(ctypes.c_double) * 1)()

and pass it by reference to the function:

ctypes.byref(arg)

For the second function I would just pass one element of the array I created:

arg[0]
like image 135
Mac Avatar answered Nov 01 '22 22:11

Mac