【问题标题】:Python ctypes: how to pass ctypes array to DLL?Python ctypes:如何将 ctypes 数组传递给 DLL?
【发布时间】:2018-01-01 00:00:29
【问题描述】:

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

但它会返回:

": 类型错误"

我尝试将它转换为指针,我得到了同样的结果:

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

我对此进行了详细研究,关于这个问题有很多线程,但没有一个描述我的情况。

【问题讨论】:

    标签: python ctypes


    【解决方案1】:

    这是我上面问题的答案:

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

    【讨论】:

    • 这不是一个完整的答案。什么是pvarrarrA(问题中也没有定义)?
    【解决方案2】:

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

    ReturnVecInstance = ReturnVec()
    

    【讨论】:

      猜你喜欢
      • 2021-09-28
      • 1970-01-01
      • 2015-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-27
      • 2011-05-11
      相关资源
      最近更新 更多