Advertisement
rimsky82

trainer- basic trainer functions

Apr 17th, 2012
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.44 KB | None | 0 0
  1.         [System.Runtime.InteropServices.DllImport("kernel32.dll")]
  2.         private static extern bool ReadProcessMemory(int hProcess, int lpBaseAddress, byte[] buffer, int size, int lpNumberOfBytesRead);
  3.         [System.Runtime.InteropServices.DllImport("kernel32.dll")]
  4.         private static extern bool WriteProcessMemory(int hProcess, int lpBaseAddress, byte[] buffer, int size, int lpNumberOfBytesWritten);
  5.         [System.Runtime.InteropServices.DllImport("kernel32.dll")]
  6.         private static extern int OpenProcess(uint dwDesiredAccess, bool bInheritHandle, int dwProcessId);
  7.         [System.Runtime.InteropServices.DllImport("kernel32.dll")]
  8.         private static extern bool CloseHandle(int hObject);
  9.  
  10.         public static int ProcessHandle = 0;
  11.         public static int BaseAddress = 0;
  12.  
  13.         private bool findProcess()
  14.         {
  15.             Process[] p = Process.GetProcessesByName("name");
  16.             if (p.Length != 0)
  17.             {
  18.                 ProcessHandle = (int)OpenProcess(0x1F0FFF, false, p[0].Id);
  19.                 BaseAddress = (int)p[0].MainModule.BaseAddress;
  20.                 return true;
  21.             }
  22.             else return false;
  23.         }
  24.  
  25.         private int findAddress(int StaticPointer, int[] Offsets)
  26.         {
  27.             byte[] tmp;
  28.             int address = BaseAddress + StaticPointer;
  29.             for (int i = 0; i < Offsets.Count(); i++)
  30.             {
  31.                 tmp = new byte[4];
  32.                 ReadProcessMemory(ProcessHandle, address, tmp, tmp.Length, 0);
  33.                 address = BitConverter.ToInt32(tmp, 0) + Offsets[i];
  34.             }
  35.             return address;
  36.         }
  37.  
  38.         private void writeMemory(int address, int value, bool flt = false, int length = 4)
  39.         {
  40.             if (write)
  41.             {
  42.                 byte[] bytes = new byte[4];
  43.                 if (flt) bytes = BitConverter.GetBytes(Convert.ToSingle(value));
  44.                 else bytes = BitConverter.GetBytes(value);
  45.                 WriteProcessMemory(ProcessHandle, address, bytes, length, 0);
  46.             }
  47.         }
  48.  
  49.         private int readMemory(int address, bool flt = false, int length = 4)
  50.         {
  51.             int value;
  52.             byte[] tmp = new byte[4];
  53.             ReadProcessMemory(ProcessHandle, address, tmp, length, 0);
  54.             if (flt) value = (int)BitConverter.ToSingle(tmp, 0);
  55.             else value = BitConverter.ToInt32(tmp, 0);
  56.             return value;
  57.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement