Search code examples
c#byteintptrreadprocessmemory

How to convert byte[] array to IntPtr?


Possible Duplicate:
How to get IntPtr from byte[] in C#

I'm reading strings from memory with

byte[] array = 
reader.ReadProcessMemory((IntPtr)address, (uint)255, out bytesReadSize);

and then I'm converthing this array to string.

I've got a problem now coz under the address 003A53D4 in program's memory there is a pointer, which points to a string. How can I get string's address? Thanks :)

THATS WHAT I TRIED:

IntPtr pointers_address = new IntPtr(module_base_address + 3822548);
byte[] pointer_arrays = 
reader.ReadProcessMemory(pointers_address, (uint)16, out bytesReadSize2); 
IntPtr pointer_for_string = new IntPtr();
Marshal.Copy(pointers_array, 0, pointer_for_string, 16);

It says (about 4th line):

Value cannot be null. Parameter name: destination

and when I change new IntPtr() to new IntPtr(1) it says

Attempted to read or write protected memory. This is often an indication that other memory is corrupt.


Solution

  • You can use an Encoding.GetString() to convert the bytes to a string. Which encoding to use depends on the encoding of the string, e.g. Encoding.UTF8.GetString(pointer_arrays, 0) for UTF8 encoding, Encoding.Unicode for unicode, Encoding.ASCII for ASCII or Encoding.Default for the default code page of your system.