Python 访问ctypes返回的对象';s方法
我需要将C++ DLL打包到Python。我正在使用Python 访问ctypes返回的对象';s方法,python,ctypes,Python,Ctypes,我需要将C++ DLL打包到Python。我正在使用ctypes模块来实现这一点 c++标题类似于: class NativeObj { void func(); } extern "C" { NativeObj* createNativeObj(); }; //extern "C" 我想在python代码中创建NativeObj,然后调用它的func方法 我写了这段代码并得到了指向NativeObj的指针,但我没有找到如何访问func >>> impo
ctypes
模块来实现这一点
c++标题类似于:
class NativeObj
{
void func();
}
extern "C"
{
NativeObj* createNativeObj();
}; //extern "C"
我想在python代码中创建NativeObj
,然后调用它的func
方法
我写了这段代码并得到了指向NativeObj
的指针,但我没有找到如何访问func
>>> import ctypes
>>> d = ctypes.cdll.LoadLibrary('dll/path')
>>> obj = d.createNativeObj()
>>> obj
36408838
>>> type(obj)
<type 'int'>
>>导入ctypes
>>>d=ctypes.cdll.LoadLibrary('dll/path')
>>>obj=d.createNativeObj()
>>>obj
36408838
>>>类型(obj)
谢谢。不能从cType调用C++实例方法。您需要导出一个调用该方法的非成员函数。在C++中看起来是这样的:
void callFunc(NativeObj* obj)
{
obj->func();
}
你可以这样称呼它:
import ctypes
d = ctypes.cdll.LoadLibrary('dll/path')
obj = d.createNativeObj()
d.callFunc(obj)
告诉ctypes
所涉及的类型也很有用
import ctypes
d = ctypes.cdll.LoadLibrary('dll/path')
createNativeObj = d.createNativeObj
createNativeObj.restype = ctypes.c_void_p
callFunc = d.callFunc
callFunc.argtypes = [ctypes.c_void_p]
obj = createNativeObj()
callFunc(obj)