You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何Pymem内存写入代码仅作用于单个Game.exe进程?如何适配多进程?

问题原因

原代码仅作用于第一个Game.exe进程的核心原因是:

  • 调用Pymem("Game.exe")时,Pymem库会直接返回系统中第一个匹配到的同名进程,不会自动遍历所有进程。后续所有内存操作都只针对这一个进程实例执行。
  • 另外注意代码中的0xA4G678、0xC4H541、0xX3JGBC属于无效十六进制地址(包含G、H、J等非十六进制字符),实际使用时必须替换为合法的十六进制内存地址。
修改后的代码(支持多进程)
from pymem import Pymem
from pymem.process import module_from_name, list_processes

def getPtrAddr(mem_instance, address, offsets):
    addr = mem_instance.read_int(address)
    for offset in offsets:
        if offset != offsets[-1]:
            addr = mem_instance.read_int(addr + offset)
    addr = addr + offsets[-1]
    return addr

# 获取所有名为Game.exe的进程
game_processes = [proc for proc in list_processes() if proc.name == "Game.exe"]

# 遍历每个进程执行内存写入
for proc in game_processes:
    try:
        # 通过PID初始化Pymem实例
        mem = Pymem(proc.pid)
        # 获取当前进程的Game.exe模块基址
        game_module = module_from_name(mem.process_handle, "Game.exe").lpBaseOfDll
        
        # 执行内存写入操作(注意替换为合法的十六进制地址)
        mem.write_int(getPtrAddr(mem, game_module + 0xA4G678, [0x4A5]), 4)
        mem.write_int(getPtrAddr(mem, game_module + 0xC4H541, [0x1249]), 1)
        mem.write_int(getPtrAddr(mem, game_module + 0xX3JGBC, [0x2159]), 1)
        
        print(f"已成功处理进程PID: {proc.pid}")
    except Exception as e:
        print(f"处理进程PID {proc.pid} 失败: {str(e)}")
代码说明
  • 用list_processes()遍历系统所有进程,筛选出所有Game.exe进程。
  • 对每个进程单独初始化Pymem实例,确保内存操作针对当前进程。
  • 给getPtrAddr函数增加mem_instance参数,使其支持传入不同的Pymem实例,适配多进程场景。
  • 添加异常捕获,避免单个进程处理失败导致整个程序崩溃。

内容的提问来源于stack exchange,提问作者MrGlavrox66

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 21:15:49