ReadProcessMemory始终读取0

问题描述

我正在制作一款用于教育目的的作弊游戏(目前在Windows上)。到目前为止,我的错误ReadProcessMemory始终读为0。

LPCVOID addr = (LPCVOID *) 0x1228A93C; // TO-DO: c++ casts 
int dest = 0;
SIZE_T read = 0;
bool error = ReadProcessMemory(process,addr,&dest,sizeof(int),&read);
if (!error) {
    printf("I read %llu w/ %u at %p\r\n",read,dest,addr);
} else {
    printf("This isn't working: %p / %llu\r\n",read);
    std::cerr << "err: " << std::to_string(::GetLastError()) << std::endl;
    return (1);
}

在那里,我尝试读取游戏中的金钱金额。通过使用作弊引擎,我得到的价值在您每次使用钱时都会改变,即上面的代码片段中的0x1228A93C。如果我更改作弊引擎中该地址所指向的值,那么金钱也会在游戏中发生变化,因此我认为这是正确的地址。

尽管如此,当我运行此代码片段时,仍会得到以下输出

I read 0 w/ 0 at 0x1228a93c

这意味着它不可读。

注意:在我的程序中,还有一个以上的代码,但这基本上是找到游戏窗口,创建游戏快照并找到exe模块。

解决方法

这可能是您想要的:

LPCVOID addr = (LPCVOID *) 0x1228A93C; // TO-DO: c++ casts 
int dest = 0;
SIZE_T read = 0;
if (ReadProcessMemory(process,addr,&dest,sizeof(int),&read))
{
    printf("I read %llu w/ %u at %p\r\n",read,dest,addr);
} else {
    auto lasterror = ::GetLastError();               // first thing to do: call GetLastError
    printf("This isn't working: %p / %llu\r\n",read);
    std::cerr << "err: " << lasterror << std::endl;  // std::to_string is useless here
}

如果发生错误,首先要做的是致电GetLastError(),因为您不知道cout是否会致电SetLastError()