/// <summary>Initializes a new instance of the <see cref="SafeByteArray"/> class by copying the bytes from another unmanaged array.</summary> /// <param name="src">Another unmanaged array.</param> /// <exception cref="System.ArgumentNullException">src</exception> /// <exception cref="System.ArgumentException">Invalid source object. - src</exception> public SafeByteArray(SafeByteArray src) : base(src?.Count ?? 0) { if (src == null) { throw new ArgumentNullException(nameof(src)); } if (src.IsInvalid) { throw new ArgumentException(@"Invalid source object.", nameof(src)); } CopyMemory(src.handle, handle, src.Count); }
// write to a UTF8 stream public static HRESULT IStream_ReadToBuffer(IStream pstm, uint uMaxSize, out byte[] ppBytes) { ppBytes = null; var hr = IStream_Size(pstm, out var uli); if (hr.Succeeded) { hr = (uli < uMaxSize) ? HRESULT.S_OK : HRESULT.E_FAIL; if (hr.Succeeded) { var uliLowPart = (uint)(uli & uint.MaxValue); using (var pdata = new SafeByteArray((int)uliLowPart)) { hr = IStream_Read(pstm, (IntPtr)pdata, uliLowPart); if (hr.Succeeded) { ppBytes = pdata.ToArray(); } } } } return(hr); }