我不确定 win32gui.EnumWindows 是否正常工作

I'm not sure win32gui.EnumWindows is working properly

我刚刚开始使用 pywin32,所以我对它应该如何工作有点粗略,但是 win32gui.EnumWindows 只是不喜欢它,当我 return 错误从回调中,我不确定为什么。例如,以下崩溃

from win32gui import EnumWindows

def derp(hWnd, lParam):
    return False

EnumWindows(derp, 0)

带回溯

Traceback (most recent call last):
  File "C:\--------\test5.py", line 7, in <module>
    EnumWindows(ewp, 0)
error: (126, 'EnumWindows', 'The specified module could not be found.')

错误代码可能会有所不同,所以我认为它们实际上与 EnumWindows 调用没有任何关系。例如,如果我在我的旧 XP 笔记本电脑上 运行 代码更改为 123 ('The filename, directory name, or volume label syntax is incorrect.'),如果我在 'return False' 之前放置一个打印语句,它会返回错误 0。一切如果我每次都从回调中 return True 并让它循环遍历所有 windows.

就可以正常工作

我的想法是,由于 EnumWindows 的 C 版本 returns false 当回调 returns false 真正出错时,包装器只能看到 return 值并在它变为假时假设最坏的情况?还是其他原因?

(使用 python 2.7.9 和 pywin32 build 219)

我认为 EnumWindows 完全按照其文档所说的那样做。从回调函数返回 False 终止枚举。你的声明 "the wrapper can only sees the return value" 没有任何意义,因为根据文档,这个函数不应该 return 任何东西。为什么不忘记分析 return 值并尝试将对 EnumWindows 的调用包装起来:except: block if you want to suppress the exception.