import ctypes
from ctypes import wintypes

def main():
    count = 0
    def cb(hwnd, lparam):
        nonlocal count
        length = ctypes.windll.user32.GetWindowTextLengthW(hwnd)
        if length > 0:
            buff = ctypes.create_unicode_buffer(length + 1)
            ctypes.windll.user32.GetWindowTextW(hwnd, buff, length + 1)
            title = buff.value
            if ctypes.windll.user32.IsWindowVisible(hwnd) and len(title.strip()) > 0:
                pid = ctypes.c_ulong()
                ctypes.windll.user32.GetWindowThreadProcessId(hwnd, ctypes.byref(pid))
                print(f"[{count}] HWND: {hex(hwnd)} | PID: {pid.value} | Title: {title[:50]}")
                count += 1
        return True

    WNDENUMPROC = ctypes.WINFUNCTYPE(wintypes.BOOL, wintypes.HWND, wintypes.LPARAM)
    ctypes.windll.user32.EnumWindows(WNDENUMPROC(cb), 0)
    print(f"Total visible windows with title: {count}")

if __name__ == "__main__":
    main()
