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'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"><<a href=3D"mailto:[email protected]" target=3D"_blank">ery= [email protected]</a>></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> <<a href=3D"mailto:[email protected]">mysecretrobotfactory@= gmail.<wbr>com</a>> wrote:<br> ><br> > I am trying to create SYSTEM_INFO structure=C2=A0 and MEMORY_BASIC_INF= ORMATION<br> > structure<br> <br> </span>First, avoid relying on constants, enumerations, and structures<br> published on MSDN. It'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't worry about the<br> MEMORY_BASIC_INFORMATION32 and MEMORY_BASIC_INFORMATION64 versions.<br> Those are meant for a debugger that'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'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 """<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>"""<br> =C2=A0 =C2=A0 _fields_ =3D (('BaseAddress', PVOID),<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 ('AllocationBas= e',=C2=A0 =C2=A0 PVOID),<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 ('AllocationPro= tect', DWORD),<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 ('RegionSize= 9;, SIZE_T),<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 ('State',= =C2=A0 =C2=A0DWORD),<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 ('Protect',= DWORD),<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 ('Type',=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 """<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>"""<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 (('wProcessorArc= hitecture', 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 ('wReserved', WORD))<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 _fields_ =3D (('dwOemId', DWORD), # obs= olete<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 ('= ;_s', _S))<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 _anonymous_ =3D ('_s',)<br> =C2=A0 =C2=A0 _fields_ =3D (('_u', _U),<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 ('dwPageSize= 9;, DWORD),<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 ('lpMinimumAppl= icationAddress'<wbr>, LPVOID),<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 ('lpMaximumAppl= icationAddress'<wbr>, LPVOID),<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 ('dwActiveProce= ssorMask',=C2=A0 =C2=A0DWORD_PTR),<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 ('dwNumberOfPro= cessors',=C2=A0 =C2=A0 DWORD),<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 ('dwProcessorTy= pe',=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 ('dwAllocationG= ranularity', DWORD),<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 ('wProcessorLev= el',=C2=A0 =C2=A0 WORD),<br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 ('wProcessorRev= ision', WORD))<br> =C2=A0 =C2=A0 _anonymous_ =3D ('_u',)<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==--