如何使用ctypes将类对象从c ++(DllExport)返回到python,反之亦然
How to return a class object from c++ (DllExport) to python using ctypes vice-versa?
我在 c++ 中有一个函数,它将类对象返回给 python,
我在下面的代码片段中有一个 python(ctypes import(的包装器,当我从 python 调用函数(Get_Student(时,学校用 c++ 创建了新实例,我无法检索学生类的数据,
C++ 代码为:
#include"stdafx.h"
#include<string>
using namespace std;
class Student
{
int ID;
public:
void SetId(int id)
{
ID = id;
}
int GetId()
{
return ID;
}
};
class School{
Student* stud;
public:
void* CreateStudent()
{
stud = new Student();
return stud;
}
Student* GetStudent()
{
return stud;
}
};
extern "C"
{
__declspec(dllexport) void Student_SetId(Student* stud ,int id)
{
stud->SetId(id);
}
__declspec(dllexport) int Student_GetId(Student* stud)
{
return (stud->GetId());
}
__declspec(dllexport) School* School_new(){
return new School();
}
__declspec(dllexport) void* School_CreateStudent(School* school){
return (school->CreateStudent());
}
__declspec(dllexport)Student* School_GetStudent(School* school){
return (school->GetStudent());
}
}
#The Python code is:
from ctypes import*
mydll=cdll.LoadLibrary("D:\shameel\Cpp\Sample_class\Debug\Sample_class.dll")
class Student():
def __init__(self):
self.nativeStudent = mydll.School_CreateStudent()
def Set_Id(self,arg):
mydll.Student_SetId(self.nativeStudent,arg)
def Get_Id(self):
return (mydll.Student_GetId(self.nativeStudent))
class School():
def __init__(self):
self.nativeSchool = mydll.School_new()
def CreateStudent(self):
return Student()
def Get_Student(self):
mydll.School_GetStudent.restype = c_void_p
self.nativestd = mydll.School_GetStudent(self.nativeSchool)
return (self.nativestd)
#The Python call is as follows:
fobj = School()
std_new = fobj.CreateStudent()
std_new.Set_Id(3)
Id = std_new.Get_Id()
getobj =fobj.Get_Student()
print(getobj)
当我打印 getobj 时,它的输出是"无"。
如何从 CPP 获取相同的对象?
ctypes
假设参数和返回值是32位整数(ctypes.c_int
(,除非另有说明。 特别是如果您使用的是 64 位 Python,指针将被截断为 32 位。 首先要做的是正确声明函数参数并返回值:
mydll.Student_SetId.argtypes = c_void_p,c_int
mydll.Student_SetId.restype = None
mydll.Student_GetId.argtypes = c_void_p,
mydll.Student_GetId.restype = c_int
mydll.School_new.argtypes = ()
mydll.School_new.restype = c_void_p
mydll.School_CreateStudent.argtypes = c_void_p,
mydll.School_CreateStudent.restype = c_void_p
mydll.School_GetStudent.argtypes = c_void_p,
mydll.School_GetStudent.restype = c_void_p
这也将帮助您捕获错误,例如需要传递School_CreateStudent()
School*
才能正常工作。
若要捕获更多错误,请声明特定的指针类型。 下面是一个小类,将显示其类型和示例:
class Ptr(c_void_p):
def __repr__(self):
cls_name = self.__class__.__name__
if self.value is not None:
return f'{cls_name}({self.value:#x})'
return f'{cls_name}(None)'
class pStudent(Ptr): pass
class pSchool(Ptr): pass
演示:
>>> p = pSchool()
>>> p
pSchool(None)
然后声明函数参数和结果,如下所示:
mydll.Student_SetId.argtypes = pStudent,c_int
mydll.Student_SetId.restype = None
mydll.Student_GetId.argtypes = pStudent,
mydll.Student_GetId.restype = c_int
mydll.School_new.argtypes = ()
mydll.School_new.restype = pSchool
mydll.School_CreateStudent.argtypes = pSchool,
mydll.School_CreateStudent.restype = pStudent
mydll.School_GetStudent.argtypes = pSchool,
mydll.School_GetStudent.restype = pStudent
现在,在Python中错误地使用代码的任何地方都应该给你一个很好的错误消息。
修复C++和 Python 代码后,print(getobj)
的输出将如下所示:
pStudent(0x1b54b89b350)
相关文章:
- 来自 std::list 的迭代器 .end() 按预期返回"0xcdcdcdcdcdcdcdcd"但 .begin()
- 什么时候在C++中返回常量引用是个好主意
- 你能重载对象变量名本身返回的内容吗
- 为什么 Serial.println(<char[]>);返回随机字符?
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- 如何获取std::result_of函数的返回类型
- QueryWorkingSet总是返回false
- (C++)分析树以计算返回错误值的简单算术表达式
- 访问者访问变体并返回不同类型时出错
- 如何返回一个类的两个对象相加的结果
- OpenInventor从9.8升级到10.4.2后,GLSL纹理返回零
- lower_bound()返回最后一个元素
- "throw expression code" 1e7 >返回 d 是什么?投掷标准::overflow_error( "too big" ) : d;意味 着?
- 奇怪的(对我来说)返回声明 - 在谷歌上找不到任何关于它的信息
- 如何取消对nullptr的屏蔽,返回正确的对象
- 奇怪的结构&GCC&clang(void*返回类型)
- 架构决策:返回std::future还是提供回调
- 从python中调用C++函数并获取返回值
- 矩阵向量乘法(cublasDgemv)返回零
- c#返回接口由DllExport编写