Results 1 to 2 of 2
  1. #1
    Rpetty84's Avatar
    Join Date
    Sep 2012
    Gender
    male
    Posts
    35
    Reputation
    10
    Thanks
    92

    Whats wrong with this C# WPM method?

    First off I know my offsets are correct (0x01C2EF54, 0x458, 0x0, 0x350, 0x584) because the ReadProcessMemory works fine and dandy. But im having a problem with Writing to address using the same offsets above, and im tired of banging my head on my desk trying to figure it out. So if any of you kind folks care to take a look it be most appreciated. How im trying to use is at the bottom.


    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Runtime.InteropServices;
    using System.Diagnostics;
    using System.Windows.Forms;
    
    namespace MemoryAPI
    {
        public class MemoryEditor
        {
    
            public Process[] _TargetProcess;
            public int _ProcessId;
            public IntPtr _ProcessHandle;
    
            public void GetTargetProcess(string process_name)
            {
                try
                {
                    _TargetProcess = Process.GetProcessesByName(process_name);
                    _ProcessId = _TargetProcess[0].Id;
                }
                catch (Exception)
                {
                    MessageBox.Show(null, "Sorry, target process " + process_name + " Could not be found. Please make sure desired application is running",
                        "Target Process Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
    
    
            public void OpenProcess()
            {
                try
                {
                    _ProcessHandle = OpenProcess((uint)RequiredAccess.All, false, _ProcessId);
                }
                catch (Exception)
                {
                    MessageBox.Show(null, "failed to open target process", "Target Process Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
    
            public void CloseProcess()
            {
                if (_ProcessHandle != null)
                {
                    CloseHandle(_ProcessHandle);
                }
            }
    
    
            public byte[] ReadMemoryWithOffsets(int[] offsets)
            {
                byte[] value = new byte[20];
                byte[] tmpAddressBuffer = new byte[4];
                IntPtr tmpAddress = IntPtr.Zero;
                int count = offsets.Length - 1;
    
    
                OpenProcess();
    
                if (count == 0)//Only 1 Offset
                {
                    ReadProcessMemory(_ProcessHandle, IntPtr.Add(_TargetProcess[0].MainModule.BaseAddress, offsets[0]), value, 20, 0);
                    CloseProcess();
                    return value;
                }
    
                for (int i = 0; i <= count; i++)
                {
                    if (i == count)
                    {
                        ReadProcessMemory(_ProcessHandle, IntPtr.Add(tmpAddress, offsets[offsets.Length - 1]), value, 20, 0);
                        tmpAddress = IntPtr.Add(tmpAddress, offsets[i]);
                        CloseProcess();
                        return value;
                    }
    
                    else if (i == 0)
                    {
                        ReadProcessMemory(_ProcessHandle, IntPtr.Add(_TargetProcess[0].MainModule.BaseAddress, offsets[i]), tmpAddressBuffer, 4, 0);
                        tmpAddress = (IntPtr)BitConverter.ToInt32(tmpAddressBuffer, 0);
                    }
                    else
                    {
                        ReadProcessMemory(_ProcessHandle, IntPtr.Add(tmpAddress, offsets[i]), tmpAddressBuffer, 4, 0);
                        tmpAddress = (IntPtr)BitConverter.ToInt32(tmpAddressBuffer, 0);
                    }
                }
    
                CloseProcess();
                return value;
    
            }
    
    
            public void WriteMemoryWithOffsets(int[] offsets, byte[] bytesToWrite)
            {
    
                byte[] value = new byte[20];
                byte[] tmpAddressBuffer = new byte[4];
                IntPtr tmpAddress = IntPtr.Zero;
                int count = offsets.Length - 1;
    
                OpenProcess();
    
                if (count == 0)
                {
                    ReadProcessMemory(_ProcessHandle, IntPtr.Add(_TargetProcess[0].MainModule.BaseAddress, offsets[0]), tmpAddressBuffer, 4, 0);
                }
    
                for (int i = 0; i <= count; i++)
                {
    
                    if (i == count)
                    {
                        WriteProcessMemory(_ProcessHandle, IntPtr.Add(_TargetProcess[0].MainModule.BaseAddress, offsets[i]), bytesToWrite, bytesToWrite.Length, 0);
                    }
    
                    else if (i == 0)
                    {
                        ReadProcessMemory(_ProcessHandle, IntPtr.Add(_TargetProcess[0].MainModule.BaseAddress, offsets[i]), tmpAddressBuffer, 4, 0);
                        tmpAddress = (IntPtr)BitConverter.ToInt32(tmpAddressBuffer, 0);
                    }
    
                    else
                    {
                        ReadProcessMemory(_ProcessHandle, IntPtr.Add(tmpAddress, offsets[i]), tmpAddressBuffer, 4, 0);
                        tmpAddress = (IntPtr)BitConverter.ToInt32(tmpAddressBuffer, 0);
                    }
                }
    
                CloseProcess();
            }
    
            //Access Flags
            [Flags]
            public enum RequiredAccess : uint
            {
                All = 0x001F0FFF,
                Terminate = 0x00000001,
                CreateThread = 0x00000002,
                VMOperation = 0x00000008,
                VMRead = 0x00000010,
                VMWrite = 0x00000020,
                DupHandle = 0x00000040,
                SetInformation = 0x00000200,
                QueryInformation = 0x00000400,
                Synchronize = 0x00100000,
            }
    
            //DLL IMPORTS
            [DllImport("kernel32.dll")]
            public static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] buffer, int size, int lpNumberOfBytesRead);
    
    
            [DllImport("kernel32.dll")]
            public static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] buffer, int size, int lpNumberOfBytesWritten);
    
            [DllImport("kernel32.dll")]
            public static extern IntPtr OpenProcess(uint dwDesiredAccess, bool bInheritHandle, int dwProcessId);
    
            [DllImport("kernel32.dll")]
            public static extern Int32 CloseHandle(IntPtr hProcess);
    
        }
    }

    So i load my offset array like this:
    Code:
    public static int[] CamDistOffsets = new int[] { 0x01C2EF54, 0x458, 0x0, 0x350, 0x584 };
    and im trying to write to memory inside a trackbar value change event. (the event is working because thats how i tested my RPM method, value returned is correct(matches in game value)).
    But like i said above when i try to write to same address the changes do not take place. (tested value change with CE and it works fine, Camera Zooms out as attended), but cant get same results with the WPM.

    Oh and if your wondering, the trackbar changes the string in a textbox, and the textbox is the value im trying to WPM.

    Code:
     private void tbr_CameraDistance_ValueChanged(object sender, EventArgs e)
            {
                //Write Value to Memory for CameraDistance
                float tbxValue = Convert.ToSingle(tbx_CameraDistance.Text);
                byte[] valueToWrite = BitConverter.GetBytes(tbxValue);
                Memory.WriteMemoryWithOffsets(CamDistOffsets, valueToWrite);
            }

  2. #2
    Rpetty84's Avatar
    Join Date
    Sep 2012
    Gender
    male
    Posts
    35
    Reputation
    10
    Thanks
    92
    LOL solved my own mistake by looking at a previous version of this class i made.(been away for awhile from the code dealing with processes).

    My problem was i was adding offset to module base address when i should of been reading from the tempaddress.
    Code:
    if (i == count)
    {
         WriteProcessMemory(_ProcessHandle, IntPtr.Add(_TargetProcess[0].MainModule.BaseAddress, offsets[i]), bytesToWrite, bytesToWrite.Length, 0);
    }
    and it should of been like this:

    Code:
                    if (i == count)
                    {
                        WriteProcessMemory(_ProcessHandle, IntPtr.Add(tmpAddress, offsets[i]), bytesToWrite, bytesToWrite.Length, 0);
                    }

Similar Threads

  1. whats wrong with this?
    By whitten in forum C++/C Programming
    Replies: 3
    Last Post: 08-21-2009, 07:41 PM
  2. Whats wrong with this?
    By superHackz in forum Visual Basic Programming
    Replies: 1
    Last Post: 05-31-2008, 11:35 AM
  3. Whats wrong with this?
    By superHackz in forum WarRock - International Hacks
    Replies: 1
    Last Post: 05-26-2008, 04:03 AM
  4. Whats wrong with this code ?
    By bohnenbong in forum WarRock - International Hacks
    Replies: 7
    Last Post: 10-26-2007, 06:57 AM
  5. whats wrong with this...
    By NetNavi in forum WarRock - International Hacks
    Replies: 6
    Last Post: 09-03-2007, 01:19 AM

Tags for this Thread