虽然我正在寻找解决这个特殊问题的方法,但我也在寻找关于使用ctypes的更一般的建议,因为文档和过程似乎有点单薄。
我有以下c函数:
extern "C" {
void f( int* array, int arraylen ) {
for(int i = 0; i < arraylen; i++) {
array[i] = g() // mutate the value array[i];
}
}
}
以及以下python代码:
import ctypes
plib = ctypes.cdll.LoadLibrary('./mylib.so')
_f = plib.f
_f.restype = None
_f.argtypes = [ ctypes.POINTER(ctypes.c_int), ctypes.c_int ]
seqlen = 50
buffer = ctypes.c_int * seqlen
_f( buffer, seqlen )
但是,此代码段会随着以下回溯而消亡:
Traceback (most recent call last):
File "particle.py", line 9, in <module>
_f( buffer, seqlen )
ctypes.ArgumentError: argument 1: <type 'exceptions.TypeError'>: expected LP_c_int instance instead of _ctypes.ArrayType