Created
March 29, 2023 11:02
-
-
Save ra101/f9baa2f0bf2d16395088f695eed3e638 to your computer and use it in GitHub Desktop.
get exe name of foreground window
This file contains 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
from os.path import basename as get_base_name | |
from ctypes import wintypes, windll, create_unicode_buffer, byref | |
def getActiveWindowExe(): | |
# PROCESS_QUERY_INFORMATION, PROCESS_VM_READ, BUF_LEN = 0x0400, 0x0010, 260 | |
hwnd, pid = windll.user32.GetForegroundWindow(), wintypes.DWORD() | |
tid = windll.user32.GetWindowThreadProcessId(hwnd, byref(pid)) | |
handle = windll.kernel32.OpenProcess(0x0400 | 0x0010, False, pid) | |
buf = create_unicode_buffer(260) | |
windll.psapi.GetModuleFileNameExW(handle, 0, buf, 260) | |
exe_name = get_base_name(buf.value) | |
windll.kernel32.CloseHandle(handle) | |
return exe_name if exe_name else None |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment