I successfully called a dll library in my Python code. All the By-Value functions worked smoothly. The problem is that my c function require a pointer of doubles array to return the results in. I could not figure out how to define this array.
from ctypes import *
testlib = cdll.LoadLibrary(".\\testdll.dll")
def wrap_func(lib, funcname, restype, argtypes):
func = lib.__getattr__(funcname)
func.restype = restype
func.argtypes = argtypes
return func
test1 = wrap_func(testlib, 'testfun1', c_double, [c_double, POINTER(c_double), POINTER(c_char)])
test2 = wrap_func(testlib, 'testfun2', c_double, [c_double])
a = 2.5
b = Pointer(c_double)
tstr = Pointer(c_char)
d = test1(a, b, tstr)
print(b.values)
test1 has the problem. test2 worked successfully. The original function test1 n C is:
double testfun1(double x, double* y, char* str)
I expect the output of the function is restored through the array b. The error was:
ctypes.ArgumentError: argument 2: <class 'TypeError'>: expected LP_c_double instance instead of _ctypes.PyCPointerType
Anyone could help me?
testfun1returns an array of doubles iny, how does it know how long the array is?__getattr__isn't meant to be called directly. Usefunc = getattr(lib,funcname)instead.