Re: [Tutor] ctypes wintypes

Michael C <[email protected]> Fri, 6 Oct 2017 14:06:50 -0700
Newsgroups gmane.comp.python.ctypes,gmane.comp.python.tutor
Message-ID <CANyKM1jOQbddsvmR1VBCU2oNPviOiAvS65-BBFQ-WmXdN1+aCg@mail.gmail.com>
--===============8624388497251830213==
Content-Type: multipart/alternative; boundary="94eb2c18d9803e9809055ae738c3"

--94eb2c18d9803e9809055ae738c3
Content-Type: text/plain; charset="UTF-8"

like this?

buffer = ctypes.byref(ctypes.create_string_buffer(4))

On Fri, Oct 6, 2017 at 1:55 PM, eryk sun <[email protected]> wrote:

> On Fri, Oct 6, 2017 at 9:12 PM, Michael C
> <[email protected]> wrote:
> >
> > How do I create a buffer, or rather, is a buffer just a variable?
>
> A buffer is a block of memory for an I/O operation. For example, if
> you need to read a 4-byte (32-bit) integer at an address in another
> process, the 'buffer' could be ctypes.c_int32(). In general, to read
> an arbitrary-sized block of memory, use ctypes.create_string_buffer()
> to create a char array.
>
> > How do I create a pointer to it?
>
> Pass it byref().
>
> > print('mbi.State: ',mbi.State)
>
> Check whether mbi.State is MEM_COMMIT before trying to read it. If
> it's MEM_FREE or MEM_RESERVE, then ReadProcessMemory will fail.
>
> > buffer = ctypes.create_string_buffer(4)
> > bufferSize = (ctypes.sizeof(buffer))
> >
> > ReadProcessMemory = Kernel32.ReadProcessMemory
> >
> > if ReadProcessMemory(Process, ctypes.byref(mbi), buffer, bufferSize,
> None):
> >         print('buffer is: ',buffer)
> > else:
> >         print('something is wrong')
>
> Don't print "something is wrong". You're capturing the thread's last
> error value, so use it to raise an informative exception. For example:
>
>     if not success:
>         raise ctypes.WinError(ctypes.get_last_error())
>

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

<div dir=3D"ltr">like this?<div><br></div><div>buffer =3D ctypes.byref(ctyp=
es.create_string_buffer(4))<br></div></div><div class=3D"gmail_extra"><br><=
div class=3D"gmail_quote">On Fri, Oct 6, 2017 at 1:55 PM, eryk sun <span di=
r=3D"ltr">&lt;<a href=3D"mailto:[email protected]" target=3D"_blank">eryksu=
[email protected]</a>&gt;</span> wrote:<br><blockquote class=3D"gmail_quote" styl=
e=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span c=
lass=3D"">On Fri, Oct 6, 2017 at 9:12 PM, Michael C<br>
&lt;<a href=3D"mailto:[email protected]">mysecretrobotfactory@=
gmail.<wbr>com</a>&gt; wrote:<br>
&gt;<br>
&gt; How do I create a buffer, or rather, is a buffer just a variable?<br>
<br>
</span>A buffer is a block of memory for an I/O operation. For example, if<=
br>
you need to read a 4-byte (32-bit) integer at an address in another<br>
process, the &#39;buffer&#39; could be ctypes.c_int32(). In general, to rea=
d<br>
an arbitrary-sized block of memory, use ctypes.create_string_buffer()<br>
to create a char array.<br>
<span class=3D""><br>
&gt; How do I create a pointer to it?<br>
<br>
</span>Pass it byref().<br>
<br>
&gt; print(&#39;mbi.State: &#39;,mbi.State)<br>
<br>
Check whether mbi.State is MEM_COMMIT before trying to read it. If<br>
it&#39;s MEM_FREE or MEM_RESERVE, then ReadProcessMemory will fail.<br>
<span class=3D""><br>
&gt; buffer =3D ctypes.create_string_buffer(4)<br>
&gt; bufferSize =3D (ctypes.sizeof(buffer))<br>
&gt;<br>
&gt; ReadProcessMemory =3D Kernel32.ReadProcessMemory<br>
&gt;<br>
&gt; if ReadProcessMemory(Process, ctypes.byref(mbi), buffer, bufferSize, N=
one):<br>
&gt;=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0print(&#39;buffer is: &#39;,buffer)<b=
r>
&gt; else:<br>
&gt;=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0print(&#39;something is wrong&#39;)<b=
r>
<br>
</span>Don&#39;t print &quot;something is wrong&quot;. You&#39;re capturing=
 the thread&#39;s last<br>
error value, so use it to raise an informative exception. For example:<br>
<br>
=C2=A0 =C2=A0 if not success:<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 raise ctypes.WinError(ctypes.get_<wbr>last_erro=
r())<br>
</blockquote></div><br></div>

--94eb2c18d9803e9809055ae738c3--


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

--===============8624388497251830213==--