Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python ctypes API调用导致访问冲突_Python_Windows_Api_Ctypes - Fatal编程技术网

Python ctypes API调用导致访问冲突

Python ctypes API调用导致访问冲突,python,windows,api,ctypes,Python,Windows,Api,Ctypes,我试图使用ctypes在python中列出一个包含FindFirstFileW和FindNextFileW的目录。FindFirstFileW成功,但FindNextFileW或FindClose导致操作错误:异常:访问冲突写入0xFFFFFFFFB8093F80 代码如下: def assert_success(success): if not success: raise AssertionError(FormatError()) def assert_handle

我试图使用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
<例如,code>ctypes假设返回值是
c_int
,但在64位Python上句柄是64位的,并且被截断为32位。另外,由于
ctypes
知道参数类型应该是什么,因此您还可以获得类型检查调用的额外好处

对于自动检查返回值,还建议使用
.errcheck

试试这个:

从ctypes导入*
从ctypes导入wintypes作为w
无效的\u句柄\u值=w.HANDLE(-1).VALUE
错误\u无\u更多\u文件=18
def布尔检查(结果、函数、参数):
如果没有结果:
raise WinError(获取上次错误()
一无所获
#如果有更多文件,则返回True
#如果没有更多文件,则返回False
#出于其他原因提出例外
def nomorecheck(结果、函数、参数):
如果不是,则返回结果并获取上次错误()错误\u无\u更多\u文件:
raise WinError(获取上次错误()
返回结果
def handlecheck(结果、函数、参数):
如果结果==无效的\u句柄\u值:
raise WinError(获取上次错误()
返回结果
dll=windl('kernel32',use\u last\u error=True)
dll.FindFirstFileW.argtypes=w.LPCWSTR,w.LPWIN32\u FIND\u DATAW
dll.FindFirstFileW.restype=w.HANDLE
dll.FindFirstFileW.errcheck=handlecheck
dll.FindNextFileW.argtypes=w.HANDLE,w.LPWIN32\u FIND\u DATAW
dll.FindNextFileW.restype=w.BOOL
dll.FindClose.errcheck=nomorecheck
dll.FindClose.argtypes=w.HANDLE,
dll.FindClose.restype=w.BOOL
dll.FindClose.errcheck=boolcheck
def find_文件(目录):
wfd=w.WIN32\u FIND\u DATAW()
handle=dll.FindFirstFileW(目录,byref(wfd))
产生wfd.cFileName
而dll.FindNextFileW(句柄,byref(wfd)):
产生wfd.cFileName
dll.FindClose(句柄)
对于查找文件(r'c:\windows\*')中的文件:
打印(文件)

这确实有效。非常感谢。