-1

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

14
  • 7
    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 like this.
    – wohlstad
    Commented Jun 13 at 18:13
  • 2
    Where did you get that memory address from?? Commented Jun 13 at 18:13
  • 5
    3221225477 --> This is 0xC0000005 which is an Access Violation. Not sure where or how you expected a totally separate program with its own address space, plus has been terminated is supposed to magically retain 0x57da7ffe7c as the address to another, totally separate program. Imagine you ran program 1 on Monday, and program 2 on Friday -- did you expect the results to show 1 after 4 days of your computer running thousands of processes? Commented Jun 13 at 18:15
  • 3
    In order to read the memory of another process on Windows (under certain limitations), you can have a look at ReadProcessMemory.
    – wohlstad
    Commented Jun 13 at 18:20
  • 2
    If this was allowed freely by the OS, there is no point in writing secure and safe programs because another program will easily read confidential data handled by another. Hold that thought for a moment. The other program whose data will be read has to give permission don’t you think? Commented Jun 13 at 18:20

1 Answer 1

3

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).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.