0

Currently I'm writing a game trainer using C# (note that I'm only making one for fun, for a private server, and NOT for hacking the game to become the "best player ever") and it's working smoothly, but not the string.

When I write the string for the first time and I have 10 chars, it's working (it'll write for example: hellolady!). When I type 8 chars (for example hellolol) it will automatically write 10 chars, so the new string would be hellololy!.

I don't know why I get the problem, this is my WriteString:

public static bool WriteString(IntPtr handle, int address, string value)
{
    int written;

    byte[] data = Encoding.Default.GetBytes(value);

    return WriteProcessMemory(handle, address, data, data.Length, out written);
}

My WriteProcessMemory:

[DllImport("Kernel32.dll")]
static extern bool WriteProcessMemory(IntPtr handle, int lpBaseAddress, byte[] lpBuffer, int nSize, out  int lpNumberOfBytesWritten);

Hopefully somebody can help me with it.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
Joshua Bakker
  • 2,288
  • 3
  • 30
  • 63
  • 2
    10 - 8 is 2, so you presumably leave the 2 chars from the first write as they are in situ. Pad with a terminating \0 or whitespace – Alex K. Apr 08 '15 at 17:12
  • 2
    Try adding null terminator at the end of the string - http://stackoverflow.com/a/2794356/2158970 – Yuraj Apr 08 '15 at 17:13
  • Hmm, I think that works but I think it's a workaround. I will wait for people to answer and if not, I'll use your method. But adding a lot of whitespaces is kind of messy in my opinion. Thanks anyway. – Joshua Bakker Apr 08 '15 at 17:13
  • 1
    What is the total size of the buffer you use for this string? Note that if you want to change the string, you should rewrite the whole area in the memory that is used for this string. So, if you deal with 10-characters strings, you should always read and write the whole buffer, all tha 10 bytes. And if new string is less than 10 bytes length, just pad it with \0 – ZuoLi Apr 08 '15 at 17:15
  • 3
    Null-terminating is most likely what you are looking for. `Encoding.GetBytes` does not add a null terminator. – Vercas Apr 08 '15 at 17:16
  • But then I have to read the string length first right, and use that as buffer length and read the original value. Then when writing adding null terminator. Or do I just misunderstand it? – Joshua Bakker Apr 08 '15 at 17:24
  • 1
    [`Encoding.Default.GetBytes(value + "\0")`](https://stackoverflow.com/questions/15929998/how-to-convert-string-to-null-terminated-one) – dbc Apr 08 '15 at 17:37
  • Thanks everybody, this is working! – Joshua Bakker Apr 08 '15 at 17:40
  • 2
    @user3599496 Please post what changes you made as an answer to your own question and you can accept it in two days. Don't just post it as a edit to the question. This is a decent question and it would be nice to have a good accepted answer to go with it. – Scott Chamberlain Apr 08 '15 at 17:52

1 Answers1

0

Edit of the function which works, you have to use a null terminator at GetBytes, then it'll work.

public static bool WriteString(IntPtr handle, int address, string value)
{
    int written;

    byte[] data = Encoding.Default.GetBytes(value + "\0");

    return WriteProcessMemory(handle, address, data, data.Length, out written);
}
Joshua Bakker
  • 2,288
  • 3
  • 30
  • 63