Re: destroying a window on windows

Michael C <[email protected]> Wed, 17 May 2017 19:14:28 -0700
Newsgroups gmane.comp.python.ctypes
Message-ID <CANyKM1hQJb0qcBg=+=-p4nfvV-sd62h7v2ic3y82GA4zdH3yBw@mail.gmail.com>
--===============8796208396684110518==
Content-Type: multipart/alternative; boundary="001a113f26b2f30f94054fc2f661"

--001a113f26b2f30f94054fc2f661
Content-Type: text/plain; charset="UTF-8"

where can i read up on what a thread is and what it does?

Thanks!

On Wed, May 17, 2017 at 7:12 PM, eryk sun <[email protected]> wrote:

> On Wed, May 17, 2017 at 11:10 PM, Michael C
> <[email protected]> wrote:
> > Apparently even though the MSDN says, given a handle, it would kill the
> > window, but it doesn't do that for me. Am I doing it properly?
> >
> > import time
> > import ctypes
> > User32 = ctypes.WinDLL('User32', use_last_error=True)
> >
> > handle = User32.GetForegroundWindow()
> > print(handle)
> > time.sleep(2)
> > User32.DestroyWindow(handle)
>
> You're not checking the BOOL return value to see whether the call
> succeeded. If it failed, you need to raise an exception for the
> thread's last error value. Do this in a ctypes errcheck function. For
> example:
>
>     def _check_bool(result, func, args):
>         if not result:
>             raise ctypes.WinError(ctypes.get_last_error())
>         return args
>
>     User32.DestroyWindow.errcheck = _check_bool
>
> With this check in place, if User32.DestroyWindow(handle) fails,
> you'll get an idiomatic exception. Chances are that the foreground
> window doesn't belong to your thread, and you haven't read the fine
> print in the remarks on MSDN:
>
>    A thread cannot use DestroyWindow to destroy a window created by a
> different thread.
>
> Thus it's probably failing with ERROR_ACCESS_DENIED (5). Try sending
> the window a WM_CLOSE message instead.
>

--001a113f26b2f30f94054fc2f661
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">where can i read up on what a thread is and what it does?<=
div><br></div><div>Thanks!</div></div><div class=3D"gmail_extra"><br><div c=
lass=3D"gmail_quote">On Wed, May 17, 2017 at 7:12 PM, eryk sun <span dir=3D=
"ltr">&lt;<a href=3D"mailto:[email protected]" target=3D"_blank">eryksun@gm=
ail.com</a>&gt;</span> wrote:<br><blockquote class=3D"gmail_quote" style=3D=
"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class=
=3D"">On Wed, May 17, 2017 at 11:10 PM, Michael C<br>
&lt;<a href=3D"mailto:[email protected]">mysecretrobotfactory@=
gmail.<wbr>com</a>&gt; wrote:<br>
&gt; Apparently even though the MSDN says, given a handle, it would kill th=
e<br>
&gt; window, but it doesn&#39;t do that for me. Am I doing it properly?<br>
&gt;<br>
</span><span class=3D"">&gt; import time<br>
&gt; import ctypes<br>
&gt; User32 =3D ctypes.WinDLL(&#39;User32&#39;, use_last_error=3DTrue)<br>
&gt;<br>
&gt; handle =3D User32.GetForegroundWindow()<br>
&gt; print(handle)<br>
&gt; time.sleep(2)<br>
&gt; User32.DestroyWindow(handle)<br>
<br>
</span>You&#39;re not checking the BOOL return value to see whether the cal=
l<br>
succeeded. If it failed, you need to raise an exception for the<br>
thread&#39;s last error value. Do this in a ctypes errcheck function. For<b=
r>
example:<br>
<br>
=C2=A0 =C2=A0 def _check_bool(result, func, args):<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 if not result:<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 raise ctypes.WinError(ctypes.get_=
<wbr>last_error())<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 return args<br>
<br>
=C2=A0 =C2=A0 User32.DestroyWindow.errcheck =3D _check_bool<br>
<br>
With this check in place, if User32.DestroyWindow(handle) fails,<br>
you&#39;ll get an idiomatic exception. Chances are that the foreground<br>
window doesn&#39;t belong to your thread, and you haven&#39;t read the fine=
<br>
print in the remarks on MSDN:<br>
<br>
=C2=A0 =C2=A0A thread cannot use DestroyWindow to destroy a window created =
by a<br>
different thread.<br>
<br>
Thus it&#39;s probably failing with ERROR_ACCESS_DENIED (5). Try sending<br=
>
the window a WM_CLOSE message instead.<br>
</blockquote></div><br></div>

--001a113f26b2f30f94054fc2f661--


--===============8796208396684110518==
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
--===============8796208396684110518==
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

--===============8796208396684110518==--