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.

1 Answer 1

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]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.