WriteProcessMemory C ++

时间:2014-01-13 03:33:55

标签: c++ winapi

只是粘贴了必要的内容,即使我的日志记录显示WriteProcessMemory()成功,也没有写入内存地址。另外,我已经仔细检查过我的内存地址是否正确。谢谢你的帮助。

char* offsets[][3] = {
    { "0x3E264", "0", "char[1]" },
    { "0x45848", "Auto-Mine", "char[10]" },
    { "0x458C0", "Auto-Build", "char[10]" },
    //to be continued...
};

HANDLE scHandle = OpenProcess(PROCESS_VM_WRITE | PROCESS_VM_OPERATION, FALSE, ID);
if (scHandle == NULL) {
    log << "ERROR: OpenProcess() returned " << GetLastError() << endl;
    return false;
}
DWORD bytesOut;
for (int a = 0; a < 9; a++) {
    if (WriteProcessMemory(scHandle, (LPVOID)(wDetectorBaseAddress + (int)strtol(offsets[a][0], NULL, 0)), offsets[a][1], strlen(offsets[a][1]) + 1, &bytesOut))
    {
        log << "WriteProcessMemory() to address " << wDetectorBaseAddress << " + " << (int)strtol(offsets[a][0], NULL, 0) << " = " << wDetectorBaseAddress + (int)strtol(offsets[a][0], NULL, 0) << " with '" << offsets[a][1] << "'; " << bytesOut << " bytes were written" << endl;
    }
    else
    {
        log << "ERROR: WriteProcessMemory() returned " << GetLastError() << endl;
        return false;
    }
}
CloseHandle(scHandle);

1 个答案:

答案 0 :(得分:2)

您需要先使用VirtualProtect调用PAGE_EXECUTE_READWRITE,然后才能写入进程的内存。写完后,您需要恢复原始保护。

另一件事是,你究竟知道这些地址总是一样的吗?你能证实它永远不会改变吗?

注意:您可能还必须在写完后致电FlushInstructionCache