我想使用python调用C ++,所以我构建了一个新的Dll项目。我可以成功加载DLL!我想在python中打印string[]
。但现在我可以打印int
,char*
(如下图所示,它是:"我爱myPan!")。
那么,我该如何打印其他参数呢?
hello.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);
}
HELLO.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!