How to get the application name having the process name using python?

Viewed 176

I want to get the name of running application having the process name for e.g. pycharm64.exe-->Pycharm or chrome.exe --> Google chrome

Here's what I have achieved so far

import wmi
c = wmi.WMI()

def get_process_name(hwnd):
"""Get applicatin filename given hwnd."""
try:
    _, pid = win32process.GetWindowThreadProcessId(hwnd)

    for p in c.query('SELECT Name FROM Win32_Process WHERE ProcessId = %s' % str(pid)):
        exe = p.Name
        # print(GetFileVersionInfo(exe))
        break
    return exe
except:
    return None


while True :
time.sleep(3) 
print(get_process_name(win32gui.GetForegroundWindow()))
0 Answers
Related