Re: [Tutor] ctypes wintypes

Michael C <[email protected]> Thu, 5 Oct 2017 12:27:53 -0700
Newsgroups gmane.comp.python.ctypes,gmane.comp.python.tutor
Message-ID <CANyKM1jVDKnvammyo1XFjsDR-Uk2E8sev-cAE=zyU0h6SQGg=Q@mail.gmail.com>
--===============6283680734438922519==
Content-Type: multipart/alternative; boundary="001a113c2e64832753055ad1b899"

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

First of all, thanks for the reply.


How do I see the values of each field? This doesn't work.

print(PMEMORY_BASIC_INFORMATION.Protect)

thanks!

On Thu, Oct 5, 2017 at 11:34 AM, eryk sun <[email protected]> wrote:

> On Tue, Oct 3, 2017 at 10:30 PM, Michael C
> <[email protected]> wrote:
> >
> > I am trying to create SYSTEM_INFO structure  and MEMORY_BASIC_INFORMATION
> > structure
>
> First, avoid relying on constants, enumerations, and structures
> published on MSDN. It's not always right. Get the SDK and use the
> header files instead. MEMORY_BASIC_INFORMATION is defined in winnt.h,
> and SYSTEM_INFO is defined in sysinfoapi.h.
>
> MEMORY_BASIC_INFORMATION is simple. Don't worry about the
> MEMORY_BASIC_INFORMATION32 and MEMORY_BASIC_INFORMATION64 versions.
> Those are meant for a debugger that's reading this structure directly
> from the memory of another process.
>
> SYSTEM_INFO is a bit tricky, given the anonymous struct and union. I
> prefer to nest the definitions, but you could flatten it as separate
> definitions if you like. Refer to the docs for how to use _anonymous_:
>
> https://docs.python.org/3/library/ctypes#ctypes.Structure._anonymous_
>
> Here are the definitions. Please don't mindlessly copy and paste.
> Recreate them on your own and use this example as a reference.
>
> import ctypes
> from ctypes.wintypes import WORD, DWORD, LPVOID
>
> PVOID = LPVOID
> SIZE_T = ctypes.c_size_t
>
> # https://msdn.microsoft.com/en-us/library/aa383751#DWORD_PTR
> if ctypes.sizeof(ctypes.c_void_p) == ctypes.sizeof(ctypes.c_ulonglong):
>     DWORD_PTR = ctypes.c_ulonglong
> elif ctypes.sizeof(ctypes.c_void_p) == ctypes.sizeof(ctypes.c_ulong):
>     DWORD_PTR = ctypes.c_ulong
>
> class MEMORY_BASIC_INFORMATION(ctypes.Structure):
>     """https://msdn.microsoft.com/en-us/library/aa366775"""
>     _fields_ = (('BaseAddress', PVOID),
>                 ('AllocationBase',    PVOID),
>                 ('AllocationProtect', DWORD),
>                 ('RegionSize', SIZE_T),
>                 ('State',   DWORD),
>                 ('Protect', DWORD),
>                 ('Type',    DWORD))
>
> PMEMORY_BASIC_INFORMATION = ctypes.POINTER(MEMORY_BASIC_INFORMATION)
>
> class SYSTEM_INFO(ctypes.Structure):
>     """https://msdn.microsoft.com/en-us/library/ms724958"""
>     class _U(ctypes.Union):
>         class _S(ctypes.Structure):
>             _fields_ = (('wProcessorArchitecture', WORD),
>                         ('wReserved', WORD))
>         _fields_ = (('dwOemId', DWORD), # obsolete
>                     ('_s', _S))
>         _anonymous_ = ('_s',)
>     _fields_ = (('_u', _U),
>                 ('dwPageSize', DWORD),
>                 ('lpMinimumApplicationAddress', LPVOID),
>                 ('lpMaximumApplicationAddress', LPVOID),
>                 ('dwActiveProcessorMask',   DWORD_PTR),
>                 ('dwNumberOfProcessors',    DWORD),
>                 ('dwProcessorType',         DWORD),
>                 ('dwAllocationGranularity', DWORD),
>                 ('wProcessorLevel',    WORD),
>                 ('wProcessorRevision', WORD))
>     _anonymous_ = ('_u',)
>
> LPSYSTEM_INFO = ctypes.POINTER(SYSTEM_INFO)
>

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

<div dir=3D"ltr">First of all, thanks for the reply.<div><br></div><div><br=
></div><div>How do I see the values of each field? This doesn&#39;t work.</=
div><div><div><br></div><div>print(PMEMORY_BASIC_INFORMATION.Protect)</div>=
</div><div><br></div><div>thanks!</div></div><div class=3D"gmail_extra"><br=
><div class=3D"gmail_quote">On Thu, Oct 5, 2017 at 11:34 AM, eryk sun <span=
 dir=3D"ltr">&lt;<a href=3D"mailto:[email protected]" target=3D"_blank">ery=
[email protected]</a>&gt;</span> wrote:<br><blockquote class=3D"gmail_quote" s=
tyle=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><spa=
n class=3D"">On Tue, Oct 3, 2017 at 10:30 PM, Michael C<br>
&lt;<a href=3D"mailto:[email protected]">mysecretrobotfactory@=
gmail.<wbr>com</a>&gt; wrote:<br>
&gt;<br>
&gt; I am trying to create SYSTEM_INFO structure=C2=A0 and MEMORY_BASIC_INF=
ORMATION<br>
&gt; structure<br>
<br>
</span>First, avoid relying on constants, enumerations, and structures<br>
published on MSDN. It&#39;s not always right. Get the SDK and use the<br>
header files instead. MEMORY_BASIC_INFORMATION is defined in winnt.h,<br>
and SYSTEM_INFO is defined in sysinfoapi.h.<br>
<br>
MEMORY_BASIC_INFORMATION is simple. Don&#39;t worry about the<br>
MEMORY_BASIC_INFORMATION32 and MEMORY_BASIC_INFORMATION64 versions.<br>
Those are meant for a debugger that&#39;s reading this structure directly<b=
r>
from the memory of another process.<br>
<br>
SYSTEM_INFO is a bit tricky, given the anonymous struct and union. I<br>
prefer to nest the definitions, but you could flatten it as separate<br>
definitions if you like. Refer to the docs for how to use _anonymous_:<br>
<br>
<a href=3D"https://docs.python.org/3/library/ctypes#ctypes.Structure._anony=
mous_" rel=3D"noreferrer" target=3D"_blank">https://docs.python.org/3/<wbr>=
library/ctypes#ctypes.<wbr>Structure._anonymous_</a><br>
<br>
Here are the definitions. Please don&#39;t mindlessly copy and paste.<br>
Recreate them on your own and use this example as a reference.<br>
<br>
import ctypes<br>
from ctypes.wintypes import WORD, DWORD, LPVOID<br>
<br>
PVOID =3D LPVOID<br>
SIZE_T =3D ctypes.c_size_t<br>
<br>
# <a href=3D"https://msdn.microsoft.com/en-us/library/aa383751#DWORD_PTR" r=
el=3D"noreferrer" target=3D"_blank">https://msdn.microsoft.com/en-<wbr>us/l=
ibrary/aa383751#DWORD_PTR</a><br>
if ctypes.sizeof(ctypes.c_void_p) =3D=3D ctypes.sizeof(ctypes.c_<wbr>ulongl=
ong):<br>
=C2=A0 =C2=A0 DWORD_PTR =3D ctypes.c_ulonglong<br>
elif ctypes.sizeof(ctypes.c_void_p) =3D=3D ctypes.sizeof(ctypes.c_ulong):<b=
r>
=C2=A0 =C2=A0 DWORD_PTR =3D ctypes.c_ulong<br>
<br>
class MEMORY_BASIC_INFORMATION(<wbr>ctypes.Structure):<br>
=C2=A0 =C2=A0 &quot;&quot;&quot;<a href=3D"https://msdn.microsoft.com/en-us=
/library/aa366775" rel=3D"noreferrer" target=3D"_blank">https://msdn.micros=
oft.com/<wbr>en-us/library/aa366775</a>&quot;&quot;&quot;<br>
=C2=A0 =C2=A0 _fields_ =3D ((&#39;BaseAddress&#39;, PVOID),<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (&#39;AllocationBas=
e&#39;,=C2=A0 =C2=A0 PVOID),<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (&#39;AllocationPro=
tect&#39;, DWORD),<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (&#39;RegionSize&#3=
9;, SIZE_T),<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (&#39;State&#39;,=
=C2=A0 =C2=A0DWORD),<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (&#39;Protect&#39;,=
 DWORD),<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (&#39;Type&#39;,=C2=
=A0 =C2=A0 DWORD))<br>
<br>
PMEMORY_BASIC_INFORMATION =3D ctypes.POINTER(MEMORY_BASIC_<wbr>INFORMATION)=
<br>
<br>
class SYSTEM_INFO(ctypes.Structure):<br>
=C2=A0 =C2=A0 &quot;&quot;&quot;<a href=3D"https://msdn.microsoft.com/en-us=
/library/ms724958" rel=3D"noreferrer" target=3D"_blank">https://msdn.micros=
oft.com/<wbr>en-us/library/ms724958</a>&quot;&quot;&quot;<br>
=C2=A0 =C2=A0 class _U(ctypes.Union):<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 class _S(ctypes.Structure):<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 _fields_ =3D ((&#39;wProcessorArc=
hitecture&#39;, WORD),<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0 (&#39;wReserved&#39;, WORD))<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 _fields_ =3D ((&#39;dwOemId&#39;, DWORD), # obs=
olete<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (&#39=
;_s&#39;, _S))<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 _anonymous_ =3D (&#39;_s&#39;,)<br>
=C2=A0 =C2=A0 _fields_ =3D ((&#39;_u&#39;, _U),<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (&#39;dwPageSize&#3=
9;, DWORD),<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (&#39;lpMinimumAppl=
icationAddress&#39;<wbr>, LPVOID),<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (&#39;lpMaximumAppl=
icationAddress&#39;<wbr>, LPVOID),<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (&#39;dwActiveProce=
ssorMask&#39;,=C2=A0 =C2=A0DWORD_PTR),<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (&#39;dwNumberOfPro=
cessors&#39;,=C2=A0 =C2=A0 DWORD),<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (&#39;dwProcessorTy=
pe&#39;,=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0DWORD),<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (&#39;dwAllocationG=
ranularity&#39;, DWORD),<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (&#39;wProcessorLev=
el&#39;,=C2=A0 =C2=A0 WORD),<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (&#39;wProcessorRev=
ision&#39;, WORD))<br>
=C2=A0 =C2=A0 _anonymous_ =3D (&#39;_u&#39;,)<br>
<br>
LPSYSTEM_INFO =3D ctypes.POINTER(SYSTEM_INFO)<br>
</blockquote></div><br></div>

--001a113c2e64832753055ad1b899--


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

--===============6283680734438922519==--