![](/img/trans.png)
[英]Python and ctypes: how to correctly pass “pointer-to-pointer” into 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.