Fabricio
Fabricio

Reputation: 7935

Reading a process memory

I'm trying to read memory from a process (calc.exe). But I'm hitting "Could not read memory" message. Where is my mistake?

int main() {
    HWND handle = FindWindow(0, TEXT("Calculadora"));
    if (!handle) {
        msg("Could not find window");
        return 0;
    }

    DWORD id;
    GetWindowThreadProcessId(handle, &id);
    HANDLE proc = OpenProcess(PROCESS_VM_READ | PROCESS_VM_WRITE | PROCESS_VM_OPERATION, FALSE, id);
    if (!proc) {
        msg("Could not open process");
        return 0;
    }

    char buffer[128];
    if (ReadProcessMemory(proc, 0, &buffer, 128, NULL)) {
        msg("yes!!");
    }
    else {
        msg("Could not read memory");
    }

    CloseHandle(proc);
}

Upvotes: 0

Views: 784

Answers (1)

David Heffernan
David Heffernan

Reputation: 613491

You are attempting to read address 0 in the target process. That will always fail. You need to read from an address which is meaningful in the virtual address space of the target process.

Note that in order to call ReadProcessMemory you only need PROCESS_VM_READ. That's not the problem here, but I thought I would point it out for sake of completeness.

Upvotes: 7

Related Questions