python调用c++ ctype list传数组或者返回数组的方法


Posted in Python onFebruary 13, 2019

示例1:

pycallclass.cpp:

#include <iostream>
using namespace std;
typedef unsigned char BYTE;
#define MAX_COUNT 20
 
struct tagOutCardResult_py
{
	BYTE							cbCardCount;					
	BYTE							cbResultCard1;
	BYTE							cbResultCard2;
	BYTE							cbResultCard3;
	BYTE							cbResultCard4;
	BYTE							cbResultCard5;
	BYTE							cbResultCard6;
	BYTE							cbResultCard7;
	BYTE							cbResultCard8;
	BYTE							cbResultCard9;
	BYTE							cbResultCard10;
	BYTE							cbResultCard11;
	BYTE							cbResultCard12;
	BYTE							cbResultCard13;
	BYTE							cbResultCard14;
	BYTE							cbResultCard15;
	BYTE							cbResultCard16;
	BYTE							cbResultCard17;
	BYTE							cbResultCard18;
	BYTE							cbResultCard19;
	BYTE							cbResultCard20;
};
 
struct tagOutCardResult
{
	BYTE							cbCardCount;					
	BYTE							cbResultCard[MAX_COUNT];		
	void clear()
	{
		cbCardCount = 0;
		for (int nIdx = 0;nIdx < MAX_COUNT;++nIdx)
		{
			cbResultCard[nIdx] = 0;
		}
	}	
	void topy(tagOutCardResult_py* ppy)
	{
		cout<<"topy function begin"<<endl;
		ppy->cbCardCount = cbCardCount;
		cout<<"topy function 1"<<endl;
		ppy->cbResultCard1 = cbResultCard[1 - 1];
		cout<<"topy function 2"<<endl;
		ppy->cbResultCard2 = cbResultCard[2 - 1];
		ppy->cbResultCard3 = cbResultCard[3 - 1];
		ppy->cbResultCard4 = cbResultCard[4 - 1];
		ppy->cbResultCard5 = cbResultCard[5 - 1];
		ppy->cbResultCard6 = cbResultCard[6 - 1];
		ppy->cbResultCard7 = cbResultCard[7 - 1];
		ppy->cbResultCard8 = cbResultCard[8 - 1];
		ppy->cbResultCard9 = cbResultCard[9 - 1];
		ppy->cbResultCard10 = cbResultCard[10 - 1];
		ppy->cbResultCard11 = cbResultCard[11 - 1];
		ppy->cbResultCard12 = cbResultCard[12 - 1];
		ppy->cbResultCard13 = cbResultCard[13 - 1];
		ppy->cbResultCard14 = cbResultCard[14 - 1];
		ppy->cbResultCard15 = cbResultCard[15 - 1];
		ppy->cbResultCard16 = cbResultCard[16 - 1];
		ppy->cbResultCard17 = cbResultCard[17 - 1];
		ppy->cbResultCard18 = cbResultCard[18 - 1];
		ppy->cbResultCard19 = cbResultCard[19 - 1];
		ppy->cbResultCard20 = cbResultCard[20 - 1];
		cout<<"topy function end"<<endl;
	}
};
 
class TestLib
{
	public:
		void display(tagOutCardResult& ret);
};
void TestLib::display(tagOutCardResult& ret) {
	ret.cbCardCount = 3;
	ret.cbResultCard[0] = 1;
	ret.cbResultCard[1] = 50;
	ret.cbResultCard[2] = 100;
 
	cout<<"First display aaa ";
	cout<<"hello ";
	cout<<"world ";
}
 
extern "C" {
	TestLib oGameLogic;
	void display(tagOutCardResult_py* ret_py) {
		tagOutCardResult oRet;
		oGameLogic.display(oRet);
		cout<<"before topy"<<endl;
		oRet.topy(ret_py);
		cout<<"after topy"<<endl;
		cout<<"in cpp:ret_py->cbCardCount:"<<ret_py->cbCardCount<<endl;
		cout<<"in cpp:ret_py->cbResultCard1:"<<ret_py->cbResultCard1<<endl;
		cout<<" this:" << ret_py << endl;
	}
}

编译脚本:

g++ -o libpycallclass.so -shared -fPIC pycallclass.cpp -I/usr/include/python2.6 -L/usr/lib64/python2.6/config

Game.py调用部分。类声明:

import ctypes
 
class tagOutCardResult_py(ctypes.Structure):
 _fields_ = [("cbCardCount", ctypes.c_ubyte), \
("cbResultCard1", ctypes.c_ubyte), \
("cbResultCard2", ctypes.c_ubyte), \
("cbResultCard3", ctypes.c_ubyte), \
("cbResultCard4", ctypes.c_ubyte), \
("cbResultCard5", ctypes.c_ubyte), \
("cbResultCard6", ctypes.c_ubyte), \
("cbResultCard7", ctypes.c_ubyte), \
("cbResultCard8", ctypes.c_ubyte), \
("cbResultCard9", ctypes.c_ubyte), \
("cbResultCard10", ctypes.c_ubyte), \
("cbResultCard11", ctypes.c_ubyte), \
("cbResultCard12", ctypes.c_ubyte), \
("cbResultCard13", ctypes.c_ubyte), \
("cbResultCard14", ctypes.c_ubyte), \
("cbResultCard15", ctypes.c_ubyte), \
("cbResultCard16", ctypes.c_ubyte), \
("cbResultCard17", ctypes.c_ubyte), \
("cbResultCard18", ctypes.c_ubyte), \
("cbResultCard19", ctypes.c_ubyte), \
("cbResultCard20", ctypes.c_ubyte)]

Game.py调用部分。具体调用:

import ctypes
  so = ctypes.cdll.LoadLibrary
  lib = so("./libpycallclass.so")
  ERROR_MSG('display(\)')
  ret = tagOutCardResult_py(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
  ERROR_MSG("before lib.display(ctypes.byref(ret))")
  lib.display(ctypes.byref(ret))
  ERROR_MSG("after lib.display(ctypes.byref(ret))")
  ERROR_MSG('#######################################################################################')
  ERROR_MSG(ret)
  ERROR_MSG(ret.cbCardCount)
  ERROR_MSG(ret.cbResultCard1)
  ERROR_MSG(ret.cbResultCard2)
  ERROR_MSG(ret.cbResultCard3)
  ERROR_MSG(type(ret))

传入一个结构体,使用引用返回,回到python中打印出来结果是对的。

这样就可以传入,可以传出了。

示例1end#########################################################################

示例2:

pycallclass.cpp:

#include <iostream>
using namespace std;
typedef unsigned char BYTE;
#define MAX_COUNT 20
 
#if defined(WIN32)||defined(WINDOWS)
#define DLL_EXPORT __declspec(dllexport)
#else
#define DLL_EXPORT
#endif
 
struct ByteArray_20
{
	BYTE e1;
	BYTE e2;
	BYTE e3;
	BYTE e4;
	BYTE e5;
	BYTE e6;
	BYTE e7;
	BYTE e8;
	BYTE e9;
	BYTE e10;
	BYTE e11;
	BYTE e12;
	BYTE e13;
	BYTE e14;
	BYTE e15;
	BYTE e16;
	BYTE e17;
	BYTE e18;
	BYTE e19;
	BYTE e20;
};
struct ByteArray_20_3
{
	ByteArray_20 e1;
	ByteArray_20 e2;
	ByteArray_20 e3;
};
 
struct ByteArrayNew_20_3
{
	BYTE e[3][20];
};
 
class TestLib
{
	public:
		void LogicFunc(ByteArray_20_3& ret);
		void LogicFuncNew(ByteArrayNew_20_3& ret);
};
void TestLib::LogicFunc(ByteArray_20_3& ret) {
	ret.e1.e1 = 3;
	ret.e1.e2 = 1;
	ret.e1.e3 = 50;
	ret.e2.e1 = 100;
	ret.e2.e2 = 200;
	ret.e2.e3 = 20;
 
	cout<<"TestLib::LogicFunc"<<endl;
}
void TestLib::LogicFuncNew(ByteArrayNew_20_3& ret) {
	ret.e[0][0] = 31;
	ret.e[0][1] = 11;
	ret.e[0][2] = 51;
	ret.e[1][0] = 101;
	ret.e[1][1] = 201;
	ret.e[1][2] = 21;
 
	cout << "TestLib::LogicFuncNew" << endl;
}
 
extern "C" {
	TestLib oGameLogic;
	void DLL_EXPORT display(ByteArray_20_3* pret) {
		cout<<"cpp display func begin"<<endl;
		oGameLogic.LogicFunc(*pret);
		cout<<"cpp display func end"<<endl;
	}
	void DLL_EXPORT display2(ByteArrayNew_20_3* pret) {
		cout << "cpp display2 func begin" << endl;
		oGameLogic.LogicFuncNew(*pret);
		cout << "cpp display2 func end" << endl;
	}
}

pycallclass.py:

import ctypes
 
def ERROR_MSG(str):
 print str
 
class ByteArray_20(ctypes.Structure):
 _fields_ = [\
("e1", ctypes.c_ubyte), \
("e2", ctypes.c_ubyte), \
("e3", ctypes.c_ubyte), \
("e4", ctypes.c_ubyte), \
("e5", ctypes.c_ubyte), \
("e6", ctypes.c_ubyte), \
("e7", ctypes.c_ubyte), \
("e8", ctypes.c_ubyte), \
("e9", ctypes.c_ubyte), \
("e10", ctypes.c_ubyte), \
("e11", ctypes.c_ubyte), \
("e12", ctypes.c_ubyte), \
("e13", ctypes.c_ubyte), \
("e14", ctypes.c_ubyte), \
("e15", ctypes.c_ubyte), \
("e16", ctypes.c_ubyte), \
("e17", ctypes.c_ubyte), \
("e18", ctypes.c_ubyte), \
("e19", ctypes.c_ubyte), \
("e20", ctypes.c_ubyte)]
 
 
class ByteArray_20_3(ctypes.Structure):
 _fields_ = [\
("e1", ByteArray_20), \
("e2", ByteArray_20), \
("e3", ByteArray_20)]
 def __init__(self):
  self.aaa = 123
  self.bbb = [1, 2, 3, 4, 5]
  self.ccc = "alksdfjlasdfjk"
 def test(self):
  self.aaa = 123
  self.bbb = [1, 2, 3, 4, 5]
  self.ccc = "alksdfjlasdfjk"
  self.e1.e1 = 5
  self.e1.e2 = 20
 
 
so = ctypes.cdll.LoadLibrary
lib = so("./libpycallclass.dll")
print('display()')
ret = ByteArray_20_3()
ret.test()
ERROR_MSG(ret.e1.e1)
ERROR_MSG(ret.e1.e2)
print("before lib.display(ctypes.byref(ret))")
lib.display(ctypes.byref(ret))
print("after lib.display(ctypes.byref(ret))")
print('#######################################################################################')
print(ret)
ERROR_MSG(ret.e1)
ERROR_MSG(ret.e2)
ERROR_MSG(ret.e3)
ERROR_MSG(ret.e1.e1)
ERROR_MSG(ret.e1.e2)
ERROR_MSG(ret.e1.e3)
ERROR_MSG(ret.e2.e1)
ERROR_MSG(ret.e2.e2)
ERROR_MSG(ret.e2.e3)
ERROR_MSG(type(ret))
 
print("before lib.display2(ctypes.byref(ret))")
lib.display2(ctypes.byref(ret))
print("after lib.display2(ctypes.byref(ret))")
print('#######################################################################################')
print(ret)
ERROR_MSG(ret.e1)
ERROR_MSG(ret.e2)
ERROR_MSG(ret.e3)
ERROR_MSG(ret.e1.e1)
ERROR_MSG(ret.e1.e2)
ERROR_MSG(ret.e1.e3)
ERROR_MSG(ret.e2.e1)
ERROR_MSG(ret.e2.e2)
ERROR_MSG(ret.e2.e3)
ERROR_MSG(type(ret))
 
ret.test()
ERROR_MSG(ret.e1.e1)
ERROR_MSG(ret.e1.e2)

g++:

g++ -o libpycallclass.so -shared -fPIC pycallclass.cpp -I/usr/include/python2.6 -L/usr/lib64/python2.6/config

windows:

新建一个DLL工程,把pycallclass.cpp加进去,编译成DLL就OK了。

千万注意python的运行时是32位的还是64位的,DLL或者SO必须和它对应。

python类可以嵌套使用,继承ctypes.Structure,部分成员是_fields_里定义的,部分成员在__init__里定义,这样的类也可以ctypes.byref(self)传进c++去,传的是指针,传入传出就都OK了。

注意示例2中ByteArrayNew_20_3的用法,python中是定义了20个变量,c++中是直接一个二维数组。内存结构是一致的,所以可以直接这样使用。注意类型和长度必须一致,否则可能会内存访问越界。

以上这篇python调用c++ ctype list传数组或者返回数组的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
使用Python进行新浪微博的mid和url互相转换实例(10进制和62进制互算)
Apr 25 Python
python实现通过pil模块对图片格式进行转换的方法
Mar 24 Python
Python实现批量下载图片的方法
Jul 08 Python
在Python的Django框架中创建语言文件
Jul 27 Python
利用Python进行异常值分析实例代码
Dec 07 Python
python中Apriori算法实现讲解
Dec 10 Python
通过Python模块filecmp 对文件比较的实现方法
Jun 29 Python
创建Django项目图文实例详解
Jun 06 Python
使用django实现一个代码发布系统
Jul 18 Python
浅析pandas 数据结构中的DataFrame
Oct 12 Python
基于OpenCV的路面质量检测的实现
Nov 04 Python
pandas将list数据拆分成行或列的实现
Dec 13 Python
python调用c++传递数组的实例
Feb 13 #Python
利用ctypes获取numpy数组的指针方法
Feb 12 #Python
python3利用ctypes传入一个字符串类型的列表方法
Feb 12 #Python
使用python绘制二元函数图像的实例
Feb 12 #Python
python matplotlib实现双Y轴的实例
Feb 12 #Python
对Pycharm创建py文件时自定义头部模板的方法详解
Feb 12 #Python
numpy基础教程之np.linalg
Feb 12 #Python
You might like
php中对xml读取的相关函数的介绍一
2008/06/05 PHP
ThinkPHP访问不存在的模块跳转到404页面的方法
2014/06/19 PHP
javascript之函数直接量(function(){})()
2007/06/29 Javascript
jQuery ajax BUG:object doesn't support this property or method
2010/07/06 Javascript
DOM 中的事件处理介绍
2012/01/18 Javascript
JavaScript语言核心数据类型和变量使用介绍
2013/08/23 Javascript
jQuery .tmpl() 用法示例介绍
2014/08/21 Javascript
编写高性能Javascript代码的N条建议
2015/10/12 Javascript
jquery自定义右键菜单、全选、不连续选择
2016/03/01 Javascript
AngularJS指令与控制器之间的交互功能示例
2016/12/14 Javascript
JavaScript之面向对象_动力节点Java学院整理
2017/06/29 Javascript
原生JS实现日历组件的示例代码
2017/09/22 Javascript
Vue.js简易安装和快速入门(第二课)
2017/10/17 Javascript
Angular实现点击按钮后在上方显示输入内容的方法
2017/12/27 Javascript
angularjs实现table增加tr的方法
2018/02/27 Javascript
基于mpvue的简单弹窗组件mptoast使用详解
2019/08/02 Javascript
[03:17]DOTA2-DPC中国联赛1月29日Recap集锦
2021/03/11 DOTA
使用Python的Twisted框架编写非阻塞程序的代码示例
2016/05/25 Python
Python数据结构与算法之常见的分配排序法示例【桶排序与基数排序】
2017/12/15 Python
Python实现的用户登录系统功能示例
2018/02/05 Python
django Serializer序列化使用方法详解
2018/10/16 Python
python3文件复制、延迟文件复制任务的实现方法
2019/09/02 Python
Django 批量插入数据的实现方法
2020/01/12 Python
什么是python的必选参数
2020/06/21 Python
Django+Django-Celery+Celery的整合实战
2021/01/20 Python
python 指定源路径来解决import问题的操作
2021/03/04 Python
Sperry官网:帆船鞋创始品牌
2016/09/07 全球购物
艺术用品:Arteza
2018/11/25 全球购物
经典c++面试题四
2015/05/14 面试题
项目经理的岗位职责
2013/11/23 职场文书
会计主管岗位职责
2014/01/03 职场文书
应聘英语教师求职信
2014/04/24 职场文书
大班上学期幼儿评语
2014/04/30 职场文书
村委会贫困证明范本
2014/09/17 职场文书
2015试用期转正工作总结
2014/12/12 职场文书
学生会工作感言
2015/08/07 职场文书