简体   繁体   中英

Simple ReadProcessMemory not working

I googled a bit but can't seem to make this work.

privileges();
int pid = getPid("test.exe");
cout << "Process ID :" << pid << endl;

const char* prename;
HANDLE pHandle = OpenProcess(PROCESS_VM_READ , FALSE, pid);
if (pHandle)
{
    cout << "Handle Open Success" << endl;
    //SIZE_T bytesRead;
    if (ReadProcessMemory(pHandle, (void*)0x013831BC, &prename, strlen(prename), NULL))
    {
        cout << "Read Success" << endl;
        cout << prename << endl;
    }

    else
        cout << GetLastError() << endl;

}
return 0;

It prints "Read Success" but does not print the variable just blank. The address(address of a string in another process) I got is from ollydbg and verified it using a function as well.

I also wanted to replace the string using writeprocessmemory but before i get to that i needed to make sure reading is correct.

Any idea?

Your problem lies here:

const char* prename;

ReadProcessMemory(pHandle, (void*)0x013831BC, &prename, strlen(prename), NULL)

Your char pointer is not initialized and neither is the random memory it points to. When you call strlen on it, it's trying to get the length of a random memory location.

Secondly you're using the address of the pointer &prename , that's the address of the pointer not the char array it should point to.

To fix do it like this:

char prename[100];

ReadProcessMemory(pHandle, (void*)0x013831BC, &prename, sizeof(prename), NULL)

sizeof() will return 100, so you will be reading 100 bytes of memory

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM