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

问题描述 投票:0回答:2

使用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))

我已经详细研究了这个问题,在这个问题上有许多线索,但没有描述我的情况。

python ctypes
2个回答
0
投票

以下是我上述问题的答案:

SimpleTest = hllDll.SimpleTest

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

retvar = SimpleTest(ctypes.byref(pharr),ctypes.byref(arrAY))


0
投票

ReturnVec是一种类型,因此“必须是实数”错误。您需要一个类型的实例:

ReturnVecInstance = ReturnVec()
© www.soinside.com 2019 - 2024. All rights reserved.