71 lines
2.1 KiB
Python
71 lines
2.1 KiB
Python
import win32api
|
||
import win32con
|
||
import win32process
|
||
import win32gui
|
||
from ctypes import c_int, byref, c_void_p, windll
|
||
from ctypes.wintypes import HANDLE
|
||
import time
|
||
|
||
def get_pid_by_window_title(title):
|
||
def callback(hwnd, hwnds):
|
||
if win32gui.IsWindowVisible(hwnd) and win32gui.GetWindowText(hwnd) == title:
|
||
hwnds.append(hwnd)
|
||
return True
|
||
|
||
hwnds = []
|
||
win32gui.EnumWindows(callback, hwnds)
|
||
|
||
pids = []
|
||
for hwnd in hwnds:
|
||
_, pid = win32process.GetWindowThreadProcessId(hwnd)
|
||
pids.append(pid)
|
||
|
||
return pids
|
||
|
||
def read_process_memory(pid, address):
|
||
hProcess = win32api.OpenProcess(win32con.PROCESS_ALL_ACCESS, False, pid)
|
||
buffer = (c_int * 1)()
|
||
bytes_read = c_int(0)
|
||
success = windll.kernel32.ReadProcessMemory(HANDLE(hProcess), c_void_p(address), byref(buffer), 4, byref(bytes_read))
|
||
win32api.CloseHandle(hProcess)
|
||
if not success:
|
||
raise Exception("Failed to read process memory")
|
||
return buffer[0]
|
||
|
||
def write_process_memory(pid, address, value):
|
||
hProcess = win32api.OpenProcess(win32con.PROCESS_ALL_ACCESS, False, pid)
|
||
value_c = c_int(value)
|
||
if not windll.kernel32.WriteProcessMemory(HANDLE(hProcess), c_void_p(address), byref(value_c), 4, None):
|
||
raise Exception("Failed to write process memory")
|
||
win32api.CloseHandle(hProcess)
|
||
|
||
def main():
|
||
window_title = "植物大战僵尸v2.2 "
|
||
pids = get_pid_by_window_title(window_title)
|
||
|
||
if len(pids) == 0:
|
||
print("No matching windows found.")
|
||
return
|
||
|
||
pid = pids[0] # 取第一个匹配的PID,如果有多个窗口,你可能需要调整逻辑
|
||
base_address = 0x2A9EC0
|
||
offset1 = 768
|
||
offset2 = 5560
|
||
final_address = base_address + offset1 + offset2
|
||
|
||
try:
|
||
original_value = read_process_memory(pid, final_address)
|
||
print(f"Original value: {original_value}")
|
||
|
||
new_value = 9420
|
||
write_process_memory(pid, final_address, new_value)
|
||
|
||
time.sleep(1)
|
||
|
||
modified_value = read_process_memory(pid, final_address)
|
||
print(f"Modified value: {modified_value}")
|
||
except Exception as e:
|
||
print(e)
|
||
|
||
if __name__ == "__main__":
|
||
main() |