由于某种原因,WriteProcessMemory无法正常工作

时间:2020-06-07 12:30:19

标签: c++ winapi

这是我要制作的程序的所有源代码,而我根本无法使WriteProcessMemory正常工作。它返回正确的消息,表明一切顺利,但游戏中实际上没有任何变化。有人知道修复程序吗?

#include <iostream>
#include <Windows.h>
using namespace std;

// variables
int plcHold = 1;
string hlthLoop = "OFF";
string ammoLoop = "OFF";
DWORD pid;
DWORD playerAddr;
DWORD hlthOffs = 0xF8;

// main function
int main()
{
    // finding pid, opening proc, finding player address
    HWND hwnd = FindWindowA(NULL, "AssaultCube");
    if(hwnd == NULL)
    {
        cout << "Error; Couldn't find window" << endl;
    } else{
        GetWindowThreadProcessId(hwnd, &pid);
        HANDLE pHandle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);

        if(pHandle == NULL)
        {
            cout << "Error; Couldn't open process" << endl;
        } else{
        ReadProcessMemory(pHandle, (LPCVOID)0x50F4F4, &playerAddr, sizeof(playerAddr), 0);

        if(ReadProcessMemory != FALSE)
        {
            cout << "Health successfully read!" << endl;
        } else{
            cout << "Error code " << GetLastError << endl;
        }
        }
    while(plcHold == 1){
        cout << "========== *****'s Assault Cube Trainer ==========\n" << endl;
        cout << "=============== Health Loop - " << hlthLoop << " ================" << endl;

        Sleep(1500);
        system("cls");

        if(GetAsyncKeyState(0x5A))
        {
            cout << "Health successfully edited!" << endl;
            WriteProcessMemory(pHandle, LPVOID(playerAddr + hlthOffs), 0, sizeof(999), 0);
            CloseHandle(pHandle);
        }
    }
    }
    return 0;
}

1 个答案:

答案 0 :(得分:2)

您正在为第三个(WriteProcessMemory)参数传递指向lpBuffer的空指针。您必须传递实际值的地址,而不是值本身。如果要写入整数值,请尝试以下操作:

DWORD val = 0; // or 999?
WriteProcessMemory(
    pHandle, static_cast<LPVOID>(playerAddr + hlthOffs),
    &val, sizeof(val), 0);