Search code examples
c++pointersmemory

i want to change the value of the first program with the second program


here is my problem

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

int main()
{
    int coins = 1;
    

    std::cout << &coins <<std::endl;

    Sleep(10000);
    return 0;
}

the coins value is the one i want to change with the second program

here is the code of the second program

#include <iostream>

int main()
{
    int* coins = reinterpret_cast<int*> (0x57da7ffe7c);
    std::cout<<*coins<<"\n";
    return 0;
}

i expected that it will print the value of coins witch is (1) but it gives me this error (exited with code=3221225477)

the editor i use is vs code


Solution

  • Summary of information from the comments:

    1. Each process has its own address space. Even if the address 0x57da7ffe7c is a fixed one in one process, you cannot access it from another process this way.

    2. The error code; 3221225477, which you got (which is 0xC0000005 in hex) is "Access Violation", meaning that you attempted to read an invalid address.

    3. You seem to be using Windows. There is a Windows API for reading another process’s memory (under certain limitations): ReadProcessMemory. You can have a look at the documentation link, but it's quite advanced to use.

    4. The address space of a process is valid only as long as it is alive. Therefore if you use ReadProcessMemory, the first program would need to be still running at that stage (before your edit to the question you didn't have a Sleep in the first program, and so it would have exited almost immediatly).