Skip to content

Invalid cast from 'WinRT.IInspectable' to 'IMemoryBufferByteAccess' (GeneratedComInterface) #1672

Closed

Description

@lostmsu

It's basically this issue, but it only happens when I replace [ComImport] with [GeneratedComInterface].

Describe the bug
Given

[GeneratedComInterface]
[Guid("5B0D3235-4DBA-4D44-865E-8F1D0E4FD04D")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public unsafe partial interface IMemoryBufferByteAccess
{
    void GetBuffer(out IntPtr buffer, out uint capacity);
}

Attempting to convert IMemoryBufferReference reference to IMemoryBufferByteAccess fails with

System.InvalidCastException: 'Invalid cast from 'WinRT.IInspectable' to 'ChatGPT.Platforms.Windows.Core.IMemoryBufferByteAccess'.'

To Reproduce
See the linked issue

  1. IMemoryBufferReference reference = ...
  2. reference.As<IMemoryBufferByteAccess>()

Expected behavior
Should get IMemoryBufferByteAccess

Version Info
.NET SDK 8.0 - net8.0-windows10.0.22621.0

Additional context

Activity

  1. dongle-the-gadget commented on Jul 14, 2024

    @dongle-the-gadget
    Contributor

    GeneratedComInterface isn’t allowed right now as the NuGet version does not contain the .NET 8 WinRT.Runtime that contains this functionality.

  2. Sergio0694 commented on Jul 15, 2024

    @Sergio0694
    Member

    Yeah this is caused by the fact we're not shipping the .NET 8 TFM yet, so this is expected. That said, it definitely shouldn't be needed to manually define IMemoryBufferByteAccess, do interop with that interface and use that directly just to get data out of an IMemoryBufferReference instance. I've opened #1673 to add a helper API to do this much more easily (and efficiently) 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions