如何通过ctypes从dll回调传递char **到python并将其返回到dll?

时间:2012-07-18 09:52:04

标签: c++ python c ctypes

这是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

希望这有助于某人...

0 个答案:

没有答案