CsWinRT
CsWinRT copied to clipboard
Add support for GeneratedComClassAttribute on WinRT classes
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
WindowsRuntimeTypeAttributeto be considered a WinRT type, rather than all internal interfaces (which is the current behavior)
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();
}
Added Jeremy for his thoughts on this.
"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.
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).