CsWinRT icon indicating copy to clipboard operation
CsWinRT copied to clipboard

Add support for GeneratedComClassAttribute on WinRT classes

Open dongle-the-gadget opened this issue 1 year ago • 4 comments

Fixes #1722. Fixes #1851.

This PR includes two changes:

  • Modifying the default COM wrappers so that the vtables generated by the COM Wrappers generators are included.
  • Changing how WinRT interfaces are determined in the authoring generator. To be more specific, internal interfaces are now required to include WindowsRuntimeTypeAttribute to be considered a WinRT type, rather than all internal interfaces (which is the current behavior)

dongle-the-gadget avatar Oct 31 '24 16:10 dongle-the-gadget

Something like this?

[CustomMarshaller(typeof(Exception), MarshalMode.UnmanagedToManagedOut, typeof(ExceptionHelpersHResultMarshaller<>))]
public static class ExceptionHelpersHResultMarshaller<T>
    where T : unmanaged, INumber<T>
{
    public static T ConvertToUnmanaged(Exception e)
    {
        ExceptionHelpers.SetErrorInfo(e);

        return T.CreateTruncating(ExceptionHelpers.GetHRForException(e));
    }
}

And then you should be able to use it like this maybe? idk:

[Guid("6234C2F7-9917-469F-BDB4-3E8C630598AF")]
[GeneratedComInterface(Options = ComInterfaceOptions.ManagedObjectWrapper)]
public partial interface IFoo
{
    [return: MarshalUsing(typeof(ExceptionHelpersHResultMarshaller<int>))]
    void Bar();
}

Sergio0694 avatar Nov 02 '24 21:11 Sergio0694

Added Jeremy for his thoughts on this.

manodasanW avatar Nov 03 '24 19:11 manodasanW

"just to make sure there's a decent experience there"

What makes me a bit sad is that until we support this, eg. I won't be able to migrate interfaces like this to this 🥲

I'll go open an API proposal in the runtime repo for tracking.

Sergio0694 avatar Nov 04 '24 20:11 Sergio0694

Do we know whether these COM exceptions should be propagated through IErrorInfo or IRestrictedErrorInfo? If it's the former then it can be resolved through a .NET runtime update alone (as it is a COM interface, not WinRT).

dongle-the-gadget avatar Nov 05 '24 03:11 dongle-the-gadget