【问题标题】:Pass array parameter from external dll to Python将数组参数从外部 dll 传递给 Python
【发布时间】:2014-08-28 15:19:30
【问题描述】:

我尝试在 Python 中从外部 DLL 调用函数。

函数原型为:

void Myfunction(int32_t *ArraySize, uint64_t XmemData[])

此函数创建一个包含“ArraySize”元素的 uint64 表。这个dll是labview生成的。

以下是调用此函数的 Python 代码:

import ctypes

# Load the library
dllhandle = ctypes.CDLL("SharedLib.dll")

#specify the parameter and return types 
dllhandle.Myfunction.argtypes = [ctypes.c_int,ctypes.POINTER(ctypes.c_uint64)]

# Next, set the return types...
dllhandle.Myfunction.restype = None

#convert our Python data into C data
Array_Size = ctypes.c_int(10)
Array = (ctypes.c_uint64 * Array_Size.value)()

# Call function
dllhandle.Myfunction(Array_Size,Array)
for index, value in enumerate(Array):
print Array[index]

执行此操作时出现错误代码:

dllhandle.ReadXmemBlock(Array_Size,Array)
WindowsError: exception: access violation reading 0x0000000A

我猜我没有正确地将参数传递给函数,但我无法弄清楚。

我尝试像 uint64 一样对来自 labview dll 的简单数据进行排序,效果很好;但是一旦我尝试传递 uint64 数组,我就卡住了。

任何帮助将不胜感激。

【问题讨论】:

    标签: arrays dll parameters ctypes


    【解决方案1】:

    看起来它正在尝试访问内存地址 0x0000000A(即 10)。这是因为您传递的是 int 而不是指向 int 的指针(尽管那仍然是 int),并且您正在使 int = 10。

    我会开始:

    import ctypes
    
    # Load the library
    dllhandle = ctypes.CDLL("SharedLib.dll")
    
    #specify the parameter and return types 
    dllhandle.Myfunction.argtypes = [POINTER(ctypes.c_int),    # make this a pointer
                                     ctypes.c_uint64 * 10]
    
    # Next, set the return types...
    dllhandle.Myfunction.restype = None
    
    #convert our Python data into C data
    Array_Size = ctypes.c_int(10)
    Array = (ctypes.c_uint64 * Array_Size.value)()
    
    # Call function
    dllhandle.Myfunction(byref(Array_Size), Array)    # pass pointer using byref
    for index, value in enumerate(Array):
    print Array[index]
    

    【讨论】:

      猜你喜欢
      • 2021-01-29
      • 2020-10-09
      • 1970-01-01
      • 1970-01-01
      • 2019-05-30
      • 1970-01-01
      • 1970-01-01
      • 2013-01-13
      • 1970-01-01
      相关资源
      最近更新 更多