Python ctypes:访问冲突 [英] Python ctypes: Access violation
问题描述
我正在尝试为第三方C DLL编写Python包装器.
I am trying to write a Python wrapper for a 3rd-party C DLL.
函数 WolaInit
初始化库并返回一个句柄,以用于后续函数调用.
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?
推荐答案
句柄通常实现为指针.在64位系统上,指针是64位的.在Windows上, c_ulong
是32位.我认为您正在截断句柄,但是没有看到函数原型,这只是一个理论. c_void_p
可能是更适合该句柄的类型.
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.
这篇关于Python ctypes:访问冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!