0

This is my current code I have on a timer

myMemory.ReadProcess = myProcess[0];
myMemory.Open();
int pointerAddress = HexToDec(HealthPointer);
int[] pointerOffset = HealthOffest;

int tempBytesToRead;
uint valueToRead = 8;
byte[] oldHealth = myMemory.PointerRead((IntPtr)pointerAddress, valueToRead, pointerOffset, out tempBytesToRead);
HealthToFill = BitConverter.ToInt32(oldHealth, 0) + 1;


int bytesWritten;
//Get current health
byte[] valueToWrite = BitConverter.GetBytes(HealthToFill);
string writtenAddress = myMemory.PointerWrite((IntPtr)pointerAddress, valueToWrite, pointerOffset, out bytesWritten);
myMemory.CloseHandle();

This code writes to the address just fine but when I try to read it always returns 0. By the way, HealthToFill is a float and yes I have tried BitConverter.ToSingle(oldHealth,0). But I debugged it and it seems the byte array oldHealth isn't getting any data written to it. If the float I'm trying to read can be between 3-6 integers and numerous decimal places what would be a good value to use for uint valueToRead?

Bender Guy
  • 33
  • 1
  • 6

1 Answers1

0

You does not provide enough information to help you. Which library are you using ? What is the class of the var myMemory ? Why do you convert HealthPointer from hex to dec to store it in a integer ?

Nevertheless, the library you are using seems to provide a very shattered API. I would suggest you to have a look to another memory editing library like MemorySharp (I'm the author). Using it, you can write your code like that.

using (var m = new MemorySharp(ApplicationFinder.FromProcessName("app").First()))
{
    var healthLocation = m[m[healthPointer].Read<IntPtr>() + healthOffset];
    var health = healthLocation.Read<float>();
    healthLocation.Write(newValue);
}

EDIT: Browse a multi-leveled pointer using a loop.

var healthPtr = new IntPtr(0xdeadbeef);
var offsets = new[] { 4, 5, 6 };

foreach (var offset in offsets)
{
    healthPtr = m[healthPtr + offset, false].Read<IntPtr>();
}
Jämes
  • 6,945
  • 4
  • 40
  • 56
  • I just realised I was using someone elses memory class. I'll try out yours – Bender Guy Sep 17 '13 at 20:03
  • Ok an issue when using your class, when defining healthLocation, VS shows me an error that reads "Operator '+' cannot be applied to operands of type 'System.IntPtr' and 'int[]'" HealthOffset is an int array as my pointer is multi-leveled pointer – Bender Guy Sep 17 '13 at 20:24
  • You cannot append and array with an IntPtr object. In the case where you have a multi-leveled pointer, you can resolve the final location using a loop. I edited my answer. – Jämes Sep 17 '13 at 21:57
  • Alight I tried it and here is my code so far [link](http://pastebin.com/FWcgFqdZ) but I get this error thrown to me "An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in MemorySharp.dll Additional information: The relative address cannot be greater than the main module size."---- This error occurs on line 8 of the pastepin link I provided. Not too sure why this is occuring – Bender Guy Sep 17 '13 at 23:52
  • **EDIT:** [link](http://pastebin.com/7ShCEGMY) <--- UPDATED CODE | I added all the variables in there that way no information is left out but still getting error – Bender Guy Sep 18 '13 at 00:07
  • The OP created another post for this issue: http://stackoverflow.com/questions/18863518/memorysharp-setting-offset-to-an-address-not-working/18878344#18878344 – Jämes Sep 18 '13 at 17:14