Skip to content

Commit 61b482b

Browse files
fix(windows): handle hwnd==0 gracefully for UAC/secure desktop (#118)
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 #90
1 parent e202578 commit 61b482b

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

aw_watcher_window/lib.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,20 @@ def get_current_window_windows() -> Optional[dict]:
3939
from . import windows
4040

4141
window_handle = windows.get_active_window_handle()
42+
43+
# hwnd 0 means no foreground window (e.g. during UAC prompt, lock screen,
44+
# or secure desktop). Return None so heartbeat_loop skips this poll cycle.
45+
if not window_handle:
46+
return None
47+
4248
try:
4349
app = windows.get_app_name(window_handle)
44-
except Exception: # TODO: narrow down the exception
45-
# try with wmi method
46-
app = windows.get_app_name_wmi(window_handle)
50+
except Exception:
51+
# Fall back to WMI for elevated/admin processes where OpenProcess fails
52+
try:
53+
app = windows.get_app_name_wmi(window_handle)
54+
except Exception:
55+
app = None
4756

4857
title = windows.get_window_title(window_handle)
4958

0 commit comments

Comments
 (0)