我有一个.dll,我正试图使用ctype访问它。.dll的头部如下所示:
typedef void* CMS_HANDLE;
CMS_HANDLE WINAPI cmsOpen( int nDeviceID , int nComType);
BOOL WINAPI cmsClose(CMS_HANDLE hCms);我的python代码如下所示:
from ctypes import WinDLL, c_void_p, c_double, byref
dll = WinDLL("CMS_APIX64.dll")
handle = c_void_p(dll.cmsOpen(15, 3))
print(handle)
dll.cmsClose(handle)它正确地执行dll.cmsOpen并返回一个值。但是,它不喜欢我传递给dll.cmsClose()的值:
c_void_p(18446744072525716064)
Traceback (most recent call last):
File "C:\Users\mjwilson\dev\Cms100-tool\cms100-tool.py", line 40, in <module>
dll.cmsClose(handle)
OSError: exception: access violation reading 0xFFFFFFFFB9701A60我肯定我只是在用手柄做些蠢事。有什么想法吗?(我有使用这个.dll的C代码)
发布于 2021-01-06 17:54:29
问题是在64位Python上,句柄是64位,但是ctype假设返回类型是c_int (32位),如果没有具体定义,那么cmsOpen返回值在放入c_void_p时已经被截断了。
最好的做法是为您使用的每个函数充分定义.argtypes和.restype。这将正确地转换参数,并在传递不正确或错误的参数数时提供错误检查:
from ctypes import *
dll = WinDLL("CMS_APIX64.dll")
dll.cmsOpen.argtypes = c_int,c_int
dll.cmsOpen.restype = c_void_p
dll.cmsClose.argtypes = c_void_p,
dll.cmsClose.restype = c_int
handle = dll.cmsOpen(15, 3)
print(handle)
dll.cmsClose(handle)https://stackoverflow.com/questions/64828437
复制相似问题