Problem finding exe path for all windows in python
I am using the following code to detect the foreground window and find the path to the .exe file that created it.
hwnd = win32gui.GetForegroundWindow() _, pid = win32process.GetWindowThreadProcessId(hwnd) hndl = win32api.OpenProcess(win32con.PROCESS_QUERY_INFORMATION | win32con.PROCESS_VM_READ, 0, pid) path = win32process.GetModuleFileNameEx(hndl, 0) print path
This work for windows like Google Chrome, PyCharm, Filezilla, etc., but the line
path = win32process.GetModuleFileNameEx(hndl, 0)
throws an error
pywintypes.error: (299, 'GetModuleFileNameEx', 'Only part of a ReadProcessMemory or WriteProcessMemory request was completed.')
for Windows browser, calculator, command line, etc.
I'm relatively new to coding and python and can't figure out why this is the case and what is the difference.
+3
source to share