简体   繁体   English

python ctypes数组将无法正确返回

[英]python ctypes array will not return properly

This does not work: 这不起作用:

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

I get this error ( n_export is 3): 我收到此错误( n_export为3):

TypeError: 'c_double_Array_3' object is not callable

Error is pretty self-explainatory. 错误是不言而喻的。 export_array is not a callable object, but you try to call it in last line of function. export_array不是可调用的对象,但是您尝试在函数的最后一行中调用它。 Also, you try to use pointer-related interface ('.contents') to retrieve value from array, not pointer to it. 另外,您尝试使用与指针相关的接口('.contents')从数组中检索值,而不是从数组中获取指针。

Simplest way to make it work would be to convert ctypes result array to pure Python object and return it: 使它工作的最简单方法是将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