/// <summary>
/// Copies the data of the specified managed <see cref="Object"/> into the unmanaged memory
/// block at the specified offset.</summary>
/// <param name="structure">
/// The <see cref="Object"/> containing the data to store at <paramref name="offset"/>.
/// </param>
/// <param name="offset">
/// The offset from the unmanaged memory handle, in bytes, at which copying begins.</param>
/// <param name="delete">
/// <c>true</c> to have the <see cref="Marshal.DestroyStructure"/> method called on the
/// unmanaged memory at <paramref name="offset"/> before copying begins. Note that passing
/// <c>false</c> can lead to a memory leak.</param>
/// <exception cref="ArgumentNullException">
/// <paramref name="structure"/> is a null reference.</exception>
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="offset"/> is less than zero.</exception>
/// <exception cref="PropertyValueException">
/// <see cref="SafeHandle.IsInvalid"/> is <c>true</c>.</exception>
/// <remarks><para>
/// <b>SetMemory</b> calls <see cref="Marshal.StructureToPtr"/> to copy the data of the
/// specified <paramref name="structure"/>, which must be an instance of a formatted class,
/// into the unmanaged memory block at the specified <paramref name="offset"/>.
/// </para><note type="caution">
/// The specified <paramref name="offset"/> is <b>not</b> checked against the (unknown) size
/// of the unmanaged memory block. Buffer overruns are possible!</note></remarks>
public unsafe void SetMemory(object structure, int offset, bool delete)
{
if (IsInvalid)
{
ThrowHelper.ThrowPropertyValueException("IsInvalid", Strings.PropertyTrue);
}
if (structure == null)
{
ThrowHelper.ThrowArgumentNullException("structure");
}
if (offset < 0)
{
ThrowHelper.ThrowArgumentOutOfRangeException(
"offset", offset, Strings.ArgumentNegative);
}
Marshal.StructureToPtr(structure, (IntPtr)((byte *)handle + offset), delete);
}