// 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); }