/// <summary> /// Initializes a new instance of the <see cref="MemoryBase" /> class. /// </summary> /// <param name="proc">The process.</param> /// <remarks>Created 2012-02-15</remarks> protected MemoryBase(Process proc) { if (proc.HasExited) { throw new AccessViolationException("Process: " + proc.Id + " has already exited. Can not attach to it."); } Process.EnterDebugMode(); // Good to set this too if ure using events. proc.EnableRaisingEvents = true; // Since people tend to not realize it exists, we make sure to handle it. proc.Exited += (s, e) => { if (ProcessExited != null) { ProcessExited(s, e); } HandleProcessExiting(); }; Process = proc; proc.ErrorDataReceived += OutputDataReceived; proc.OutputDataReceived += OutputDataReceived; const ProcessAccessFlags a = ProcessAccessFlags.PROCESS_CREATE_THREAD | ProcessAccessFlags.PROCESS_QUERY_INFORMATION | ProcessAccessFlags.PROCESS_SET_INFORMATION | ProcessAccessFlags.PROCESS_TERMINATE | ProcessAccessFlags.PROCESS_VM_OPERATION | ProcessAccessFlags.PROCESS_VM_READ | ProcessAccessFlags.PROCESS_VM_WRITE | ProcessAccessFlags.SYNCHRONIZE; ProcessHandle = Imports.OpenProcess(a, false, proc.Id); ImageBase = Process.MainModule.BaseAddress; }
/// <summary> /// Initializes a new instance of the <see cref="MemoryProtectionOperation"/> class. /// </summary> /// <param name="hProcess">The process handle.</param> /// <param name="address">The address.</param> /// <param name="size">The size.</param> /// <param name="flNewProtect">The fl new protect.</param> /// <remarks> /// Created 2012-01-16 16:34 by Nesox. /// </remarks> public MemoryProtectionOperation(SafeMemoryHandle hProcess, IntPtr address, int size, uint flNewProtect) { _hProcess = hProcess; _address = address; _size = size; #if OOP VirtualProtectEx(_hProcess, _address, size, flNewProtect, out _oldProtect); #else VirtualProtect(_address, size, flNewProtect, out _oldProtect); #endif }
private static extern bool VirtualProtectEx(SafeMemoryHandle hProcess, IntPtr lpAddress, IntPtr dwSize, uint flNewProtect, out uint lpflOldProtect);
private static extern bool ReadProcessMemory(SafeMemoryHandle hProcess, uint dwAddress, IntPtr lpBuffer, int nSize, out int lpBytesRead);
private static extern bool WriteProcessMemory(SafeMemoryHandle hProcess, IntPtr lpBaseAddress, byte[] lpBuffer, int nSize, out int lpNumberOfBytesWritten);
private static extern bool ReadProcessMemory(SafeMemoryHandle hProcess, IntPtr lpBaseAddress, byte *lpBuffer, int dwSize, out int lpNumberOfBytesRead);