简体   繁体   中英

ReadProcessMemory() returns 0 for static addresses

What I try to do is to read a static address that's pointing to a dynamic adress that holds some value. But if I try to read the static address it always returns 0. The only way for it to be read is if I attach a debugger to the dynamic address in cheat engine. However I have no problem reading it with only reading from the dynamic address.

DWORD address = 0x74EA46D8;
int value = 0;
int new_address = 0;
DWORD pid;
HWND hwnd;
hwnd = FindWindow(NULL,L"HackMe.exe");
if(!hwnd) {
    cout <<"Window not found!\n";
    cin.get();
} else {
    GetWindowThreadProcessId(hwnd,&pid);

    HANDLE phandle = OpenProcess(PROCESS_VM_READ,0,pid);
    if(!phandle) {
        cout <<"Could not get handle!\n";
        cin.get();
    } else {
        while(1) {
            ReadProcessMemory(phandle,(void*)address,&new_address,sizeof(new_address),0);
            cout << new_address << "\n"; //Always print 0
            //int new_address = 0x2ECA40B0; //This works if I uncomment this
            ReadProcessMemory(phandle,(void*)new_address,&value,sizeof(value),0);

I even tried getting debug privelege, but that didn't do anything. I have no clue on what the problem is since I'm very new to C++. Any help is appreciated.

Thank you.

Edit

GetLastError() returns 0 at first, then it returns 299

Edit 2

BOOL x = ReadProcessMemory(phandle,(void*)address,&new_address,sizeof(new_address),0);
cout << x << " " << GetLastError() << "\n";

returns

1 0
1 299
1 299
1 299

and so on

Edit 3 Bytes read is 4.

Edit 4

Just to clarify.

Reading directly from 0x74EA46D8 with ReadProcessMemory() returns 0.

If I open up cheat engine and add the address 0x74EA46D8 points to to the address list. Then right click on it and press "Find out what access this address" it can be read all of a sudden. Enabling SeDebugPrivelege does nothing.

The dynamic address can be read as normal, without having debug privelege(as long as I manually type the address for it or cheat engine debugs the address so the static address can be read)

It's structured in this way:

static address pointing to the address I try to read, this return 0 as the "new address" unless see above.

dynamic address, containing the value I'm trying to read. This reads just fine if I define the dynamic address manually. But if I don't it fails since new_address is 0, unless see above.

Edit 5

Finally I found out the problem, the previous address was wrong. That address was part of cheat engine and the real address was 0x013CD878 with an offset of 0x4B0. That was the reason why it didn't work unless I debugged it.

But I hope others will learn from my mistake :P

Aren't you reading from different addresses? address != new_address . (void*)address - is the address where you start reading from.

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