Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MemoryManager
- {
- #region DLLImports
- [DllImport("kernel32.dll")]
- public static extern IntPtr OpenProcess(uint dwDesiredAccess, bool bInheritHandle, int dwProcessId);
- [DllImport("kernel32.dll")]
- public static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] buffer, int size, int lpNumberOfBytesRead);
- [DllImport("kernel32.dll", SetLastError = true)]
- public static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, IntPtr lpBuffer, int nSize, out IntPtr lpNumberOfBytesWritten);
- [DllImport("kernel32.dll", SetLastError = true)]
- public static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] lpBuffer, int nSize, out IntPtr lpNumberOfBytesWritten);
- [DllImport("psapi.dll", CallingConvention = CallingConvention.StdCall, SetLastError = true)]
- public static extern int EnumProcessModulesEx(IntPtr hProcess, [Out] IntPtr lphModule, uint cb, out uint lpcbNeeded, UIntPtr dwFilterFlags);
- [DllImport("psapi.dll")]
- static extern uint GetModuleFileNameEx(IntPtr hProcess, IntPtr hModule, [Out] StringBuilder lpBaseName, [In] [MarshalAs(UnmanagedType.U4)] uint nSize);
- [DllImport("psapi.dll", SetLastError = true)]
- static extern bool GetModuleInformation(IntPtr hProcess, IntPtr hModule, out MODULEINFO lpmodinfo, uint cb);
- public struct MODULEINFO
- {
- public IntPtr lpBaseOfDll;
- public uint SizeOfImage;
- public IntPtr EntryPoint;
- }
- #endregion
- #region Offsets
- #endregion
- public static int processID = 0;
- public static IntPtr processHandle = (IntPtr)0;
- public static IntPtr clientPosition = (IntPtr)0;
- public static IntPtr enginePosition = (IntPtr)0;
- public static IntPtr hWnd { get; set; }
- public static void Init(int pid)
- {
- processID = pid;
- processHandle = OpenProcess(2035711, false, pid);
- Process csProc = Process.GetProcessById(pid);
- hWnd = csProc.MainWindowHandle;
- IntPtr[] hMods = new IntPtr[1024];
- GCHandle gch = GCHandle.Alloc(hMods, GCHandleType.Pinned);
- IntPtr pModules = gch.AddrOfPinnedObject();
- uint uiSize = (uint)(Marshal.SizeOf(typeof(IntPtr)) * (hMods.Length));
- uint cbNeeded = 0;
- if (EnumProcessModulesEx((IntPtr)csProc.Handle, pModules, uiSize, out cbNeeded, (UIntPtr)0x01) == 1)
- {
- Int32 uiTotalNumberofModules = (Int32)(cbNeeded / (Marshal.SizeOf(typeof(IntPtr))));
- for (int i = 0; i < (int)uiTotalNumberofModules; i++)
- {
- StringBuilder strbld = new StringBuilder(1024);
- GetModuleFileNameEx((IntPtr)csProc.Handle, hMods[i], strbld, (uint)(strbld.Capacity));
- if (strbld.ToString().Substring(strbld.ToString().Length - DecodeB64("Y2xpZW50LmRsbA==").Length, DecodeB64("Y2xpZW50LmRsbA==").Length).Equals(DecodeB64("Y2xpZW50LmRsbA==")))
- {
- MODULEINFO ClientModule = new MODULEINFO();
- GetModuleInformation((IntPtr)csProc.Handle, hMods[i], out ClientModule, uiSize);
- clientPosition = ClientModule.lpBaseOfDll;
- }
- if (strbld.ToString().Substring(strbld.ToString().Length - DecodeB64("ZW5naW5lLmRsbA==").Length, DecodeB64("ZW5naW5lLmRsbA==").Length).Equals(DecodeB64("ZW5naW5lLmRsbA==")))
- {
- MODULEINFO ClientModule = new MODULEINFO();
- GetModuleInformation((IntPtr)csProc.Handle, hMods[i], out ClientModule, uiSize);
- enginePosition = ClientModule.lpBaseOfDll;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment