Pass array parameter from external dll to Python

0

I tried to call a function from external DLL in Python.

The function prototype is:

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

This function creates a table of uint64 with "ArraySize" elements. This dll is generated by labview.

Here is the Python code to call this function:

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]

When executing this I got the error code:

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

I guess that I don't pass correctly the parameters to the function, but I can't figure it out.

I tried to sort simple data from the labview dll like a uint64, and that works fine; but as soon as I tried to pass arrays of uint64 I'm stuck.

Any help will be appreciated.

arrays
dll
parameters
ctypes
asked on Stack Overflow Aug 28, 2014 by PINTO • edited Nov 4, 2014 by J0e3gan

1 Answer

1

It looks like it's trying to access the memory address 0x0000000A (which is 10). This is because you're passing an int instead of a pointer to an int (although that's still an int), and you're making that int = 10.

I'd start with:

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]
answered on Stack Overflow Nov 4, 2014 by 101

User contributions licensed under CC BY-SA 3.0