我正在尝试为第三方C DLL编写Python包装器。
函数WolaInit
初始化库并返回一个句柄,用于后续函数调用。
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)
但是,在使用返回的句柄时,会发生访问冲突(访问冲突的地址对应于句柄值加上一个额外的偏移量)。
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
这种访问冲突的原因是什么?如何解决?
答案 0 :(得分:0)
句柄通常实现为指针。在64位系统上,指针是64位。在Windows上,c_ulong
是32位。我认为你正在截断句柄,但没有看到函数原型,它只是一个理论。 c_void_p
可能是句柄更合适的类型。