Dexter
Dexter

Reputation: 1337

Passing python class object as parameter to c library

I have a python script that calls methods exported out of C shared library.

How to pass a python class object as parameter to one of these methods.

Python code :

class module(object)
     def __init__(self, param1):
         self._param1 = param1

lib = CDLL("name")
mod = module1(10)
lib.func(pointer(mod))

C sample code:

struct module {
  int val;
};
extern "C" void func(struct module* data);

Can i access the value stored in module1 parameter in the c function "func".

Any more ideas how this functionality can be achieved ?

Upvotes: 1

Views: 207

Answers (1)

Dexter
Dexter

Reputation: 1337

After bit of goggling found out a way:

you can declare a structure in python as :

class POINT(Structure):
    _fields_ = [("x", c_int),
               ("y", c_int)]

and pass the reference of this structure.

Refer : ctypes docs page

Upvotes: 1

Related Questions