我试图通过python中的ctypes库访问具有以下原型的C函数
HandleT OpenFile (const char *FName, ResultT *ReturnCode);
ResultT GetItem (HandleT handle, void **data, int *len);
ResultT是一个枚举
假设我使用的python代码是:
FILE = "E:/LogData/!d1fx/D1F1"
clib = ctypes.CDLL('c_libs\RAWRD64.dll')
data = ctypes.c_void_p(0)
datalen = ctypes.c_int()
result = ctypes.c_int()
handle = ctypes.c_void_p()
handle = clib.OpenFile(FILE, ctypes.byref(result))
#debug output
print ("handle: %d, ret: %d" %(handle, result.value))
clib.GetItem(handle, ctypes.byref(data), ctypes.byref(datalen))
#debug output
print ("result: %d" % (result.value))
print ("data: %s, len: %d" %(data.value, datalen.value))
在Windows7机器上使用Python2.7时,此代码有效。我现在已经迁移到Windows10机器,并在同一步骤中尝试将此代码移植到Python3.8
我遇到的第一个问题是,openFile()返回,无法打开该文件。这可以通过在文件名中添加编码来解决
handle = clib.OpenFile(FILE.encoding("ANSI"), ctypes.byref(result))
但是,GetItem()现在引发以下异常:
handle: 389636160, ret: 0
Traceback (most recent call last):
File ".\test_clib.py", line 19, in <module>
clib.GetItem(handle, ctypes.byref(data), ctypes.byref(datalen))
OSError: exception: access violation reading 0x0000000017396040
为什么这段代码会引发pyhton 3.8的异常,而使用pyhton 2.7?使用ctypes处理DLL的方式有什么不同吗
目前没有回答
相关问题 更多 >
编程相关推荐