繁体   English   中英

python ctypes数组将无法正确返回

[英]python ctypes array will not return properly

这不起作用:

def CopyExportVars(self, n_export):
    export_array = (ctypes.c_double * n_export)()
    self.dll_handle.vs_copy_export_vars(ctypes.cast(export_array,  ctypes.POINTER(ctypes.c_double)))
    return export_array().contents

我收到此错误( n_export为3):

TypeError: 'c_double_Array_3' object is not callable

错误是不言而喻的。 export_array不是可调用的对象,但是您尝试在函数的最后一行中调用它。 另外,您尝试使用与指针相关的接口('.contents')从数组中检索值,而不是从数组中获取指针。

使它工作的最简单方法是将ctypes结果数组转换为纯Python对象并返回它:

def CopyExportVars(self, n_export):
    export_array = (ctypes.c_double * n_export)()
    self.dll_handle.vs_copy_export_vars(ctypes.cast(export_array,  ctypes.POINTER(ctypes.c_double)))
    return list(export_array)

暂无
暂无

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

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