2014-08-28 105 views
0

我試圖從Python中的外部DLL調用函數。從外部DLL傳遞數組參數到Python

功能的原型如下:

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

該函數創建UINT64的表用 「ARRAYSIZE」 元素。這個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 

我想,我沒有通過正確的參數給功能,但我無法弄清楚。

我試圖從labview dll像uint64那樣分類簡單的數據,並且工作正常;但只要我嘗試傳遞uint64數組,我就卡住了。

任何幫助將不勝感激。

回答

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]