Re: Acquiring process handle and convert it into PID
Michael C <[email protected]> Wed, 30 Nov 2016 15:13:16 -0800
| Newsgroups | gmane.comp.python.ctypes |
|---|---|
| Message-ID | <CANyKM1gOAhd18EbJsZGT1a-eYfMktNQ4oFMkLOr20Rz2=z4Z4w@mail.gmail.com> |
--===============5488925460955336391==
Content-Type: multipart/alternative; boundary=001a1144aa2e90a84305428cd97d
--001a1144aa2e90a84305428cd97d
Content-Type: text/plain; charset=UTF-8
I get it now! Reading MSDN is a merit on its own!
So this is the same thing right?
thx!
import ctypes
import time
time.sleep(2)
user32 = ctypes.WinDLL('user32', use_last_error=True)
hwnd = user32.GetForegroundWindow()
# this part
pid = ctypes.c_ulong()
user32.GetWindowThreadProcessId(hwnd, ctypes.byref(pid))
print(pid.value)
On Wed, Nov 30, 2016 at 8:56 AM, eryk sun <[email protected]> wrote:
> On Wed, Nov 30, 2016 at 4:33 PM, Michael C
> <[email protected]> wrote:
> > Sorry for asking about this over and over again.
> >
> > I ran the code at the bottom and got a
> >
> > user32.GetWindowThreadProcessId(handle, PID)
> > OSError: exception: access violation writing 0x00000001
> >
> > import ctypes
> > import time
> >
> > time.sleep(2)
> > user32 = ctypes.WinDLL('user32', use_last_error=True)
> > handle = user32.GetForegroundWindow()
> > print(handle)
> > PID = 1
> > user32.GetWindowThreadProcessId(handle, PID)
> > print (PID)
>
> Let's review the prototype:
>
> DWORD WINAPI GetWindowThreadProcessId(
> _In_ HWND hWnd,
> _Out_opt_ LPDWORD lpdwProcessId);
>
> lpdwProcessId is an LPDWORD. The LP stands for "long pointer". (Ignore
> the "long" part since it's a vestige of older CPU architectures.) You
> need to pass a pointer to a DWORD. That's a C unsigned long integer,
> so you can use ctypes.c_ulong(). Or use the wintypes module, which
> defines most of the common Windows data types. To pass a pointer you
> can use ctypes.byref. For example:
>
> import ctypes
> from ctypes import wintypes
>
> user32 = ctypes.WinDLL('user32', use_last_error=True)
>
> hwnd = user32.GetForegroundWindow()
> pid = wintypes.DWORD()
> user32.GetWindowThreadProcessId(hwnd, ctypes.byref(pid))
>
> The process ID is in the `value` attribute:
>
> >>> pid.value
> 4832
>
--001a1144aa2e90a84305428cd97d
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">I get it now! Reading MSDN is a merit on its own!<div><br>=
</div><div>So this is the same thing right?</div><div><br></div><div>thx!</=
div><div><br></div><div><br></div><div><div>import ctypes</div><div>import =
time</div><div><br></div><div>time.sleep(2)</div><div><br></div><div>user32=
=3D ctypes.WinDLL('user32', use_last_error=3DTrue)</div><div>hwnd =
=3D user32.GetForegroundWindow()</div><div><br></div><div># this part</div>=
<div>pid =3D ctypes.c_ulong()</div><div><br></div><div>user32.GetWindowThre=
adProcessId(hwnd, ctypes.byref(pid))</div><div>print(pid.value)</div></div>=
</div><div class=3D"gmail_extra"><br><div class=3D"gmail_quote">On Wed, Nov=
30, 2016 at 8:56 AM, eryk sun <span dir=3D"ltr"><<a href=3D"mailto:eryk=
[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:1=
px #ccc solid;padding-left:1ex"><span class=3D"">On Wed, Nov 30, 2016 at 4:=
33 PM, Michael C<br>
<<a href=3D"mailto:[email protected]">mysecretrobotfactory@=
gmail.<wbr>com</a>> wrote:<br>
> Sorry for asking about this over and over again.<br>
><br>
> I ran the code at the bottom and got a<br>
><br>
> user32.<wbr>GetWindowThreadProcessId(<wbr>handle, PID)<br>
> OSError: exception: access violation writing 0x00000001<br>
><br>
> import ctypes<br>
> import time<br>
><br>
> time.sleep(2)<br>
> user32 =3D ctypes.WinDLL('user32', use_last_error=3DTrue)<br>
> handle =3D user32.GetForegroundWindow()<br>
> print(handle)<br>
> PID =3D 1<br>
> user32.<wbr>GetWindowThreadProcessId(<wbr>handle, PID)<br>
> print (PID)<br>
<br>
</span>Let's review the prototype:<br>
<br>
=C2=A0 =C2=A0 DWORD WINAPI GetWindowThreadProcessId(<br>
=C2=A0 =C2=A0 =C2=A0 _In_=C2=A0 =C2=A0 =C2=A0 HWND=C2=A0 =C2=A0 hWnd,<br>
=C2=A0 =C2=A0 =C2=A0 _Out_opt_ LPDWORD lpdwProcessId);<br>
<br>
lpdwProcessId is an LPDWORD. The LP stands for "long pointer". (I=
gnore<br>
the "long" part since it's a vestige of older CPU architectur=
es.) You<br>
need to pass a pointer to a DWORD. That's a C unsigned long integer,<br=
>
so you can use ctypes.c_ulong(). Or use the wintypes module, which<br>
defines most of the common Windows data types. To pass a pointer you<br>
can use ctypes.byref. For example:<br>
<br>
=C2=A0 =C2=A0 import ctypes<br>
=C2=A0 =C2=A0 from ctypes import wintypes<br>
<span class=3D""><br>
=C2=A0 =C2=A0 user32 =3D ctypes.WinDLL('user32', use_last_error=3DT=
rue)<br>
<br>
</span>=C2=A0 =C2=A0 hwnd =3D user32.GetForegroundWindow()<br>
=C2=A0 =C2=A0 pid =3D wintypes.DWORD()<br>
=C2=A0 =C2=A0 user32.<wbr>GetWindowThreadProcessId(hwnd, ctypes.byref(pid))=
<br>
<br>
The process ID is in the `value` attribute:<br>
<br>
=C2=A0 =C2=A0 >>> pid.value<br>
=C2=A0 =C2=A0 4832<br>
</blockquote></div><br></div>
--001a1144aa2e90a84305428cd97d--
--===============5488925460955336391==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
------------------------------------------------------------------------------
--===============5488925460955336391==
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
--===============5488925460955336391==--