From 0723a64b01cb77c6b978e24022550fddacd00709 Mon Sep 17 00:00:00 2001 From: Bob Date: Fri, 27 Feb 2026 11:05:02 +0000 Subject: [PATCH] fix(windows): handle hwnd==0 gracefully for UAC/secure desktop When the foreground window handle is 0 (e.g. during UAC prompts, lock screen, or secure desktop), Win32 API calls like OpenProcess fail with "The parameter is incorrect" (error 87). This caused continuous error spam in logs. Fix: - Return None early when hwnd==0, letting heartbeat_loop skip the poll cycle (it already handles None gracefully) - Wrap WMI fallback in try/except so if both win32api and WMI fail for elevated processes, we degrade to "unknown" instead of crashing Fixes ActivityWatch/aw-watcher-window#90 --- aw_watcher_window/lib.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/aw_watcher_window/lib.py b/aw_watcher_window/lib.py index b7699632..ff213102 100644 --- a/aw_watcher_window/lib.py +++ b/aw_watcher_window/lib.py @@ -39,11 +39,20 @@ def get_current_window_windows() -> Optional[dict]: from . import windows window_handle = windows.get_active_window_handle() + + # hwnd 0 means no foreground window (e.g. during UAC prompt, lock screen, + # or secure desktop). Return None so heartbeat_loop skips this poll cycle. + if not window_handle: + return None + try: app = windows.get_app_name(window_handle) - except Exception: # TODO: narrow down the exception - # try with wmi method - app = windows.get_app_name_wmi(window_handle) + except Exception: + # Fall back to WMI for elevated/admin processes where OpenProcess fails + try: + app = windows.get_app_name_wmi(window_handle) + except Exception: + app = None title = windows.get_window_title(window_handle)