Re: destroying a window on windows
Michael C <[email protected]> Wed, 17 May 2017 19:16:49 -0700
| Newsgroups | gmane.comp.python.ctypes |
|---|---|
| Message-ID | <CANyKM1ig-wskEzcht36OFr_ZqYhhtp8BkAnA3GuXSDxLOJOh2w@mail.gmail.com> |
--===============1567556968652976621== Content-Type: multipart/alternative; boundary="001a114517dc603c13054fc2ff74" --001a114517dc603c13054fc2ff74 Content-Type: text/plain; charset="UTF-8" more importantly, how do i switch to the proper thread? thanks! On Wed, May 17, 2017 at 7:14 PM, Michael C <[email protected]> wrote: > 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. >> > > --001a114517dc603c13054fc2ff74 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable <div dir=3D"ltr">more importantly, how do i switch to the proper thread?<di= v><br></div><div>thanks!</div></div><div class=3D"gmail_extra"><br><div cla= ss=3D"gmail_quote">On Wed, May 17, 2017 at 7:14 PM, Michael C <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 .8ex;border-left:1px #ccc solid;padd= ing-left:1ex"><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"HOEnZb"><= div class=3D"h5"><div class=3D"gmail_extra"><br><div class=3D"gmail_quote">= On Wed, May 17, 2017 at 7:12 PM, 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 .8ex;bo= rder-left:1px #ccc solid;padding-left:1ex"><span>On Wed, May 17, 2017 at 11= :10 PM, Michael C<br> <<a href=3D"mailto:[email protected]" target=3D"_blank">mys= [email protected]<wbr>m</a>> wrote:<br> > Apparently even though the MSDN says, given a handle, it would kill th= e<br> > window, but it doesn't do that for me. Am I doing it properly?<br> ><br> </span><span>> import time<br> > import ctypes<br> > User32 =3D ctypes.WinDLL('User32', use_last_error=3DTrue)<br> ><br> > handle =3D User32.GetForegroundWindow()<br> > print(handle)<br> > time.sleep(2)<br> > User32.DestroyWindow(handle)<br> <br> </span>You'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'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_= las<wbr>t_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'll get an idiomatic exception. Chances are that the foreground<br> window doesn't belong to your thread, and you haven'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's probably failing with ERROR_ACCESS_DENIED (5). Try sending<br= > the window a WM_CLOSE message instead.<br> </blockquote></div><br></div> </div></div></blockquote></div><br></div> --001a114517dc603c13054fc2ff74-- --===============1567556968652976621== 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 --===============1567556968652976621== 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 --===============1567556968652976621==--