使用python调用Dll,而Dll返回string*(或char**)

2024-09-25 00:34:53 发布

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

我想使用Python调用C++,所以我构建了一个新的Dll项目。{cd1>想在python中打印。但现在我可以打印intchar*(如下所示,它是:“我爱myPan!”)。 那么,如何打印其他参数?在

你好。h

    extern "C"
    {   HELLO_API int IntAdd(int, int);
        HELLO_API char* show();
        HELLO_API char** call_char();
        HELLO_API string* call_pic(char* pic);
    }

在你好.cpp在

^{pr2}$

下面是我的python代码:

        import ctypes  
        from ctypes import *  
        func = cdll.LoadLibrary('Hello.dll')
        ret = func.IntAdd(6,4)
        print(ret) //print  10
        str0 = func.show()
        size = -1
        str0 = ctypes.string_at(str0,size)
        print(str0)  //print  b"I love U"
        str1 = func.call_pic('22.jpg')
        str1=ctypes.string_at(str1,size)
        print(str1)  //Cann't Work!
        print(str1.decode('utf-8'))  // Cann't Work!
        str2 = func.call_char()
        print(str2.value)     //Cann't Work!
        str3 =ctypes.string_at(str2[0],size)
        print(str2.decode('utf-8'))  // Cann't Work!

Tags: apihellosizestringcallctypesintwork