简体   繁体   中英

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 ?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM