/// <summary> /// Writes to the address for size bytes from data. Saves the old value, if desired. /// </summary> /// <param name="address">The address to which a new value will be written.</param> /// <param name="newValue">The new value to be written.</param> /// <param name="options">Various options that determine what happens with the old and new address.</param> /// <returns>Returns the result of WriteProcessMemory().</returns> public bool Write(IntPtr address, byte[] newValue, Patcher.WriteOptions options = WriteOptions.SaveOldValue) { if (!this.IsOpen) { this.Status.Log( "Unable to write memory, because the target process has not been opened.", Logger.Level.HIGH); return(false); } byte[] oldValue = null; if (((uint)options & (uint)WriteOptions.SaveOldValue) > 0) { oldValue = new byte[newValue.Length]; if (!this.Read(address, oldValue)) { this.Status.Log( "There was an error reading from 0x" + address.ToString("x") + " before writing to memory.", Logger.Level.HIGH); return(false); } } uint nbw = 0; if (!WinApi.WriteProcessMemory(this.ProcHandle, address, newValue, (uint)newValue.Length, out nbw)) { this.Status.Log( "Could not write value (" + newValue.ToString() + ") to 0x" + address.ToString("x") + ".", Logger.Level.HIGH); return(false); } Address newAddress = new Address(address, (((uint)options & (uint)WriteOptions.FreezeNewValue) > 0), oldValue, newValue); if ((((uint)options & (uint)WriteOptions.SaveOldValue) > 0) || (((uint)options & (uint)WriteOptions.FreezeNewValue) > 0)) { this.SaveAddress(newAddress); } return(true); }
/// <summary> /// Writes the supplied strsucture to the specified address in memory. /// </summary> /// <typeparam name="T">The type of object to be populated.</typeparam> /// <param name="address">The address containing the data used to populate the object to be returned.</param> /// <param name="newValue">The structure to be written to memory.</param> /// <param name="options">Various options that determine what happens with the old and new address.</param> /// <returns>Returns a structure that has been populated with data from the specified address.</returns> public bool WriteStructure <T>( IntPtr address, T newValue, Patcher.WriteOptions options = WriteOptions.SaveOldValue) { byte[] bytes = newValue.GetBytes(); IntPtr allocMem = WinApi.VirtualAllocEx( this.ProcHandle, address, (uint)bytes.Length, WinApi.MemoryState.MEM_COMMIT | WinApi.MemoryState.MEM_RESERVE, WinApi.MemoryProtect.PAGE_READWRITE); if (allocMem == IntPtr.Zero) { return(false); } uint nbw = 0; return(WinApi.WriteProcessMemory(this.ProcHandle, address, bytes, (uint)Marshal.SizeOf(typeof(T)), out nbw)); }
/// <summary> /// Writes to the address for size bytes from data. Saves the old value, if desired. /// </summary> /// <param name="address">The address to which a new value will be written.</param> /// <param name="newValue">The new value to be written.</param> /// <param name="options">Various options that determine what happens with the old and new address.</param> /// <returns>Returns the result of WriteProcessMemory().</returns> public bool Write(IntPtr address, byte newValue, Patcher.WriteOptions options = WriteOptions.SaveOldValue) { byte[] newValueArray = new byte[1]; newValueArray[0] = newValue; return(this.Write(address, newValueArray, options)); }