python结构体数组传出_python调⽤c++ctypelist传数组或者返
回数组的⽅法
⽰例1:
pycallclass.cpp:
#include
using namespace std;
typedef unsigned char BYTE;
#define MAX_COUNT 20
struct tagOutCardResult_py
{
BYTEcbCardCount;
BYTEcbResultCard1;
BYTEcbResultCard2;
BYTEcbResultCard3;
BYTEcbResultCard4;
BYTEcbResultCard5;
BYTEcbResultCard6;
BYTEcbResultCard7;
BYTEcbResultCard8;
BYTEcbResultCard9;
BYTEcbResultCard10;
BYTEcbResultCard11;
BYTEcbResultCard12;
BYTEcbResultCard13;
BYTEcbResultCard14;
BYTEcbResultCard15;
BYTEcbResultCard16;
BYTEcbResultCard17;
BYTEcbResultCard18;
BYTEcbResultCard19;
BYTEcbResultCard20;
};
struct tagOutCardResult
python 定义数组
{
BYTEcbCardCount;
BYTEcbResultCard[MAX_COUNT];
void clear()
{
cbCardCount = 0;
for (int nIdx = 0;nIdx < MAX_COUNT;++nIdx) {
cbResultCard[nIdx] = 0;
}
}
void topy(tagOutCardResult_py* ppy)
{
cout<
ppy->cbCardCount = cbCardCount;
cout<
ppy->cbResultCard1 = cbResultCard[1 - 1]; cout<
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->cbR
esultCard20 = cbResultCard[20 - 1]; cout<
}
};
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<
cout<
cout<
}
extern "C" {
TestLib oGameLogic;
void display(tagOutCardResult_py* ret_py) { tagOutCardResult oRet;
oGameLogic.display(oRet);
cout<
cout<
cout<cbCardCount:"<cbCardCount<
cout<cbResultCard1:"<cbResultCard1<
cout<
}
}
编译脚本:
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
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;
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
Python之数据类型和for循环
下一篇 »
发表评论