import ctypes
from ctypes import wintypes
import win32gui

import sys
sys.stdout.reconfigure(encoding='utf-8')

user32 = ctypes.windll.user32
hdesk = user32.OpenInputDesktop(0, False, 0x01FF)
print(f"hdesk: {hdesk}")

windows = []
def cb(hwnd, lparam):
    length = user32.GetWindowTextLengthW(hwnd) + 1
    buff = ctypes.create_unicode_buffer(length)
    user32.GetWindowTextW(hwnd, buff, length)
    title = buff.value
    
    cls_buf = ctypes.create_unicode_buffer(256)
    user32.GetClassNameW(hwnd, cls_buf, 256)
    
    pid = wintypes.DWORD()
    user32.GetWindowThreadProcessId(hwnd, ctypes.byref(pid))
    vis = user32.IsWindowVisible(hwnd)
    
    if len(title.strip()) > 0 or pid.value == 52500:
        windows.append((hwnd, pid.value, cls_buf.value, title, vis))
    return True

WNDENUMPROC = ctypes.WINFUNCTYPE(wintypes.BOOL, wintypes.HWND, wintypes.LPARAM)
user32.EnumDesktopWindows(hdesk, WNDENUMPROC(cb), 0)

print(f"Total windows found: {len(windows)}")
for h, pid, cls, title, vis in windows:
    if pid == 52500 or "JX" in title or "Auto" in title or "Vo Lam" in title:
        print(f"HWND: {hex(h)} | PID: {pid} | Class: {cls} | Vis: {vis} | Title: '{title}'")
