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"><<a href=3D"mailto:[email protected]" target=3D"_blank">eryksu= [email protected]</a>></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> <<a href=3D"mailto:[email protected]">mysecretrobotfactory@= gmail.<wbr>com</a>> wrote:<br> ><br> > 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 'buffer' 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> > How do I create a pointer to it?<br> <br> </span>Pass it byref().<br> <br> > print('mbi.State: ',mbi.State)<br> <br> Check whether mbi.State is MEM_COMMIT before trying to read it. If<br> it's MEM_FREE or MEM_RESERVE, then ReadProcessMemory will fail.<br> <span class=3D""><br> > buffer =3D ctypes.create_string_buffer(4)<br> > bufferSize =3D (ctypes.sizeof(buffer))<br> ><br> > ReadProcessMemory =3D Kernel32.ReadProcessMemory<br> ><br> > if ReadProcessMemory(Process, ctypes.byref(mbi), buffer, bufferSize, N= one):<br> >=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0print('buffer is: ',buffer)<b= r> > else:<br> >=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0print('something is wrong')<b= r> <br> </span>Don't print "something is wrong". You're capturing= the thread'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==--