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
user2145312
source
to share
2 answers
This error indicates that you are executing 32-bit code in the WOW64 emulator on 64-bit Windows and trying to get information about the 64-bit process.
To get past this, you have to switch to running 64-bit code. So, you need 64-bit Python.
+1
David Heffernan
source
to share
You can use psutil to get the path.
hwnd = win32gui.GetForegroundWindow() _, pid = win32process.GetWindowThreadProcessId(hwnd) path = psutil.Process(pid).exe()
0
proggeo
source
to share