Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/132.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用python调用Dll,而Dll返回字符串*(或字符**) 我想使用Python调用C++,所以我构建了一个新的Dll项目。我想用python打印string[]。但现在我可以打印int,char*(如下所示,它是:“我爱我的pan!”)。 那么,如何打印其他参数_Python_C++_Dll - Fatal编程技术网

使用python调用Dll,而Dll返回字符串*(或字符**) 我想使用Python调用C++,所以我构建了一个新的Dll项目。我想用python打印string[]。但现在我可以打印int,char*(如下所示,它是:“我爱我的pan!”)。 那么,如何打印其他参数

使用python调用Dll,而Dll返回字符串*(或字符**) 我想使用Python调用C++,所以我构建了一个新的Dll项目。我想用python打印string[]。但现在我可以打印int,char*(如下所示,它是:“我爱我的pan!”)。 那么,如何打印其他参数,python,c++,dll,Python,C++,Dll,你好,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 HELLO_API int IntAdd(int a, int b) { retu

你好,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

        HELLO_API int IntAdd(int a, int b)
        {   return a + b;
        }
        HELLO_API char* show()
        {
            return "I love U";
        }
        HELLO_API string* call_pic(char* pic)
        {   
            string a[] = {"show example","search picture","I write python " };//just to show an example
            return a;
        } //stack overflow tell me must add comment
        HELLO_API char** call_char()
        {
           char* a[] = { "I love myPan", "Love is wonderful ", "You are only love" };
           return a;
        }
下面是我的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!

call\u pic
call\u char
函数中返回一个指向局部变量的指针。这是未定义的行为。调用这些函数时,您必须返回未超出范围的内容,简单明了。但我需要一个字符串[](或包含python中字符串的列表),我该怎么做?您能给我简单的代码吗?谢谢。但我需要一根绳子…——不管是什么情况,通过返回指向局部变量的指针都无法完成所做的工作。它甚至不是一个Python问题,为什么你的代码不能工作——如果你纯粹是在C++中工作,你的代码甚至不会工作。所以也许你应该寻找其他材料来演示如何将字符串数据从C++发送到Python?谢谢。我会按你的话尝试另一种语言。