mca64
mca64

Reputation: 544

WriteProcessMemory C++

Just pasted what was necessary, the memory addresses aren't being written to even though my logging shows that WriteProcessMemory() was successful. Also, I've double checked that i have the correct memory addresses as well. Thank You for help.

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);

Upvotes: 0

Views: 1728

Answers (1)

Brandon
Brandon

Reputation: 23500

You need to call VirtualProtect with PAGE_EXECUTE_READWRITE before you can write to the process's memory. After writing, you need to restore the original protection.

Another thing is, how exactly do you know those addresses are always the same? Can you confirm that it never changes?

Note: You MIGHT also have to call FlushInstructionCache after writing.

Upvotes: 2

Related Questions