简体   繁体   中英

Python ctypes: how to pass ctypes array to DLL?

Calling a dll from Python using ctypes , I want to pass a ctypes array to the dll . Here is the Python code:

ReturnVec = ctypes.c_float * len(arrA)
t = type(ReturnVec)
hllDll = ctypes.WinDLL(r"C:/Temp2/Test_Project_3B/Std_Math_Formulas.dll")
SimpleTest = hllDll.SimpleTest
SimpleTest.argtypes = [ctypes.c_void_p, ctypes.POINTER(ctypes.c_float)]
SimpleTest.restype = ctypes.c_void_p
retvar = SimpleTest(ctypes.byref(pvarr),ctypes.c_float(ReturnVec))

The final line throws the error:

"TypeError: must be real number, not _ctypes.PyCArrayType."

The variable t shows that its type is ctypes.PyCArrayType . The variable ReturnVec shows that its type is c_floatarray_1000 (where 1000 is the length of the array).

I try to cast it to a float :

aq = ctypes.cast(ReturnVec, ctypes.c_float)

but it returns:

"<class 'TypeError'>: wrong type"

I try to cast it to a pointer, I get the same thing:

floatPtr = ctypes.cast(ReturnVec, ctypes.POINTER(ctypes.c_float))

I've researched this at length, and there are many threads on this issue, but none described my situation.

Here is the answer to my question above:

SimpleTest = hllDll.SimpleTest

SimpleTest.argtypes = [ctypes.c_void_p, ctypes.c_void_p]
SimpleTest.restype = ctypes.c_int64

retvar = SimpleTest(ctypes.byref(pvarr),ctypes.byref(arrA))

ReturnVec is a type , hence the "must be a real number" error. You need an instance of the type:

ReturnVecInstance = ReturnVec()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM