import ctypes, time, win32gui, win32process, psutil
user32 = ctypes.windll.user32
hdesk = user32.OpenInputDesktop(0, False, 0x01FF)
if hdesk: user32.SetThreadDesktop(hdesk)

# Find launcher window
launcher_hwnd = None
for h in [w for w in range(1) if False]: pass

def cb(h, _):
    global launcher_hwnd
    if win32gui.IsWindowVisible(h):
        _, pid = win32process.GetWindowThreadProcessId(h)
        try:
            if "autoupdate" in psutil.Process(pid).name().lower():
                launcher_hwnd = h
        except Exception:
            pass

win32gui.EnumWindows(cb, None)
with open(r"c:\Projects\JX\logs\launcher_inspect.txt", "w", encoding="utf-8") as f:
    f.write(f"Launcher HWND: {hex(launcher_hwnd) if launcher_hwnd else 'None'}\n")
    if launcher_hwnd:
        f.write(f"Class: {win32gui.GetClassName(launcher_hwnd)}, Title: '{win32gui.GetWindowText(launcher_hwnd)}'\n")
        f.write(f"Rect: {win32gui.GetWindowRect(launcher_hwnd)}\n")
        def c_cb(ch, _):
            cid = user32.GetDlgCtrlID(ch)
            cls = win32gui.GetClassName(ch)
            txt = win32gui.GetWindowText(ch)
            rect = win32gui.GetWindowRect(ch)
            vis = win32gui.IsWindowVisible(ch)
            f.write(f"  Child: HWND 0x{ch:X}, ID {cid}, Class '{cls}', Vis {vis}, Rect {rect}, Text '{txt}'\n")
        win32gui.EnumChildWindows(launcher_hwnd, c_cb, None)
