Advertisement
AlFas

Memory functions

Jul 20th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.51 KB | None | 0 0
  1.     public static class Memory
  2.     {
  3.         public static int GetIntFromPointers(int baseAddress, params int[] offsets)
  4.         {
  5.             int value = BitConverter.ToInt32(MemoryEdit.ReadMemory(baseAddress, 4, (int)EffectSome.processHandle), 0);
  6.             for (int i = 0; i < offsets.Length; i++)
  7.                 value = BitConverter.ToInt32(MemoryEdit.ReadMemory(value + offsets[i], 4, (int)EffectSome.processHandle), 0);
  8.             return value;
  9.         }
  10.         public static float GetFloatFromPointers(int baseAddress, params int[] offsets)
  11.         {
  12.             int value = BitConverter.ToInt32(MemoryEdit.ReadMemory(baseAddress, 4, (int)EffectSome.processHandle), 0);
  13.             for (int i = 0; i < offsets.Length - 1; i++)
  14.                 value = BitConverter.ToInt32(MemoryEdit.ReadMemory(value + offsets[i], 4, (int)EffectSome.processHandle), 0);
  15.             float result = BitConverter.ToSingle(MemoryEdit.ReadMemory(value + offsets[offsets.Length - 1], 4, (int)EffectSome.processHandle), 0);
  16.             return result;
  17.         }
  18.         public static bool GetBoolFromPointers(int baseAddress, params int[] offsets)
  19.         {
  20.             int value = BitConverter.ToInt32(MemoryEdit.ReadMemory(baseAddress, 4, (int)EffectSome.processHandle), 0);
  21.             for (int i = 0; i < offsets.Length - 1; i++)
  22.                 value = BitConverter.ToInt32(MemoryEdit.ReadMemory(value + offsets[i], 4, (int)EffectSome.processHandle), 0);
  23.             bool result = BitConverter.ToBoolean(MemoryEdit.ReadMemory(value + offsets[offsets.Length - 1], 4, (int)EffectSome.processHandle), 0);
  24.             return result;
  25.         }
  26.  
  27.         public static void SetValueFromPointers(int baseAddress, byte[] bytes, params int[] offsets)
  28.         {
  29.             int address = BitConverter.ToInt32(MemoryEdit.ReadMemory(baseAddress, 4, (int)EffectSome.processHandle), 0);
  30.             for (int i = 0; i < offsets.Length - 1; i++)
  31.                 address = BitConverter.ToInt32(MemoryEdit.ReadMemory(address + offsets[i], 4, (int)EffectSome.processHandle), 0);
  32.             address += offsets[offsets.Length - 1];
  33.             MemoryEdit.WriteMemory(address, bytes, (int)EffectSome.processHandle);
  34.         }
  35.         public static void SetIntFromPointers(int baseAddress, int value, params int[] offsets)
  36.         {
  37.             int address = BitConverter.ToInt32(MemoryEdit.ReadMemory(baseAddress, 4, (int)EffectSome.processHandle), 0);
  38.             for (int i = 0; i < offsets.Length - 1; i++)
  39.                 address = BitConverter.ToInt32(MemoryEdit.ReadMemory(address + offsets[i], 4, (int)EffectSome.processHandle), 0);
  40.             address += offsets[offsets.Length - 1];
  41.             MemoryEdit.WriteMemory(address, BitConverter.GetBytes(value), (int)EffectSome.processHandle);
  42.         }
  43.         public static void SetFloatFromPointers(int baseAddress, float value, params int[] offsets)
  44.         {
  45.             int address = BitConverter.ToInt32(MemoryEdit.ReadMemory(baseAddress, 4, (int)EffectSome.processHandle), 0);
  46.             for (int i = 0; i < offsets.Length - 1; i++)
  47.                 address = BitConverter.ToInt32(MemoryEdit.ReadMemory(address + offsets[i], 4, (int)EffectSome.processHandle), 0);
  48.             address += offsets[offsets.Length - 1];
  49.             MemoryEdit.WriteMemory(address, BitConverter.GetBytes(value), (int)EffectSome.processHandle);
  50.         }
  51.  
  52.         public static void EditInt(int baseAddress, NumericUpDown NUD, params int[] offsets)
  53.         {
  54.             SetValueFromPointers(baseAddress, BitConverter.GetBytes(GetIntFromPointers(baseAddress, offsets) + (int)NUD.Value), offsets);
  55.         }
  56.         public static void EditFloat(int baseAddress, NumericUpDown NUD, params int[] offsets)
  57.         {
  58.             SetValueFromPointers(baseAddress, BitConverter.GetBytes(GetIntFromPointers(baseAddress, offsets) + (float)NUD.Value), offsets);
  59.         }
  60.     }
  61.     public static class MemoryEdit
  62.     {
  63.         [DllImport("kernel32.dll")]
  64.         public static extern IntPtr OpenProcess(int dwDesiredAccess, bool bInheritHandle, int dwProcessId);
  65.  
  66.         [DllImport("Kernel32.dll", SetLastError = true)]
  67.         public static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] lpBuffer, uint nSize, ref uint lpNumberOfBytesRead);
  68.  
  69.         [DllImport("kernel32.dll")]
  70.         public static extern bool WriteProcessMemory(int hProcess, int lpBaseAddress, byte[] buffer, int size, ref uint lpNumberOfBytesWritten);
  71.  
  72.         [DllImport("kernel32.dll")]
  73.         public static extern bool VirtualProtectEx(IntPtr hProcess, IntPtr lpAddress, UIntPtr dwSize, uint flNewProtect, out uint lpflOldProtect);
  74.  
  75.         public static byte[] ReadMemory(int address, int processSize, int processHandle)
  76.         {
  77.             byte[] buffer = new byte[processSize];
  78.             uint shit = 0;
  79.             ReadProcessMemory(new IntPtr(processHandle), new IntPtr(address), buffer, (uint)processSize, ref shit);
  80.             return buffer;
  81.         }
  82.         public static void WriteMemory(int address, byte[] processBytes, int processHandle)
  83.         {
  84.             uint shit = 0;
  85.             ChangeMemoryProtection(address, (uint)processBytes.Length, processHandle);
  86.             WriteProcessMemory(processHandle, address, processBytes, processBytes.Length, ref shit);
  87.         }
  88.         public static void ChangeMemoryProtection(int address, uint size, int processHandle)
  89.         {
  90.             VirtualProtectEx(new IntPtr(processHandle), new IntPtr(address), new UIntPtr(size), EffectSome.PAGE_READWRITE, out uint shit);
  91.         }
  92.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement