The callback needs to return TRUE
to continue the enumeration. Your callback is implicitly returning None
, which is false. The following revised version should do what you want:
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()
The HWND
handle type is an alias for c_void_p
. LPARAM
is an integer that's the same storage size as a pointer. It's defined as follows in 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