Re: [ctypes-users] finding PID through handle with Ctypes
Brought to you by:
theller
From: Michael C <mys...@gm...> - 2017-09-24 20:08:41
|
I tried this, but it returns zero, is it because I need to initiate the variable, PID, in DWORD, somehow? thanks! > code import ctypes import os import time User32 = ctypes.WinDLL('User32', use_last_error=True) Kernel32 = ctypes.WinDLL('Kernel32', use_last_error=True) window_name = 'Star Wars™: The Old Republic™' handle = User32.FindWindowW(None, window_name ) print(handle) PID = 0 print(User32.GetWindowThreadProcessId(handle, PID)) print(PID) On Mon, Aug 14, 2017 at 10:07 PM, eryk sun <er...@gm...> wrote: > 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 > |