简体   繁体   English

Python CTYPE:如何将CTYPE数组传递给DLL?

[英]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 . 使用ctypes从Python调用dll ,我想将ctypes数组传递给dll Here is the Python code: 这是Python代码:

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." “ TypeError:必须为实数,而不是_ctypes.PyCArrayType。”

The variable t shows that its type is ctypes.PyCArrayType . 变量t显示其类型为ctypes.PyCArrayType The variable ReturnVec shows that its type is c_floatarray_1000 (where 1000 is the length of the array). 变量ReturnVec显示其类型为c_floatarray_1000 (其中1000是数组的长度)。

I try to cast it to a float : 我尝试将其转换为float

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

but it returns: 但它返回:

"<class 'TypeError'>: wrong type" “ <class'TypeError'>:错误的类型”

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 = hllDll.SimpleTest

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

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

ReturnVec is a type , hence the "must be a real number" error. ReturnVec类型 ,因此“必须是实数”错误。 You need an instance of the type: 您需要一个类型的实例:

ReturnVecInstance = ReturnVec()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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