Re: Acquiring process handle and convert it into PID
Michael C <[email protected]> Wed, 30 Nov 2016 08:33:43 -0800
| Newsgroups | gmane.comp.python.ctypes |
|---|---|
| Message-ID | <CANyKM1hHXMOM=vB-EeSF8d_k=C-SgipdMgYpo5BA_HYtziUOLA@mail.gmail.com> |
--===============8177652460344011228==
Content-Type: multipart/alternative; boundary=94eb2c062b0aa8f2780542874435
--94eb2c062b0aa8f2780542874435
Content-Type: text/plain; charset=UTF-8
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)
On Wed, Nov 30, 2016 at 8:24 AM, eryk sun <[email protected]> wrote:
> On Wed, Nov 30, 2016 at 4:15 PM, Michael C
> <[email protected]> wrote:
> > if I still want to use Windll somehow, since I need windows API, how
> should
> > I do it?
>
> Use WinDLL instead of windll, e.g.:
>
> kernel32 = ctypes.WinDLL('kernel32', use_last_error=True)
>
> Using ctypes.windll is unreliable in principle. It caches instances of
> WinDLL, which cache function pointer objects. Thus other libraries can
> modify function prototypes (i.e. restype, argtypes, errcheck) in ways
> that break your code.
>
--94eb2c062b0aa8f2780542874435
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">Sorry for asking about this over and over again.<div><br><=
/div><div>I ran the code at the bottom and got a =C2=A0 =C2=A0 =C2=A0</div>=
<div><br></div><div>user32.GetWindowThreadProcessId(handle, PID)</div><div>=
OSError: exception: access violation writing 0x00000001</div><div><br></div=
><div><br></div><div><br></div><div><div>import ctypes</div><div>import tim=
e</div><div><br></div><div>time.sleep(2)</div><div>user32 =3D ctypes.WinDLL=
('user32', use_last_error=3DTrue)</div><div>handle =3D user32.GetFo=
regroundWindow()</div><div>print(handle)</div><div>PID =3D 1</div><div>user=
32.GetWindowThreadProcessId(handle, PID)</div><div>print (PID)</div></div><=
div><br></div></div><div class=3D"gmail_extra"><br><div class=3D"gmail_quot=
e">On Wed, Nov 30, 2016 at 8:24 AM, eryk sun <span dir=3D"ltr"><<a href=
=3D"mailto:[email protected]" target=3D"_blank">[email protected]</a>></=
span> wrote:<br><blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8e=
x;border-left:1px #ccc solid;padding-left:1ex"><span class=3D"">On Wed, Nov=
30, 2016 at 4:15 PM, Michael C<br>
<<a href=3D"mailto:[email protected]">mysecretrobotfactory@=
gmail.<wbr>com</a>> wrote:<br>
> if I still want to use Windll somehow, since I need windows API, how s=
hould<br>
> I do it?<br>
<br>
</span>Use WinDLL instead of windll, e.g.:<br>
<br>
=C2=A0 =C2=A0 kernel32 =3D ctypes.WinDLL('kernel32', use_last_error=
=3DTrue)<br>
<br>
Using ctypes.windll is unreliable in principle. It caches instances of<br>
WinDLL, which cache function pointer objects. Thus other libraries can<br>
modify function prototypes (i.e. restype, argtypes, errcheck) in ways<br>
that break your code.<br>
</blockquote></div><br></div>
--94eb2c062b0aa8f2780542874435--
--===============8177652460344011228==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
------------------------------------------------------------------------------
--===============8177652460344011228==
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
--===============8177652460344011228==--