问题
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?
回答1:
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.
来源:https://stackoverflow.com/questions/48461739/python-ctypes-access-violation