Re: finding PID through handle with Ctypes
Michael C <[email protected]> Sun, 24 Sep 2017 13:08:34 -0700
| Newsgroups | gmane.comp.python.ctypes |
|---|---|
| Message-ID | <CANyKM1h4NPc9xvH9Nsn5+RN2QTyP7Pmt5nwqA1_r211dOYTsuw@mail.gmail.com> |
--===============6028250129081753995==
Content-Type: multipart/alternative; boundary="001a113d6ab6c7311a0559f50129"
--001a113d6ab6c7311a0559f50129
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
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 =3D ctypes.WinDLL('User32', use_last_error=3DTrue)
Kernel32 =3D ctypes.WinDLL('Kernel32', use_last_error=3DTrue)
window_name =3D 'Star Wars=E2=84=A2: The Old Republic=E2=84=A2'
handle =3D User32.FindWindowW(None, window_name )
print(handle)
PID =3D 0
print(User32.GetWindowThreadProcessId(handle, PID))
print(PID)
On Mon, Aug 14, 2017 at 10:07 PM, eryk sun <[email protected]> wrote:
> On Mon, Aug 14, 2017 at 7:57 PM, Michael C
> <[email protected]> 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 =3D 'chrome.exe'
> > User32 =3D ctypes.WinDLL('User32', use_last_error=3DTrue)
> > Kernel32 =3D ctypes.WinDLL('Kernel32', use_last_error=3DTrue)
> > handle =3D 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
>
--001a113d6ab6c7311a0559f50129
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">I tried this, but it returns zero, is it because I need to=
initiate the variable, PID, in DWORD, somehow?<div><br></div><div>thanks!<=
/div><div><br></div><div>> code</div><div><br></div><div><div>import cty=
pes</div><div>import os</div><div>import time</div><div><br></div><div>User=
32 =3D ctypes.WinDLL('User32', use_last_error=3DTrue)</div><div>Ker=
nel32 =3D ctypes.WinDLL('Kernel32', use_last_error=3DTrue)</div><di=
v>window_name =3D 'Star Wars=E2=84=A2: The Old Republic=E2=84=A2'</=
div><div>handle =3D User32.FindWindowW(None, window_name )</div><div><br></=
div><div>print(handle)</div></div><div><br></div><div><div>PID =3D 0</div><=
div>print(User32.GetWindowThreadProcessId(handle, PID))</div><div>print(PID=
)</div></div><div><br></div><div><br></div><div><br></div><div><br></div><d=
iv><br></div><div><br></div><div><br></div><div><br></div><div><br></div></=
div><div class=3D"gmail_extra"><br><div class=3D"gmail_quote">On Mon, Aug 1=
4, 2017 at 10:07 PM, eryk sun <span dir=3D"ltr"><<a href=3D"mailto:eryks=
[email protected]" target=3D"_blank">[email protected]</a>></span> wrote:<br>=
<blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p=
x #ccc solid;padding-left:1ex"><span class=3D"">On Mon, Aug 14, 2017 at 7:5=
7 PM, Michael C<br>
<<a href=3D"mailto:[email protected]">mysecretrobotfactory@=
gmail.<wbr>com</a>> wrote:<br>
><br>
> I am trying to get the PID of a process given its name. I acquire the<=
br>
> handle, and then feed it to the ctypes function hoping it would return=
the<br>
> PID, but it returns 0!<br>
><br>
> import ctypes<br>
><br>
> window_name =3D 'chrome.exe'<br>
> User32 =3D ctypes.WinDLL('User32', use_last_error=3DTrue)<br>
> Kernel32 =3D ctypes.WinDLL('Kernel32', use_last_error=3DTrue)<=
br>
> handle =3D User32.FindWindowW(None, window_name )<br>
> print(Kernel32.GetProcessId(<wbr>handle))<br>
<br>
</span>GetProcessId [1] takes a kernel Process handle, but you're passi=
ng it<br>
a user Window handle. Ask the window manager instead. Every Window is<br>
owned by a Thread, and every Thread belongs to a Process.<br>
user32.<wbr>GetWindowThreadProcessId [2] returns both identifiers. The<br>
second argument to this function is a pointer to a DWORD, to which it<br>
writes the Process ID.<br>
<br>
[1]: <a href=3D"https://msdn.microsoft.com/en-us/library/ms683215" rel=3D"n=
oreferrer" target=3D"_blank">https://msdn.microsoft.com/en-<wbr>us/library/=
ms683215</a><br>
[2]: <a href=3D"https://msdn.microsoft.com/en-us/library/ms633522" rel=3D"n=
oreferrer" target=3D"_blank">https://msdn.microsoft.com/en-<wbr>us/library/=
ms633522</a><br>
</blockquote></div><br></div>
--001a113d6ab6c7311a0559f50129--
--===============6028250129081753995==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
--===============6028250129081753995==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
_______________________________________________
ctypes-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ctypes-users
--===============6028250129081753995==--