0

I'd like to get a pointer to the data behind a TensorFloat in my C# .NET Core 5.0 application. I started by declaring IMemoryBufferByteAccess:

[ComImport]
[Guid("5b0d3235-4dba-4d44-865e-8f1d0e4fd04d")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
unsafe interface IMemoryBufferByteAccess
{
    void GetBuffer(out byte* buffer, out uint capacity);
}

Then, after the inference, I try to use it as follows:

var outputTensor = (TensorFloat)session.Evaluate(binding, "0").Outputs["myAwesomeOutput:0"];
using var outputReference = outputTensor.CreateReference();
((IMemoryBufferByteAccess)outputReference).GetBuffer(out var onnxOutput, out var onnxCapacity);

This results in an System.InvalidCastException: 'Invalid cast from 'WinRT.IInspectable' to 'IMemoryBufferByteAccess'.', which is surprising, since the very same approach both for Windows.Media and Windows.Graphics. The documentation of Windows.Foundation.IMemoryBufferReference even states:

The same object identity must also implement the COM interface IMemoryBufferByteAccess. A client retrieves the IMemoryBufferByteAccess interface pointer via a QueryInterface from the IMemoryBufferReference object.

What am I missing?

Simon
  • 1,814
  • 20
  • 37

1 Answers1

1

I managed to get it to work. First I had to import an extension method:

using WinRT;

Then use the As(...) extension method to convert to IMemoryBufferByteAccess to retrieve the pointer:

outputReference.As<IMemoryBufferByteAccess>().GetBuffer(out var onnxOutput, out var onnxCapacity);
Simon
  • 1,814
  • 20
  • 37