import ctypes
import ctypes.wintypes as wt
import os
import time

user32 = ctypes.windll.user32
kernel32 = ctypes.windll.kernel32

hdesk_def = user32.OpenDesktopW("Default", 0, False, 0x01FF)
if hdesk_def:
    user32.SetThreadDesktop(hdesk_def)

print("--- Visible windows right now ---")
windows = []
def enum_cb(h, _):
    if user32.IsWindow(h) and user32.IsWindowVisible(h):
        cb_buf = ctypes.create_unicode_buffer(256)
        user32.GetClassNameW(h, cb_buf, 256)
        cls_name = cb_buf.value.lower()

        b = ctypes.create_unicode_buffer(512)
        user32.GetWindowTextW(h, b, 512)
        val = b.value.strip()

        w_pid = wt.DWORD()
        user32.GetWindowThreadProcessId(h, ctypes.byref(w_pid))
        
        exe_file = ""
        h_proc = kernel32.OpenProcess(0x1000, False, w_pid.value)
        if h_proc:
            buf_path = ctypes.create_unicode_buffer(1024)
            sz = wt.DWORD(1024)
            if kernel32.QueryFullProcessImageNameW(h_proc, 0, buf_path, ctypes.byref(sz)):
                exe_file = os.path.basename(buf_path.value).lower()
            kernel32.CloseHandle(h_proc)

        if val:
            windows.append((h, cls_name, val, exe_file))
    return True

WNDENUMPROC = ctypes.WINFUNCTYPE(ctypes.c_bool, wt.HWND, wt.LPARAM)
user32.EnumWindows(WNDENUMPROC(enum_cb), 0)

for h, cls, val, exe in windows:
    print(f"HWND: {h}, Class: '{cls}', Exe: '{exe}', Title: '{val}'")
