How to retrieving variable value in C++ if you kno

2020-04-15 15:14发布

Greetings,

I have recently started to code in C++ and have come across a problem to which I was unable to find the answer, so I thought maybe somebody else might know the answer.

Is it possible to retrieve a variable value from another program if you know a variable address? Imagine that I have a memory address displayed in a program, something like: 0x7fff5fbff758 and I would like (in my own program which is not related to the first one) to get the value stored in that memory address.

Is that possible? If so, could somebody please explain me how. Thank you in advance.

5条回答
Rolldiameter
2楼-- · 2020-04-15 15:34

On today operating systems, the programs handle virtual addresses, not physical ones. Shortly, a specific address for one programs will not point to the same physical location for other programs.

To do what you want on modern operating systems, you can, for instance, set up a shared memory location.

But there is a lot of easier way to pass a value from one program to another.

If you are just wondering that out of curiosity, that's a good question, you can look at what "virtual memory" is.

查看更多
干净又极端
3楼-- · 2020-04-15 15:40

Edit: This obsolete, because the question has changed.

If you know the type of the variable, its possible.

For an int variable, you need to insert lines like

int* addr = (int*)0x7fff5fbff758;
std::cout << *addr << std::endl;

somewhere in the affected program.

Accessing the variable from a different program is generally not easily done in a modern operating system, each process has it's own address space so the same address in different processes may map to different physical memory location.

It depends on the OS, for example in linux you need to trace a process if you want to do it from a different process, see man ptrace. You can read the data in this case with PTRACE_PEEKDATA.

查看更多
Rolldiameter
4楼-- · 2020-04-15 15:44

On most modern general-purpose OSes (Windows, Linux, etc), you cannot do that. Different programs run in different processes, and each process has its very own memory space. Address 0x7fff5fbff758 in one probably points to a very different place in RAM than address 0x7fff5fbff758 in another (if that address even exists in the other).

This is why modern OSes have interprocess communications mechanisims, like pipes, shared memory, COM, etc.

查看更多
戒情不戒烟
5楼-- · 2020-04-15 15:49

It is possible, but it is OS-specific (there is no common C support for it). In general, your second program needs to have the permission that debugger has, and use the same kind of OS calls that a debugger uses.

查看更多
Animai°情兽
6楼-- · 2020-04-15 15:55

C++ has no comment on this, one way or the other. It depends entirely on the platform on which your program is running. If you're using Windows, for example, you can use the ReadProcessMemory() function to read the memory of another process (assuming you have adequate permissions).

Note that modern operating systems are designed to protect processes from interfering with each other. One of the ways they do this is by giving each process its own address space. Processes can't access memory outside this space without using special APIs.

查看更多
登录 后发表回答