import ctypes, time
from ctypes import wintypes
import win32gui, win32con, win32process

user32 = ctypes.windll.user32
hdesk = user32.OpenInputDesktop(0, False, 0x01FF)
if hdesk:
    user32.SetThreadDesktop(hdesk)

popups = []
def enum_cb(hwnd, _):
    if win32gui.IsWindow(hwnd) and win32gui.IsWindowVisible(hwnd):
        title = win32gui.GetWindowText(hwnd)
        cls = win32gui.GetClassName(hwnd)
        if title == "Error" and cls == "#32770":
            _, pid = win32process.GetWindowThreadProcessId(hwnd)
            popups.append((hwnd, pid))
win32gui.EnumWindows(enum_cb, None)

print(f"Found {len(popups)} Error popups:")
for hwnd, pid in popups:
    print(f"Dismissing popup HWND: {hex(hwnd)}, PID: {pid}")
    # Click OK button (ID 1)
    btn = win32gui.GetDlgItem(hwnd, 1)
    if btn:
        win32gui.PostMessage(btn, win32con.BM_CLICK, 0, 0)
    win32gui.PostMessage(hwnd, win32con.WM_COMMAND, 1, 0)

time.sleep(1)
from PIL import ImageGrab
im = ImageGrab.grab()
im.save(r"c:\Projects\JX\logs\real_desktop_after_dismiss.png")
print("Saved real_desktop_after_dismiss.png")
