Last active
June 7, 2021 20:56
-
-
Save Auax/9a56bf03543d7c452c43a237af0af243 to your computer and use it in GitHub Desktop.
Get all window names from Windows 10 and path to executable
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import sys | |
| import win32gui | |
| import win32api | |
| import win32process | |
| import win32con | |
| import os | |
| if not os.name == "nt": | |
| print("Only available in Windows.") | |
| sys.exit(-1) | |
| os.system("cls") | |
| processes = {} | |
| def winEnumHandler(hwnd, ctx): | |
| global processes | |
| if win32gui.IsWindowVisible(hwnd): | |
| pname = win32gui.GetWindowText(hwnd) | |
| processes[pname if pname else None] = hwnd | |
| a = win32gui.EnumWindows(winEnumHandler, None) | |
| print("{: <12}{: <40} {: >100}\n".format("HWND", "Name", "Path")) | |
| for pname_, hwnd_ in processes.items(): | |
| pid = win32process.GetWindowThreadProcessId(hwnd_) | |
| handle = win32api.OpenProcess(win32con.PROCESS_QUERY_INFORMATION | win32con.PROCESS_VM_READ, False, pid[1]) | |
| proc_name = win32process.GetModuleFileNameEx(handle, 0) | |
| pname_ = str(pname_) if len(str(pname_)) < 37 else str(pname_)[:37] + "..." | |
| row = [str(hwnd_), pname_, str(proc_name)] | |
| print("{: <12}{: <40} {: >100}".format(*row)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment