import ctypes, time, win32gui, win32process, psutil
from PIL import ImageGrab

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

launcher_hwnd = None
for _ in range(15):
    def cb(h, _):
        global launcher_hwnd
        if win32gui.IsWindowVisible(h):
            _, pid = win32process.GetWindowThreadProcessId(h)
            try:
                pname = psutil.Process(pid).name().lower()
                if "autoupdate" in pname:
                    launcher_hwnd = h
            except Exception:
                pass
    win32gui.EnumWindows(cb, None)
    if launcher_hwnd:
        break
    time.sleep(1)

print(f"Launcher HWND: {hex(launcher_hwnd) if launcher_hwnd else 'None'}")
if launcher_hwnd:
    rect = win32gui.GetWindowRect(launcher_hwnd)
    img = ImageGrab.grab(bbox=rect, all_screens=True)
    img.save(r"c:\Projects\JX\logs\launcher_now.png")
    print(f"Saved launcher_now.png, rect: {rect}")
