import ctypes, sys
from ctypes import wintypes

sys.stdout.reconfigure(encoding='utf-8')
user32 = ctypes.windll.user32
hdesk = user32.OpenInputDesktop(0, False, 0x01FF)
if hdesk:
    user32.SetThreadDesktop(hdesk)

res = []
def cb(h, _):
    cls_buf = ctypes.create_unicode_buffer(256)
    user32.GetClassNameW(h, cls_buf, 256)
    txt_len = user32.GetWindowTextLengthW(h) + 1
    txt_buf = ctypes.create_unicode_buffer(txt_len)
    user32.GetWindowTextW(h, txt_buf, txt_len)
    if 'sword' in cls_buf.value.lower() or 'game' in cls_buf.value.lower() or 'vo lam' in txt_buf.value.lower():
        pid = wintypes.DWORD()
        user32.GetWindowThreadProcessId(h, ctypes.byref(pid))
        vis = user32.IsWindowVisible(h)
        res.append((h, pid.value, cls_buf.value, txt_buf.value, vis))
    return True

WNDENUMPROC = ctypes.WINFUNCTYPE(wintypes.BOOL, wintypes.HWND, wintypes.LPARAM)
user32.EnumWindows(WNDENUMPROC(cb), 0)
for h, pid, cls, txt, vis in res:
    print(f'{hex(h)} | PID: {pid} | {cls} | "{txt}" | Visible: {vis}')
if not res:
    print('No sword/game windows found.')
