/// <summary> /// Sets the process identifier. /// </summary> /// <param name="ProcId">The process identifier.</param> public void SetProcId(int ProcId) { if (this.ProcessId == ProcId) { return; } if (this.ProcessId > 0) { NativeMemoryHandler.CloseHandle(this.Handle); } if (ProcId < 0) { throw new ArgumentException("ProcId is inferior to 0", nameof(ProcId)); } this.ProcessId = ProcId; if (ProcId == 0) { return; } this.Handle = NativeMemoryHandler.OpenProcess(0x001F0FFF, false, ProcId); }
/// <summary> /// Reads bytes at the specified address and returns it. /// </summary> /// <param name="Address">The address.</param> /// <param name="Size">The size.</param> public byte[] Read(ulong Address, uint Size) { if (this.IsDisposed) { throw new ObjectDisposedException(nameof(BekoEngine), "The memory handler is disposed"); } if (Address >= 0x7FFFFFFFFFFF) { throw new ArgumentException("Address is outside userspace virtual memory range"); } if (Address + Size > 0x7FFFFFFFFFFF) { throw new ArgumentException("Address plus size is outside userspace virtual memory range"); } var Buffer = new byte[Size]; var Read = 0u; if (!NativeMemoryHandler.ReadProcessMemory(this.Handle, Address, Buffer, Size, ref Read)) { throw new Exception("Failed to read memory from remote process"); } return(Buffer); }
/// <summary> /// Reads bytes at the specified address and returns a structure from it. /// </summary> /// <param name="Address">The address.</param> public T Read <T>(ulong Address) { if (this.IsDisposed) { throw new ObjectDisposedException(nameof(BekoEngine), "The memory handler is disposed"); } if (Address >= 0x7FFFFFFFFFFF) { throw new ArgumentException("Address is outside userspace virtual memory range"); } var Size = Marshal.SizeOf <T>(); if (Address + (ulong)Size > 0x7FFFFFFFFFFF) { throw new ArgumentException("Address plus size is outside userspace virtual memory range"); } var Buffer = default(T); var Allocation = Marshal.AllocHGlobal(Size); var Read = 0u; if (Allocation == IntPtr.Zero) { throw new InsufficientMemoryException("Couldn't allocate memory for the buffer"); } var Success = NativeMemoryHandler.ReadProcessMemory(this.Handle, Address, Buffer, (uint)Size, ref Read); if (Success) { Buffer = Marshal.PtrToStructure <T>(Allocation); } Marshal.FreeHGlobal(Allocation); if (!Success) { throw new Exception("Failed to read memory from remote process"); } return(Buffer); }