-3

I have an open c program with a random var a=20 stored in the address. Let's say 1000. I want my c# program to be able to read this address (1000) value. So in my c# program the value 20 is displayed. I already have the address, but I don't know how to see this address value, any help ?

int *ptr = (int *) 0x67a9;
*ptr = 0xaa55;

I found this c code thinking it would help.

I'm using Visual Studio 2015.

devlin carnate
  • 8,309
  • 7
  • 48
  • 82
Joe Kerr
  • 34
  • 9

1 Answers1

2

So you want your C to store a value in memory address 1000 and you want your C# program to be able to read memory address 1000 and see the same value?

Unfortunately (for you) it doesn't work that way - each exe has it's own address space, so address 1000 in each one will map to a different physical address, and you shouldn't be mucking around down at that level.

There are several inter process communication (IPC) methods you could look at to achieve what you want (like shared memory)

edit David Heffernan correctly points out that ReadProcessMemory in the comments does allow you to map an area of memory from one process to another. I must admit I didn't know about it! There is still the issue that you can't just use memory address 1000 - you somehow have to get a valid address from one side to the other, so I'm not really sure how useful it is for the general case. This may give you some more hints: sharing memory between two applications

Community
  • 1
  • 1
John3136
  • 28,809
  • 4
  • 51
  • 69
  • That makes a lot of sense, but what if i get process handle, the address 1000 will be the same for both? Also thanks, i'll read all about it – Joe Kerr Feb 23 '16 at 03:56
  • A handle is typically just an ID, not actually an address. Again. Both processes have their own address space. Even if you do use shared memory, the same block might be mapped to address 1000 in the first process and address 2020 in the second. – John3136 Feb 23 '16 at 04:04
  • Both processes will have address 1000 (as well as any address 0-2^32 for x86), but there is no guarantee that either of processes will have memory mapped to that part of address space and chances of having the same information for different processes are basically 0. – Alexei Levenkov Feb 23 '16 at 04:04
  • This is not complete. ReadProcessMemory exists and does what you say cannot be done. – David Heffernan Feb 23 '16 at 07:17