如何将char **从dll回调传递给Python,通过ctypes返回给dll?

2024-09-28 16:58:07 发布

您现在位置:Python中文网/ 问答频道 /正文

这里是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);

以及

^{pr2}$

经过将近一天的google搜索、阅读晦涩难懂的文档、玩弄代码和内核转储,下面是相应的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

希望这对某人有帮助。。。在


Tags: 代码fromimportdefctypesarg1arg2char