/// <summary> /// Write an object's data to the process memory at ptr. /// </summary> /// <param name="obj"></param> /// <param name="ptr"></param> /// <param name="size"></param> public void Write(object obj, int size, IntPtr ptr) { using (LP_Pinner pin = new LP_Pinner(obj)) { uint bytesWritten = 0; if (!WriteProcessMemory(hProcess, ptr, pin.Ptr, size, ref bytesWritten)) { int err = GetLastError(); string s = "Write failed; err=" + err + "; bytesWritten=" + bytesWritten; throw new ApplicationException(s); } } }
/// <summary> /// Reads an object's data from the process memory at ptr. /// </summary> /// <param name="obj">Must be a reference type (no struct!)</param> /// <param name="ptr"></param> public void Read(object obj, IntPtr ptr) { using (LP_Pinner pin = new LP_Pinner(obj)) { uint bytesRead = 0; int size = Marshal.SizeOf(obj); if (!ReadProcessMemory(hProcess, ptr, pin.Ptr, size, ref bytesRead)) { int err = GetLastError(); string s = "Read failed; err=" + err + "; bytesRead=" + bytesRead; throw new ApplicationException(s); } } }