简体   繁体   English

将python类对象作为参数传递给C库

[英]Passing python class object as parameter to c library

I have a python script that calls methods exported out of C shared library. 我有一个python脚本,该脚本调用从C共享库导出的方法。

How to pass a python class object as parameter to one of these methods. 如何将python类对象作为参数传递给这些方法之一。

Python code : Python代码:

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

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

C sample code: C示例代码:

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". 我可以在c函数“ func”中访问存储在module1参数中的值吗?

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 : 您可以在python中将结构声明为:

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

and pass the reference of this structure. 并传递此结构的参考。

Refer : ctypes docs page 请参阅: ctypes docs页面

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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