2

I am trying to write a Python wrapper for a 3rd-party C DLL.

The function WolaInit initializes the library and returns a handle to be used for subsequent function calls.

import ctypes

# Load WOLA DLL into memory.
wolaDLL = ctypes.WinDLL("wola.dll")

# Function prototypes
WolaInit = wolaDLL.WolaInit
WolaInit.restype = ctypes.c_ulong
WolaInit.argtypes = [
        ctypes.c_int,              # La
        ctypes.c_int,              # Ls
        ctypes.c_int,              # R
        ctypes.c_int,              # N
        ctypes.c_int]              # stacking

WolaGetStacking = wolaDLL.WolaGetStacking
WolaGetStacking.restype = ctypes.c_int
WolaGetStacking.argtypes = [ctypes.c_ulong]

# Parameters
La = 128
Ls = 64
R = 32
N = 8
stacking = 0

# Initialize
wolaHandle = WolaInit(La, Ls, R, N, stacking)
print('Handle: ' + hex(wolaHandle))

# Test if library was initialized
stackingVal = WolaGetStacking(wolaHandle)

However when using the returned handle, an access violation occurs (the address of the access violation corresponds to the handle value plus an additional offset).

Handle: 0x3fe22310

Traceback (most recent call last):

  File "<ipython-input-47-5ba1f23d5c33>", line 1, in <module>
    runfile('D:/Projects/Desyncra_611110600/Simulations/WOLA/wola.py', wdir='D:/Projects/Desyncra_611110600/Simulations/WOLA')

  File "C:\ProgramData\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 710, in runfile
    execfile(filename, namespace)

  File "C:\ProgramData\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 101, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "D:/Projects/Desyncra_611110600/Simulations/WOLA/wola.py", line 69, in <module>
    stackingVal = WolaGetStacking(wolaHandle)

OSError: exception: access violation reading 0x000000003FE22328

What could be the reason for this access violation and how to resolve it?

LaMontagne
  • 55
  • 4
  • Do you have the source code for the 2 functions? What is the *Python* version used? Is the *.dll* available? If not some documentation about its API (or the associated header (*.h*) file)? – CristiFati Jan 26 '18 at 12:56
  • No, unfortunately I only have the header file (which I cannot distribute, but the function prototypes correspond to the ones stated). Python version is 3.6.3, 64bit – LaMontagne Jan 26 '18 at 13:04
  • Can you then share the function declarations? Also, if you call the 2 functions from *C* what is the output? What *C* runtime was the *.dll* built with? – CristiFati Jan 26 '18 at 14:18

1 Answers1

0

Handles are typically implemented as pointers. On 64-bit systems, pointers are 64-bit. On Windows, c_ulong is 32-bits. I think you are truncating the handle, but without seeing the function prototypes it is only a theory. c_void_p may be a more appropriate type for the handle.

Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251