2

(Sorry for my bad English )

How to read a value address from pointer in C#?

Example: I know my pointer but the value change at application starting.

1) Start

(Pointer) 0x0018F36C   =  ( Value) 0x0342AD68

2) Restart

(Pointer) 0x0018F36C  = ( Value Changed ) 0x0342AE20

Actually i have a base address 0x0018F36C but need to read value from pointer and save in long

example:

long addr_base = 0x0018F36C;
long address;  //Obviously I do not know the Address

now i need to read long value from addr_base and put the value (long) in address

example

addr_base = memory.ReadAddress(addr_base)

anyone know how to read address stored in the variable called addr_base?

5
  • 1
    I'm very dubious as to why you are doing this... How do you get the value of the pointer in the first place? Does it come as a return value from an unmanaged function? There might be a better way to do this. (I'm predicting that you will try using Marshal.ReadIntPtr() but it won't do what you expect...) Commented May 27, 2015 at 7:51
  • Im get pointer from disassembler. I'm doing this to create a plugin to an application. Commented May 27, 2015 at 8:27
  • So you're trying to read memory from a different process? Is the application from which you got the pointer running in a separate process from your C# code? (I guess it must be...) If so, then this isn't going to work. Furthermore, the pointer is likely to change between runs. Commented May 27, 2015 at 8:33
  • Yes. the value of pointer change at run. But the address at pointer is same. dont change. Just need to obtain new addres value from the pointer Commented May 27, 2015 at 8:51
  • 1
    But you will not be able to use Marshal.ReadIntPtr() to read memory from another process. Commented May 27, 2015 at 8:55

1 Answer 1

10

If you have a an address like 0x0018F36C, you can:

IntPtr ptr = (IntPtr)0x0018F36C;
long longValue = Marshal.ReadInt64(ptr);

If from an address you need to read another address, there is another Marshal method:

IntPtr ptr = (IntPtr)0x0018F36C;
IntPtr ptr2 = Marshal.ReadIntPtr(ptr);
Sign up to request clarification or add additional context in comments.

2 Comments

Hello xanatos tanks for help. the frist code work but the second example i have error type: Attempted to read or write protected memory . This is often an indication that other parts of memory are damaged .
@JoeMartiniello, The error is confusing, but it just means you are trying to do something invalid, like an address in memory that never was a valid address in the first place (you start with a fixed value, that is never right). Working and manipulating pointers from a managed application like C# requires a good understanding of marshalling and pinning of addresses, and knowing what volatile can do, to do right. Since you came from assembler, perhaps it is easier in your case to start out with C and C++ (even a mixed mode assembly, if you like), and write a wrapper. It'll feel easier.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.