亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

python調(diào)用c++ ctype list傳數(shù)組或者返回數(shù)組的方法

 更新時間:2019年02月13日 08:59:40   作者:_uniqs  
今天小編就為大家分享一篇python調(diào)用c++ ctype list傳數(shù)組或者返回數(shù)組的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

示例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調(diào)用部分。類聲明:

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調(diào)用部分。具體調(diào)用:

  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))

傳入一個結(jié)構(gòu)體,使用引用返回,回到python中打印出來結(jié)果是對的。

這樣就可以傳入,可以傳出了。

示例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加進(jìn)去,編譯成DLL就OK了。

千萬注意python的運行時是32位的還是64位的,DLL或者SO必須和它對應(yīng)。

python類可以嵌套使用,繼承ctypes.Structure,部分成員是_fields_里定義的,部分成員在__init__里定義,這樣的類也可以ctypes.byref(self)傳進(jìn)c++去,傳的是指針,傳入傳出就都OK了。

注意示例2中ByteArrayNew_20_3的用法,python中是定義了20個變量,c++中是直接一個二維數(shù)組。內(nèi)存結(jié)構(gòu)是一致的,所以可以直接這樣使用。注意類型和長度必須一致,否則可能會內(nèi)存訪問越界。

以上這篇python調(diào)用c++ ctype list傳數(shù)組或者返回數(shù)組的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • pycharm內(nèi)無法import已安裝的模塊問題解決

    pycharm內(nèi)無法import已安裝的模塊問題解決

    今天小編就為大家分享一篇pycharm內(nèi)無法import已安裝的模塊問題解決,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-02-02
  • python 計算平均平方誤差(MSE)的實例

    python 計算平均平方誤差(MSE)的實例

    今天小編就為大家分享一篇python 計算平均平方誤差的實例 (MSE),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-06-06
  • python實現(xiàn)內(nèi)存監(jiān)控系統(tǒng)

    python實現(xiàn)內(nèi)存監(jiān)控系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了python實現(xiàn)內(nèi)存監(jiān)控系統(tǒng),通過系統(tǒng)命令或操作系統(tǒng)文件獲取到內(nèi)存信息,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-06-06
  • Python應(yīng)用之bin文件的制作

    Python應(yīng)用之bin文件的制作

    bin是二進(jìn)制文件,其用途依系統(tǒng)或應(yīng)用而定。一種文件格式binary的縮寫。這篇文章主要為大家介紹了Python如何實現(xiàn)bin文件的制作,需要的可以參考一下
    2023-01-01
  • Python 實現(xiàn)網(wǎng)課實時監(jiān)控自動簽到、打卡功能

    Python 實現(xiàn)網(wǎng)課實時監(jiān)控自動簽到、打卡功能

    這篇文章主要介紹了Python實現(xiàn)網(wǎng)課實時監(jiān)控自動簽到,打卡功能,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-03-03
  • python 自動化將markdown文件轉(zhuǎn)成html文件的方法

    python 自動化將markdown文件轉(zhuǎn)成html文件的方法

    這篇文章主要介紹了python 自動化將markdown文件轉(zhuǎn)成html文件的方法的相關(guān)資料,本文介紹的非常詳細(xì),具有參考借鑒價值,需要的朋友可以參考下
    2016-09-09
  • python如何實現(xiàn)向上取整

    python如何實現(xiàn)向上取整

    這篇文章主要介紹了python如何實現(xiàn)向上取整問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-01-01
  • Python抓取框架 Scrapy的架構(gòu)

    Python抓取框架 Scrapy的架構(gòu)

    這篇文章主要為大家詳細(xì)介紹了Python抓取框架,針對Scrapy的架構(gòu)進(jìn)行分析,感興趣的小伙伴們可以參考一下
    2016-08-08
  • Django bulk_create()、update()與數(shù)據(jù)庫事務(wù)的效率對比分析

    Django bulk_create()、update()與數(shù)據(jù)庫事務(wù)的效率對比分析

    這篇文章主要介紹了Django bulk_create()、update()與數(shù)據(jù)庫事務(wù)的效率對比分析,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-05-05
  • Python基礎(chǔ)之hashlib模塊詳解

    Python基礎(chǔ)之hashlib模塊詳解

    這篇文章主要介紹了Python基礎(chǔ)之hashlib模塊詳解,文中有非常詳細(xì)的代碼示例,對正在學(xué)習(xí)python基礎(chǔ)的小伙伴們有非常好的幫助,需要的朋友可以參考下
    2021-05-05

最新評論