Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 访问ctypes返回的对象';s方法_Python_Ctypes - Fatal编程技术网

Python 访问ctypes返回的对象';s方法

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

我需要将C++ DLL打包到Python。我正在使用
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)