I am trying to read memory from a game process (Just like what Cheat Engine does). I have study different posts, like:
- Get HWND of each Window?
- How can I read the memory of another process in Python in Windows?
- WriteProcessMemory and ReadProcessMemory
And then, I am able to piece out my first memory reading script. Basically, it has 2 steps. Getting process handle, and then read the memory address from the handle.
But I value I get is wrong.
My code
import win32gui,win32com.client
import win32api
import ctypes
import win32ui
import win32process
from ctypes import wintypes
### Initializing functions and permissions ###
OpenProcess = ctypes.windll.kernel32.OpenProcess
ReadProcessMemory = ctypes.windll.kernel32.ReadProcessMemory # Method 1
PROCESS_ALL_ACCESS = 0x1F0FFF
PROCESS_QUERY_INFORMATION = 0x0400
PROCESS_VM_OPERATION = 0x0008
PROCESS_VM_READ = 0x0010
PROCESS_VM_WRITE = 0x0020
### End of Initializing session
### Getting process handle ###
HWND = win32ui.FindWindow(None,'My Game').GetSafeHwnd()
PID = win32process.GetWindowThreadProcessId(HWND)[1]
processHandle = OpenProcess(PROCESS_ALL_ACCESS, False, PID) # Why is it zero
### End of Getting process handle ###
### Reading value of a Memory Address ###
ADDRESS = 0x0111FF62
buffer = ctypes.c_char_p("Hello, World".encode('utf-8'))
bufferSize = len(buffer.value)
bytesRead = ctypes.c_ulong(0)
memory_value = ReadProcessMemory(processHandle, ADDRESS, buffer, bufferSize, ctypes.byref(bytesRead)) # Why is it zero
print('Memory Value = ', memory_value)
Memory Value = 0
The true value I get from Memory scanning software
I have double check if my HWND value is correct by using the function win32gui.SetForegroundWindow(HWND )
. It does bring the windows to Foreground. So I am pretty sure my program wrong until the line for HWND.