我试图使用ctypes在python中列出一个包含FindFirstFileW和FindNextFileW的目录。FindFirstFileW成功,但FindNextFileW或FindClose导致操作错误:异常:访问冲突写入0xFFFFFFFFB8093F80
代码如下:
def assert_success(success):
if not success:
raise AssertionError(FormatError())
def assert_handle(handle):
if handle == INVALID_HANDLE_VALUE:
raise AssertionError(FormatError())
def FindFirstFileW(lpFileName):
wfd = WIN32_FIND_DATAW()
handle = windll.kernel32.FindFirstFileW(lpFileName, byref(wfd))
assert_handle(handle)
return handle, wfd
def FindNextFileW(handle):
wfd = WIN32_FIND_DATAW()
success = windll.kernel32.FindNextFileW(handle, byref(wfd))
assert_success(success)
return wfd
def FindClose(handle):
success = windll.kernel32.FindClose(handle)
assert_success(success)
return success
handle, wfd = FindFirstFileW('C:\\Windows\\*')
print(wfd.cFileName)
wfd = FindNextFileW(handle)
print(wfd.cFileName)
FindClose(handle)
您没有为正在使用的函数适当地设置
.argtypes
和.restype
ctypes
假设返回值是c_int
,但在64位Python上句柄是64位的,并且被截断为32位。另外,由于ctypes
知道参数类型应该是什么,因此您还可以通过类型检查调用获得额外的好处还建议
.errcheck
自动检查返回值:试试这个:
相关问题 更多 >
编程相关推荐