Changing a memory value.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Spippo
    New Member
    • Nov 2007
    • 15

    Changing a memory value.

    Hi all,

    I'm trying to create a trainer for a flash game.
    I have an AxShockwaveFlas h component to play the flash game in a windows form.
    With an other program (cheat engine) I found the memory place of the money-value in the game.
    How can I change this value in C# .NET? When I click a button for example.

    Thanks
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    You will have to use some unsafe code to do it.
    Possible even create an unmanaged DLL that contains methods for doing that.

    Comment

    • Spippo
      New Member
      • Nov 2007
      • 15

      #3
      Yes, I already thought it would be unsave. Writing directly into a programs memory, but any idea how to do this?

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        Honestly, I recomend using an unmanged DLL to do it. Less messy then trying to do unsafe stuff.
        Create a function that say takes in two integers. One integer will be an address, one will be the value to set it to. Your unmanaged function would set the memory to be that value.
        That way your C# just needs to know the memory location and the value.

        Comment

        • Spippo
          New Member
          • Nov 2007
          • 15

          #5
          OK, I found something that works for writing into the memory:

          To use the C/C++ functions I called these parts:
          Code:
          [DllImport("kernel32.dll")]
          public static extern Int32 CloseHandle(IntPtr hObject);
          [DllImport("kernel32.dll")]
          public static extern IntPtr OpenProcess(UInt32 dwDesiredAccess, Int32 bInheritHandle, UInt32 dwProcessId);
          [DllImport("kernel32.dll")]
          public static extern Int32 WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, uint[] lpBuffer, UInt32 nSize, IntPtr lpNumberOfBytesWritten);
          [DllImport("kernel32.dll")]
          public static extern Int32 ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, [In, Out] byte[] buffer, UInt32 size, out IntPtr lpNumberOfBytesRead);
          And I used them as followed:
          Code:
          Process proc = Process.GetCurrentProcess();
          IntPtr procHandle = OpenProcess(0x1F0FFF, 0, (UInt32)proc.Id);
          WriteProcessMemory(procHandle, (IntPtr)0x038C3148, new uint[] { 0x08 }, 4, (IntPtr)0);
          byte[] b = null;
          IntPtr i;
          if((ReadProcessMemory(procHandle, (IntPtr)0x038C3148, b, 4, out i) != 0))
          		   MessageBox.Show(b.ToString());
          The writing works (because the money value changes), but I don't get a messagebox, so the reading doesn't work. What am I doing wrong?
          To 0x1F0FFF parameter to open the handle is "enables all possible access rights to the process specified by the Process ID."

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            The return value is the value it is reading from the memory address right?
            And the out variable tells you how many bytes it's reading, is it reading any bytes?

            Comment

            • Spippo
              New Member
              • Nov 2007
              • 15

              #7
              no, the variable 'i' is always 0

              Comment

              • Plater
                Recognized Expert Expert
                • Apr 2007
                • 7872

                #8
                It might have something to do with how you initialized b. Seems like b is supposed to return the data read?
                Try setting b to an array that has enough bytes to read, then see if i changes?

                Comment

                • Spippo
                  New Member
                  • Nov 2007
                  • 15

                  #9
                  Nope, still doesn't return anything and i stays 0

                  Comment

                  Working...