C++实现内存修改植物大战僵尸阳光值-C++语言论坛-编程语言区-资源工坊-游戏模组资源分享

C++实现内存修改植物大战僵尸阳光值

#include "pch.h"                 // 包含预编译头文件
#include <iostream>              // 标准输入输出流
#include <windows.h>             // Windows API 头文件
#include <psapi.h>               // 进程状态 API 头文件
#include <string>                // 字符串处理
#include <sstream>               // 字符串流处理
using namespace std;            // 使用标准命名空间

int main()                       // 主函数入口
{
    HWND jincheng = FindWindowA("MainWindow", "植物大战僵尸中文版");  // 查找指定窗口句柄

    DWORD dwProcessId;                                                   // 定义进程 ID 变量
    GetWindowThreadProcessId(jincheng, &dwProcessId);                     // 获取窗口所属进程 ID

    HANDLE pid = OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwProcessId);     // 打开进程句柄,具有所有权限
    HMODULE hModules[1024];                                              // 定义模块句柄数组
    DWORD cbNeeded;                                                       // 定义回调所需变量
    if (pid != NULL) {                                                    // 如果进程句柄有效
        std::cout << "进程句柄是:" << std::hex << pid << std::endl;      // 输出进程句柄

        EnumProcessModules(pid, hModules, sizeof(hModules), &cbNeeded);   // 枚举进程模块,获取模块信息
        std::cout << "模块基地址是:" << std::hex << hModules[0] << std::endl;  // 输出模块基地址

        // 基址
        uintptr_t baseAddress = reinterpret_cast<uintptr_t>(hModules[0]);  // 将模块基地址转换为整数
        uintptr_t firstOffset = 0x2A9EC0;                                  // 第一级偏移地址
        uintptr_t firstAddress;
        ReadProcessMemory(pid, reinterpret_cast<LPCVOID>(baseAddress + firstOffset), &firstAddress, sizeof(firstAddress), NULL);  // 读取进程内存中的数据

        // 一级偏移
        uintptr_t secondOffset = 0x768;                                    // 第二级偏移地址
        uintptr_t secondAddress;
        ReadProcessMemory(pid, reinterpret_cast<LPCVOID>(firstAddress + secondOffset), &secondAddress, sizeof(secondAddress), NULL);  // 读取进程内存中的数据

        // 二级偏移
        uintptr_t thirdOffset = 0x5560;                                    // 第三级偏移地址
        int value;
        ReadProcessMemory(pid, reinterpret_cast<LPCVOID>(secondAddress + thirdOffset), &value, sizeof(value), NULL);  // 读取进程内存中的数据
        cout << "读取的数据是:" << std::dec << value << std::endl;  // 输出读取的数据

        std::cout << "修改阳光数据值为:" << std::endl;;               // 输出提示信息
        int num;                                                       // 定义整数变量
        std::cin >> num;                                               // 输入要写入的数据

        WriteProcessMemory(pid, reinterpret_cast<LPVOID>(secondAddress + thirdOffset), &num, sizeof(num), NULL);  // 写入数据到进程内存中
        std::cout << "写入的数据是:" << num << std::endl;            // 输出写入的数据
    }
    else {
        std::cout << "打开进程失败。";                                // 输出打开进程失败信息
    }

    return 0;                                                          // 返回程序执行成功
}

 

请登录后发表评论

    没有回复内容