python - 如何使用 ctypes 将类对象从 c++ (DllExport) 返回到 python,反之亦然?
问题描述
我在 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
ctypes.c_int
除非另有说明,否则假定参数和返回值是 32 位整数 ( )。特别是如果您使用 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()
需要传递 aSchool*
才能正常工作。
要捕获更多错误,请声明特定的指针类型。这是一个显示其类型和示例的小类:
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)
推荐阅读
- vba - 循环遍历的文件格式的代码问题
- android - Android Delphi App 谷歌登录方式
- javascript - 使用 javascript 解析受密码保护的 Pdf 文件
- python - 将 Selenium 作为 Lambda 层运行
- vue.js - BootstrapVue - 表单文件输入的“multiple”属性不起作用
- excel - 如何在谷歌工作表中为多个工作表自动创建非空单元格的边框(例如“工作表 1”、“工作表 2”、...“工作表 n”);
- visual-studio-code - 如何在 vsc 中同时打开 2 个终端监视器(拆分)?
- regex - 带有前缀和约束的正则表达式
- vtk - 使用 paraview 查找有限元网格的最大网格尺寸 (maxh, lc)
- firebase - Firebase 登录和使用 Apple 登录未链接到同一用户帐户