CsWinRT icon indicating copy to clipboard operation
CsWinRT copied to clipboard

Project IMemoryBufferByteAccess powers onto MemoryBuffer and Buffer

Open jonwis opened this issue 1 year ago • 0 comments

Proposal: Provide MemoryBuffer.GetAccess() that returns IMemoryBufferByteAccess

Summary

In https://github.com/microsoft/Windows-universal-samples/blob/main/Samples/AudioCreation/cs/CustomEffect/CustomEffect.cs we see a sample of extracting raw bytes from an audio sample, obtained from a MemoryBuffer. This pattern is confusing as it's not clearly documented that MemoryBuffer implements IMemoryBufferByteAccess, nor the implications of managing the lifecycle of the object.

I propose a model like "using", such as:

MemoryBuffer mb = /* ... */;
using (access = mb.GetByteAccess()) {
    unsafe {
        byte* data = access.Data;
        var len = access.Length;
    }
}

This follows the pattern in C++/WinRT that generates a .data() method onto MemoryBuffer and Buffer which under the covers acquires the byte-access interface and calls it.

Rationale

  • The link between MemoryBuffer and its non-WinRT interfaces is not documented
  • The lifecycle management of a Buffer and the underlying pointer is challenging, hence the 'disposable' type returned
  • Customer feedback indicates that using a Buffer to access this data is not obvious

jonwis avatar Jul 06 '22 17:07 jonwis