Esempio n. 1
0
 /// <summary>
 ///     Writes data to an area of memory in a specified process.
 /// </summary>
 /// <param name="value">
 ///     The structure to be allocated.
 /// </param>
 /// <param name="size">
 ///     The number of bytes to be written to the specified process.
 /// </param>
 /// <param name="buffer">
 ///     A pointer to the buffer that contains data to be written in the address
 ///     space of the specified process.
 /// </param>
 public void Write(object value, int size, IntPtr buffer)
 {
     try
     {
         using var pin = new MemoryPinner(value);
         if (WinApi.NativeMethods.WriteProcessMemory(_hProcess, buffer, pin.PointerHandle, size, out var bytesWritten))
         {
             return;
         }
         throw new MemoryException(ExceptionMessages.BytesWriteFailed + bytesWritten);
     }
     catch (Exception ex) when(ex.IsCaught())
     {
         Log.Write(ex);
     }
 }
Esempio n. 2
0
 /// <summary>
 ///     Reads data from an area of memory in a specified process.
 /// </summary>
 /// <param name="value">
 ///     The structure to be allocated.
 /// </param>
 /// <param name="address">
 ///     A pointer to the base address in the specified process from which to read.
 /// </param>
 public void Read(object value, IntPtr address)
 {
     try
     {
         using var pin = new MemoryPinner(value);
         var bytesRead = IntPtr.Zero;
         var size      = new IntPtr(Marshal.SizeOf(value));
         if (WinApi.NativeMethods.ReadProcessMemory(_hProcess, address, pin.PointerHandle, size, ref bytesRead))
         {
             return;
         }
         throw new MemoryException(ExceptionMessages.BytesReadFailed + bytesRead);
     }
     catch (Exception ex) when(ex.IsCaught())
     {
         Log.Write(ex);
     }
 }