Python - Printing a ctype string buffer in my Read/WriteProcessMemory application
<p>Let me say that this was my first attempt at a proper memory reading and writing application. The writing function isn't even implemented yet but it will come in time. Just don't flame me for the code.</p>

<p>First thing is the complete program for so everyone can see (don't comment on my "bad coding choices", the thing works and I get the process handle and ID just fine)</p>



<pre class="lang-css prettyprint-override"><code>import ctypes
from ctypes import wintypes
from time import *
import win32ui, win32process ,win32api

PROCESS_ALL_ACCESS = 0x1F0FFF

ReadProcessMemory = ctypes.WinDLL('kernel32',use_last_error=True).ReadProcessMemory
ReadProcessMemory.argtypes = [wintypes.HANDLE,wintypes.LPCVOID,wintypes.LPVOID,ctypes.c_size_t,ctypes.POINTER(ctypes.c_size_t)]
ReadProcessMemory.restype = wintypes.BOOL

WriteProcessMemory = ctypes.WinDLL('kernel32',use_last_error=True).WriteProcessMemory
WriteProcessMemory.argtypes = [wintypes.HANDLE,wintypes.LPVOID,wintypes.LPCVOID,ctypes.c_size_t,ctypes.POINTER(ctypes.c_size_t)]
WriteProcessMemory.restype = wintypes.BOOL

def main():
    WindowName = input("Enter the window name: ")
    if (len(WindowName) &gt; 0):
        Process, pID = attach(WindowName)
        Choice = input("Read or Write?").lower()
        if (Choice == "write"):
            #DoWriteMethod
            pass
        if (Choice == "read"):
            Read(Process)

    else:
        print("Invalid Window Name!\n\n")
        sleep(2)
        return 0

def attach(WindowName):
    try: 
        hWnd = win32ui.FindWindow(None,WindowName).GetSafeHwnd()
    except:
        print("Window not found!\n\n")
        sleep(2)
        return 0,0

    pID = win32process.GetWindowThreadProcessId(hWnd)[1]
    Process = win32api.OpenProcess(PROCESS_ALL_ACCESS,0,pID).handle

    print("\nProcess = ", Process)
    print("ProcessID = ", pID)

    return Process,pID

def Write():
    return 0

def Read(Process):
    Address = input("Enter the address in hexadecial (0x form) \n")
    if (Address[0] == "0" and Address[1] == "x"):
        BufferAddress = ctypes.create_string_buffer(64)
        ptr = ctypes.pointer(BufferAddress)
        ReadProcessMemory(Process,Address,BufferAddress,64,None)
        print("Pointer contains: ", BufferAddress, "\n\n")
        main()
    else:
        print("Invalid address!\n\n")
        sleep(2)
        Read(Process)
    return 0

if (main() == 0):
    main()
if (main() == 1):
    exit(0)
</code></pre>

<p>On line 54 I create a 'ctype.create_string_buffer(64)'</p>

<p>I then use this in ReadProcessMemory where the value should be stored but whenever I print it on line 57 it just returns the following: </p>

<pre><code>Enter the window name: New Tab - Google Chrome

Process =  608
ProcessID =  2844

Read or Write?Read

Enter the address in hexadecial (0x form) 
0x1A6606BE380

Pointer contains:  &lt;ctypes.c_char_Array_64 object at 0x000001A0101D78C8&gt; 
</code></pre>

<p>I know this value should be 90 (found a random address in cheat engine) so it must contain the value 90? How could I find this value.</p>

<p>I am using Python 3.5.0 and the corresponding win32 modules package from</p>

<p>'pip install pypiwin32'</p>

<p>You are printing the object.  Print the contents:</p>

<pre><code>&gt;&gt;&gt; import ctypes
&gt;&gt;&gt; BufferAddress = ctypes.create_string_buffer(64)
&gt;&gt;&gt; print(BufferAddress)
&lt;ctypes.c_char_Array_64 object at 0x0000000003124248&gt;
&gt;&gt;&gt; dir(BufferAddress)
['__class__', '__ctypes_from_outparam__', '__delattr__', '__delitem__', '__dict__', '__dir__', '__doc__', '__eq__', '__f
ormat__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__',
'__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__setstate_
_', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_b_base_', '_b_needsfree_', '_length_', '_objects', '_t
ype_', 'raw', 'value']
&gt;&gt;&gt; BufferAddress.raw
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x
00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x
00\x00\x00\x00\x00'
&gt;&gt;&gt; BufferAddress.raw[0]
0
</code></pre>

python memory buffer ctypes readprocessmemory