我试图在Python3.3中使用ctypes获取所有可见窗口的列表
但是使用我的代码,不会返回单个窗口。 EnumWindows
函数失败,返回的错误代码为0。
import ctypes
user32 = ctypes.windll.user32
def worker(hwnd, lParam):
length = user32.GetWindowTextLengthW(hwnd) + 1
buffer = ctypes.create_unicode_buffer(length)
user32.GetWindowTextW(hwnd, buffer, length)
print("Buff: ", repr(buffer.value))
a = ctypes.WINFUNCTYPE(ctypes.c_bool,
ctypes.POINTER(ctypes.c_int),
ctypes.POINTER(ctypes.c_int))(worker)
if not user32.EnumWindows(a, True):
print("Err: ", ctypes.windll.kernel32.GetLastError())
这是当前的输出:
Buff: ''
Err: 0
这就是我的预期:
Buff: 'Python 3.3.2 shell'
Buff: 'test.py - C:\Users\...'
[...]
你能指出我正确的方向来实现这个目标吗?提前谢谢。
答案 0 :(得分:4)
回调需要返回TRUE
才能继续枚举。你的回调是隐式返回None
,这是错误的。以下修订版应该做你想要的:
import ctypes
from ctypes import wintypes
WNDENUMPROC = ctypes.WINFUNCTYPE(wintypes.BOOL,
wintypes.HWND,
wintypes.LPARAM)
user32 = ctypes.windll.user32
user32.EnumWindows.argtypes = [
WNDENUMPROC,
wintypes.LPARAM]
user32.GetWindowTextLengthW.argtypes = [
wintypes.HWND]
user32.GetWindowTextW.argtypes = [
wintypes.HWND,
wintypes.LPWSTR,
ctypes.c_int]
def worker(hwnd, lParam):
length = user32.GetWindowTextLengthW(hwnd) + 1
buffer = ctypes.create_unicode_buffer(length)
user32.GetWindowTextW(hwnd, buffer, length)
print("Buff: ", repr(buffer.value))
return True
cb_worker = WNDENUMPROC(worker)
if not user32.EnumWindows(cb_worker, 42):
raise ctypes.WinError()
HWND
句柄类型是c_void_p
的别名。 LPARAM
是一个整数,与指针的存储大小相同。它在wintypes
:
if ctypes.sizeof(ctypes.c_long) == ctypes.sizeof(ctypes.c_void_p):
WPARAM = ctypes.c_ulong
LPARAM = ctypes.c_long
elif ctypes.sizeof(ctypes.c_longlong) == ctypes.sizeof(ctypes.c_void_p):
WPARAM = ctypes.c_ulonglong
LPARAM = ctypes.c_longlong