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
{
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->cbResultCard20 = 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<
oRet.topy(ret_py);
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中打印出来结果是对的。
这样就可以传入,可以传出了。
参考学习:http://www.cnblogs.com/night-ride-depart/p/4907613.html