import ctypes

import win32con
import win32gui
import win32service

h_desk = win32service.OpenDesktop("Default", 0, False, win32con.GENERIC_ALL)
ctypes.windll.user32.SetThreadDesktop(int(h_desk))

hwnd = 528446
if not win32gui.IsWindow(hwnd):
    print("HWND not valid, searching again...")
    def enum_cb(h, acc):
        if "Discord" in win32gui.GetWindowText(h):
            acc.append(h)
    wins = []
    win32gui.EnumDesktopWindows(int(h_desk), enum_cb, wins)
    hwnd = wins[0] if wins else 0

print(f"Target Discord HWND: {hwnd} ('{win32gui.GetWindowText(hwnd)}')")

# Test UIAutomation COM via ctypes
try:
    import comtypes.client
    _ = comtypes.client
    print("comtypes installed")
except ImportError:
    print("comtypes not installed")
