简体   繁体   中英

I can't read values from memory

So i am trying to get read some values from a game. I can successfully read the data from game via Cheat Engine. But when i try to enter the value to my code and receive values i only get 0. However when i try to do the same thing with some other programs it works without a problem. Here is the code

#include <iostream>
#include <Windows.h>
#include <string>

DWORD pid;
DWORD adr = 0x23CAE074AA8;
int my_value;

int main(){

    HWND hWnd = FindWindowA(0, ("game"));

    GetWindowThreadProcessId(hWnd, &pid);
    std::cout << pid << std::endl;
    HANDLE pHandle = OpenProcess(PROCESS_VM_READ, FALSE, pid);
    while(1){
        ReadProcessMemory(pHandle, (LPVOID)adr, &my_value, sizeof(my_value),0);
        std::cout << my_value << std::endl;
    }
}

I think the problem is the length of my address. Other programs adresses had shorter length. It also gives these errors:

Description Resource    Path    Location    Type
cast to pointer from integer of different size [-Wint-to-pointer-cast]  read_memory.cpp /read_memory/src    line 17 C/C++ Problem
Description Resource    Path    Location    Type
overflow in conversion from 'long long int' to 'LONG' {aka 'long int'} changes value from '2459641006760' to '-1375253848' [-Woverflow] read_memory.cpp /read_memory/src    line 6  C/C++ Problem

As the message says, DWORD seems too small to store addresses in your environment.

You should use uintptr_t from the header cstdint to store integers to be used as pointers.

#include <iostream>
#include <Windows.h>
#include <string>
#include <cstdint> // add this

DWORD pid;
uintptr_t adr = 0x23CAE074AA8; // change type

// ...

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