Re: [Tutor] ctypes wintypes
Michael C <[email protected]> Fri, 6 Oct 2017 14:26:27 -0700
| Newsgroups | gmane.comp.python.ctypes,gmane.comp.python.tutor |
|---|---|
| Message-ID | <CANyKM1jQ3p+1DS3r+Latspd_6Gsr=qAuqBMf4gm4BjFn93hvTQ@mail.gmail.com> |
--===============1653202447115911841==
Content-Type: multipart/alternative; boundary="001a113724cc63c3ce055ae77e70"
--001a113724cc63c3ce055ae77e70
Content-Type: text/plain; charset="UTF-8"
This is my updated version, it still doesn't work :(
base = mbi.BaseAddress
buffer = ctypes.c_int32()
buffer_pointer = ctypes.byref(buffer)
ReadProcessMemory = Kernel32.ReadProcessMemory
if ReadProcessMemory(Process, base, buffer_pointer, mbi.RegionSize, None):
print('buffer is: ',buffer)
else:
raise ctypes.WinError(ctypes.get_last_error())
On Fri, Oct 6, 2017 at 2:06 PM, Michael C <[email protected]>
wrote:
> 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())
>>
>
>
--001a113724cc63c3ce055ae77e70
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">This is my updated version, it still doesn't work :(<d=
iv><br></div><div><div><br></div><div>base =3D mbi.BaseAddress</div><div>bu=
ffer =3D ctypes.c_int32()</div><div>buffer_pointer =3D ctypes.byref(buffer)=
</div><div><br></div><div>ReadProcessMemory =3D Kernel32.ReadProcessMemory<=
/div><div><br></div><div>if ReadProcessMemory(Process, base, buffer_pointer=
, mbi.RegionSize, None):</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 print('b=
uffer is: ',buffer)</div><div>else:</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=
=A0 raise ctypes.WinError(ctypes.get_last_error())</div></div></div><div cl=
ass=3D"gmail_extra"><br><div class=3D"gmail_quote">On Fri, Oct 6, 2017 at 2=
:06 PM, Michael C <span dir=3D"ltr"><<a href=3D"mailto:mysecretrobotfact=
[email protected]" target=3D"_blank">[email protected]</a>></sp=
an> wrote:<br><blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;=
border-left:1px #ccc solid;padding-left:1ex"><div dir=3D"ltr">like this?<di=
v><br></div><div>buffer =3D ctypes.byref(ctypes.create_<wbr>string_buffer(4=
))<br></div></div><div class=3D"HOEnZb"><div class=3D"h5"><div class=3D"gma=
il_extra"><br><div class=3D"gmail_quote">On Fri, Oct 6, 2017 at 1:55 PM, er=
yk sun <span dir=3D"ltr"><<a href=3D"mailto:[email protected]" target=3D=
"_blank">[email protected]</a>></span> wrote:<br><blockquote class=3D"gm=
ail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-le=
ft:1ex"><span>On Fri, Oct 6, 2017 at 9:12 PM, Michael C<br>
<<a href=3D"mailto:[email protected]" target=3D"_blank">mys=
[email protected]<wbr>m</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><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><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_las<wbr>t_erro=
r())<br>
</blockquote></div><br></div>
</div></div></blockquote></div><br></div>
--001a113724cc63c3ce055ae77e70--
--===============1653202447115911841==
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
--===============1653202447115911841==
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
--===============1653202447115911841==--