ctypes.ArgumentError:不知道如何转换参数

pfc*_*pfc 6 c python ctypes python-c-extension

我在 C 库中定义了一个函数,如下所示:

int* Test(char *str1,int id1,char *str2,float val,float *ls)
Run Code Online (Sandbox Code Playgroud)

我想在python中使用它,所以我编写了以下python代码:

str1 = 'a'
str2 = 'b'
id1 = 0
val = 1.0
system('g++ -c -fPIC libtraj.cpp -o test.o')
system('g++ -shared -Wl,-soname,test.so -o test.so test.o')
lib = cdll.LoadLibrary('./test.so')
num_item = 100000
ls = (ctypes.c_float * num_item)()
lib.Test.restype = ndpointer(dtype=ctypes.c_int, shape=(num_item,))
lib.Test.argtypes = [c_char_p]
a = create_string_buffer(str1)
b = create_string_buffer(str2)
ls_id = lib.Test(a,id1,b,val,ctypes.byref(ls))
Run Code Online (Sandbox Code Playgroud)

然后我运行这个python程序。我遇到错误说:

ls_id = lib.Test(a,id1,b,val,ctypes.byref(ls))
ctypes.ArgumentError: argument 4: <type 'exceptions.TypeError'>: Don't know how to convert parameter 4
Run Code Online (Sandbox Code Playgroud)

我的代码有什么问题?谢谢大家帮助我!!!

pfc*_*pfc 9

@CristiFati 提供的解决方案解决了我的问题。我只是复制他在评论中的答案并在这里展示。谢谢@CristiFati。

尝试:

lib.Test.argtypes = [c_char_p, c_int, c_char_p, c_float, POINTER(c_float)] # (all defined by ctypes)