0

这是 C/C++ 客户端调用:

char **ppCharOut;
char *pHello = "hello";
char *pWorld = "world";
ppCharOut = new char*[2];
ppCharOut[0] = pHello;
ppCharOut[1] = pWorld;
PythonCallback1FP(reinterpret_cast<int>(&ppCharOut), 2);

char **ppCharIn;
PythonCallback2FP(&ppCharIn);
cout << ppCharIn[0] << " " << ppCharIn[1];

经过近一天的谷歌搜索,阅读晦涩的文档,玩代码和核心转储,这里是相应的 Python / ctypes 代码:

def PythonCallBack1FP(arg1, arg2):
    from ctypes import POINTER, cast, c_char_p
    ppChar = cast(arg1, POINTER(POINTER(c_char_p))).contents
    for i in xrange(arg2):
        print ppChar[i]

def PythonCallback2FP(addressOfCharPP):
    from ctypes import POINTER, cast, c_char_p, pointer
    ArrayType = c_char_p * 2
    arrayOfCharPs = ArrayType()
    arrayOfCharPs[0] = "HELLO"
    arrayOfCharPs[1] = "BACK"
    charPP = pointer(arrayOfCharPs)
    ptr = cast(addressOfCharPP, POINTER(POINTER(ArrayType))
    ptr[0] = charPP

希望这可以帮助某人...

4

0 回答 0