简体   繁体   中英

Issue finding exe path of all windows in python

I am using the code below to determine the foreground window and find the path for 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 workd for windows such as Google Chrome, PyCharm, Filezilla etc, but the line

path = win32process.GetModuleFileNameEx(hndl, 0)

is throwing the error

pywintypes.error: (299, 'GetModuleFileNameEx', 'Only part of a ReadProcessMemory or WriteProcessMemory request was completed.')

for windows explorer, calculator, command prompt etc

Im relatively new to coding and python and cant work out why this is the case, and what the difference is.

That error indicates that you are executing 32 bit code in the WOW64 emulator on 64 bit Windows, and trying to gain information about a 64 bit process.

To get past this you should switch to running 64 bit code. So, you'll need 64 bit Python.

You can use psutil module to get path.

hwnd = win32gui.GetForegroundWindow()
_, pid = win32process.GetWindowThreadProcessId(hwnd)
path = psutil.Process(pid).exe()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM