Re: [ctypes-users] finding PID through handle with Ctypes
Brought to you by:
theller
From: eryk s. <er...@gm...> - 2017-08-15 05:07:54
|
On Mon, Aug 14, 2017 at 7:57 PM, Michael C <mys...@gm...> wrote: > > I am trying to get the PID of a process given its name. I acquire the > handle, and then feed it to the ctypes function hoping it would return the > PID, but it returns 0! > > import ctypes > > window_name = 'chrome.exe' > User32 = ctypes.WinDLL('User32', use_last_error=True) > Kernel32 = ctypes.WinDLL('Kernel32', use_last_error=True) > handle = User32.FindWindowW(None, window_name ) > print(Kernel32.GetProcessId(handle)) GetProcessId [1] takes a kernel Process handle, but you're passing it a user Window handle. Ask the window manager instead. Every Window is owned by a Thread, and every Thread belongs to a Process. user32.GetWindowThreadProcessId [2] returns both identifiers. The second argument to this function is a pointer to a DWORD, to which it writes the Process ID. [1]: https://msdn.microsoft.com/en-us/library/ms683215 [2]: https://msdn.microsoft.com/en-us/library/ms633522 |