Re: [Tutor] ctypes wintypes

Michael C <[email protected]> Fri, 6 Oct 2017 11:26:41 -0700
Newsgroups gmane.comp.python.ctypes,gmane.comp.python.tutor
Message-ID <CANyKM1gcHjBGNP6YOG3MR2cSzGo2nH0b1AyBVLsYQSRPyYXe3w@mail.gmail.com>
--===============0229661759896868453==
Content-Type: multipart/alternative; boundary="f40304353a087b93b4055ae4fbdf"

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

Hi Eryk Sun:

I started out with what you gave me:

>code starts
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)


> code ends


I am trying to acquire "lpMinimumApplicationAddress" and
"lpMaximumApplicationAddress" from system_info, so I did this,

>code
Kernel32 = ctypes.WinDLL('kernel32', use_last_error=True)
Kernel32.GetSystemInfo(LPSYSTEM_INFO)
print(LPLPSYSTEM_INFO.lpMinimumApplicationAddress)

>code ends

 and then it says

Traceback (most recent call last):
  File "C:/Users/AwesomeGuy/Google Drive/My life of hacking/SWTOR/mah
scanner/with_eryk_sun_s_help_peace by peace.py", line 55, in <module>
    Kernel32.GetSystemInfo(LPSYSTEM_INFO)
ctypes.ArgumentError: argument 1: <class 'TypeError'>: Don't know how to
convert parameter 1



thanks for reading!


On Thu, Oct 5, 2017 at 1:13 PM, eryk sun <[email protected]> wrote:

> On Thu, Oct 5, 2017 at 8:27 PM, Michael C
> <[email protected]> wrote:
> >
> > How do I see the values of each field? This doesn't work.
> >
> > print(PMEMORY_BASIC_INFORMATION.Protect)
>
> Create an instance of MEMORY_BASIC_INFORMATION and pass a pointer to
> it via byref(). For example, the following queries the region of
> memory of the VirtualQuery function itself.
>
>     kernel32 = ctypes.WinDLL('kernel32', use_last_error=True)
>
>     MEM_COMMIT = 0x1000
>     PAGE_EXECUTE_READ = 0x20
>     PAGE_EXECUTE_WRITECOPY = 0x80
>
>     VirtualQuery = kernel32.VirtualQuery
>     VirtualQuery.restype = SIZE_T
>     VirtualQuery.argtypes = (LPVOID, PMEMORY_BASIC_INFORMATION, SIZE_T)
>
>     mbi = MEMORY_BASIC_INFORMATION()
>     VirtualQuery(VirtualQuery, ctypes.byref(mbi), ctypes.sizeof(mbi))
>
>     >>> mbi.AllocationBase == kernel32._handle
>     True
>     >>> mbi.AllocationProtect == PAGE_EXECUTE_WRITECOPY
>     True
>     >>> mbi.BaseAddress
>     140703181352960
>     >>> mbi.RegionSize
>     364544
>     >>> mbi.State == MEM_COMMIT
>     True
>     >>> mbi.Protect ==  PAGE_EXECUTE_READ
>     True
>

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

<div dir=3D"ltr"><div>Hi Eryk Sun:</div><div><br></div><div>I started out w=
ith what you gave me:</div><div><br></div><div>&gt;code starts</div><div><d=
iv>class SYSTEM_INFO(ctypes.Structure):</div><div>=C2=A0 =C2=A0 &quot;&quot=
;&quot;<a href=3D"https://msdn.microsoft.com/en-us/library/ms724958">https:=
//msdn.microsoft.com/en-us/library/ms724958</a>&quot;&quot;&quot;</div><div=
>=C2=A0 =C2=A0 class _U(ctypes.Union):</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=
=A0 class _S(ctypes.Structure):</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0 _fields_ =3D ((&#39;wProcessorArchitecture&#39;, WORD),</div><di=
v>=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))</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=
=A0 _fields_ =3D ((&#39;dwOemId&#39;, DWORD), # obsolete</div><div>=C2=A0 =
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (&#39;_s&#39=
;, _S))</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 _anonymous_ =3D (&#39;_s&#39;=
,)</div><div>=C2=A0 =C2=A0 _fields_ =3D ((&#39;_u&#39;, _U),</div><div>=C2=
=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (&#39;dwPageSize&#39;,=
 DWORD),</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =
(&#39;lpMinimumApplicationAddress&#39;, LPVOID),</div><div>=C2=A0 =C2=A0 =
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (&#39;lpMaximumApplicationAddress=
&#39;, LPVOID),</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =
=C2=A0 (&#39;dwActiveProcessorMask&#39;,=C2=A0 =C2=A0DWORD_PTR),</div><div>=
=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),</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =
=C2=A0 =C2=A0 =C2=A0 =C2=A0 (&#39;dwProcessorType&#39;,=C2=A0 =C2=A0 =C2=A0=
 =C2=A0 =C2=A0DWORD),</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =
=C2=A0 =C2=A0 (&#39;dwAllocationGranularity&#39;, DWORD),</div><div>=C2=A0 =
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (&#39;wProcessorLevel&#39;=
,=C2=A0 =C2=A0 WORD),</div><div>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =
=C2=A0 =C2=A0 (&#39;wProcessorRevision&#39;, WORD))</div><div>=C2=A0 =C2=A0=
 _anonymous_ =3D (&#39;_u&#39;,)</div><div><br></div><div>LPSYSTEM_INFO =3D=
 ctypes.POINTER(SYSTEM_INFO)</div></div><div><br></div><div><br></div><div>=
&gt; code ends</div><div><br></div><div><br></div>I am trying to acquire &q=
uot;lpMinimumApplicationAddress&quot; and &quot;lpMaximumApplicationAddress=
&quot; from system_info, so I did this,<div><br></div><div>&gt;code</div><d=
iv><div>Kernel32 =3D ctypes.WinDLL(&#39;kernel32&#39;, use_last_error=3DTru=
e)</div><div>Kernel32.GetSystemInfo(LPSYSTEM_INFO)</div></div><div>print(LP=
LPSYSTEM_INFO.lpMinimumApplicationAddress)</div><div><br></div><div>&gt;cod=
e ends</div><div><br></div><div>=C2=A0and then it says</div><div><br></div>=
<div><div>Traceback (most recent call last):</div><div>=C2=A0 File &quot;C:=
/Users/AwesomeGuy/Google Drive/My life of hacking/SWTOR/mah scanner/with_er=
yk_sun_s_help_peace by peace.py&quot;, line 55, in &lt;module&gt;</div><div=
>=C2=A0 =C2=A0 Kernel32.GetSystemInfo(LPSYSTEM_INFO)</div><div>ctypes.Argum=
entError: argument 1: &lt;class &#39;TypeError&#39;&gt;: Don&#39;t know how=
 to convert parameter 1</div></div><div><br></div><div><br></div><div><br><=
/div><div>thanks for reading!</div><div><br></div></div><div class=3D"gmail=
_extra"><br><div class=3D"gmail_quote">On Thu, Oct 5, 2017 at 1:13 PM, eryk=
 sun <span dir=3D"ltr">&lt;<a href=3D"mailto:[email protected]" target=3D"_=
blank">[email protected]</a>&gt;</span> wrote:<br><blockquote class=3D"gmai=
l_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left=
:1ex"><span class=3D"">On Thu, Oct 5, 2017 at 8:27 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 see the values of each field? This doesn&#39;t work.<br>
&gt;<br>
&gt; print(PMEMORY_BASIC_<wbr>INFORMATION.Protect)<br>
<br>
</span>Create an instance of MEMORY_BASIC_INFORMATION and pass a pointer to=
<br>
it via byref(). For example, the following queries the region of<br>
memory of the VirtualQuery function itself.<br>
<br>
=C2=A0 =C2=A0 kernel32 =3D ctypes.WinDLL(&#39;kernel32&#39;, use_last_error=
=3DTrue)<br>
<br>
=C2=A0 =C2=A0 MEM_COMMIT =3D 0x1000<br>
=C2=A0 =C2=A0 PAGE_EXECUTE_READ =3D 0x20<br>
=C2=A0 =C2=A0 PAGE_EXECUTE_WRITECOPY =3D 0x80<br>
<br>
=C2=A0 =C2=A0 VirtualQuery =3D kernel32.VirtualQuery<br>
=C2=A0 =C2=A0 VirtualQuery.restype =3D SIZE_T<br>
=C2=A0 =C2=A0 VirtualQuery.argtypes =3D (LPVOID, PMEMORY_BASIC_INFORMATION,=
 SIZE_T)<br>
<br>
=C2=A0 =C2=A0 mbi =3D MEMORY_BASIC_INFORMATION()<br>
=C2=A0 =C2=A0 VirtualQuery(VirtualQuery, ctypes.byref(mbi), ctypes.sizeof(m=
bi))<br>
<br>
=C2=A0 =C2=A0 &gt;&gt;&gt; mbi.AllocationBase =3D=3D kernel32._handle<br>
=C2=A0 =C2=A0 True<br>
=C2=A0 =C2=A0 &gt;&gt;&gt; mbi.AllocationProtect =3D=3D PAGE_EXECUTE_WRITEC=
OPY<br>
=C2=A0 =C2=A0 True<br>
=C2=A0 =C2=A0 &gt;&gt;&gt; mbi.BaseAddress<br>
=C2=A0 =C2=A0 140703181352960<br>
=C2=A0 =C2=A0 &gt;&gt;&gt; mbi.RegionSize<br>
=C2=A0 =C2=A0 364544<br>
=C2=A0 =C2=A0 &gt;&gt;&gt; mbi.State =3D=3D MEM_COMMIT<br>
=C2=A0 =C2=A0 True<br>
=C2=A0 =C2=A0 &gt;&gt;&gt; mbi.Protect =3D=3D=C2=A0 PAGE_EXECUTE_READ<br>
=C2=A0 =C2=A0 True<br>
</blockquote></div><br></div>

--f40304353a087b93b4055ae4fbdf--


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

--===============0229661759896868453==--