繁体   English   中英

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

[英]Python ctypes: how to pass ctypes array to DLL?

使用ctypes从Python调用dll ,我想将ctypes数组传递给dll 这是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))

最后一行抛出错误:

“ TypeError:必须为实数,而不是_ctypes.PyCArrayType。”

变量t显示其类型为ctypes.PyCArrayType 变量ReturnVec显示其类型为c_floatarray_1000 (其中1000是数组的长度)。

我尝试将其转换为float

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

但它返回:

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

我尝试将其强制转换为指针,但得到相同的结果:

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

我对此进行了详尽的研究,并且在这个问题上有很多话题,但是没有人描述我的情况。

这是我上面的问题的答案:

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类型 ,因此“必须是实数”错误。 您需要一个类型的实例:

ReturnVecInstance = ReturnVec()

暂无
暂无

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

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