diff --git a/Projections/Test/TestHost.ProbeByHost.cs b/Projections/Test/TestHost.ProbeByHost.cs index 67c27dfe9..7149f675d 100644 --- a/Projections/Test/TestHost.ProbeByHost.cs +++ b/Projections/Test/TestHost.ProbeByHost.cs @@ -54,7 +54,7 @@ private static unsafe int Do_Abi_ActivateInstance_0(IntPtr thisPtr, out IntPtr i try { __instance = global::WinRT.ComWrappersSupport.FindObject(thisPtr).ActivateInstance(); - instance = MarshalInspectable.FromManaged(__instance); + instance = MarshalInspectable.FromManaged(__instance); } catch (Exception __exception__) { @@ -84,11 +84,11 @@ public unsafe object ActivateInstance() try { global::WinRT.ExceptionHelpers.ThrowExceptionForHR(_obj.Vftbl.ActivateInstance_0(ThisPtr, out __retval)); - return MarshalInspectable.FromAbi(__retval); + return MarshalInspectable.FromAbi(__retval); } finally { - MarshalInspectable.DisposeAbi(__retval); + MarshalInspectable.DisposeAbi(__retval); } } } @@ -123,7 +123,7 @@ public static unsafe IntPtr GetActivationFactory(String runtimeClassId) if (ctor != null) { var factory = new WinRT.Host.ActivationFactory(ctor); - return MarshalInspectable.FromManaged(factory); + return MarshalInspectable.FromManaged(factory); } } } diff --git a/Projections/TestHost.ProbeByClass/TestHost.ProbeByClass.cs b/Projections/TestHost.ProbeByClass/TestHost.ProbeByClass.cs index c198ba995..93368151b 100644 --- a/Projections/TestHost.ProbeByClass/TestHost.ProbeByClass.cs +++ b/Projections/TestHost.ProbeByClass/TestHost.ProbeByClass.cs @@ -54,7 +54,7 @@ private static unsafe int Do_Abi_ActivateInstance_0(IntPtr thisPtr, out IntPtr i try { __instance = global::WinRT.ComWrappersSupport.FindObject(thisPtr).ActivateInstance(); - instance = MarshalInspectable.FromManaged(__instance); + instance = MarshalInspectable.FromManaged(__instance); } catch (Exception __exception__) { @@ -84,11 +84,11 @@ public unsafe object ActivateInstance() try { global::WinRT.ExceptionHelpers.ThrowExceptionForHR(_obj.Vftbl.ActivateInstance_0(ThisPtr, out __retval)); - return MarshalInspectable.FromAbi(__retval); + return MarshalInspectable.FromAbi(__retval); } finally { - MarshalInspectable.DisposeAbi(__retval); + MarshalInspectable.DisposeAbi(__retval); } } } @@ -123,7 +123,7 @@ public static unsafe IntPtr GetActivationFactory(String runtimeClassId) if (ctor != null) { var factory = new WinRT.Host.ActivationFactory(ctor); - return MarshalInspectable.FromManaged(factory); + return MarshalInspectable.FromManaged(factory); } } } diff --git a/UnitTest/TestComponentCSharp_Tests.cs b/UnitTest/TestComponentCSharp_Tests.cs index 45692199f..c18d7e537 100644 --- a/UnitTest/TestComponentCSharp_Tests.cs +++ b/UnitTest/TestComponentCSharp_Tests.cs @@ -22,8 +22,6 @@ using WinRT.Interop; using System.Runtime.InteropServices; using System.Runtime.InteropServices.WindowsRuntime; -using Windows.Security.Cryptography; -using Windows.Security.Cryptography.Core; namespace UnitTest { @@ -89,13 +87,9 @@ public void TestUri() (object sender, Uri value) => Assert.Equal(managedUri, value); TestObject.RaiseUriChanged(); - var uri2 = MarshalInspectable.FromAbi(ABI.System.Uri.FromManaged(managedUri)); + var uri2 = MarshalInspectable.FromAbi(ABI.System.Uri.FromManaged(managedUri)); var str2 = uri2.ToString(); Assert.Equal(full_uri, str2); - - var uri3 = MarshalInspectable.FromAbi(ABI.System.Uri.FromManaged(managedUri)); - var str3 = uri3.ToString(); - Assert.Equal(full_uri, str3); } [Fact] @@ -1621,13 +1615,9 @@ public void WeakReferenceOfNativeObjectRehydratedAfterWrapperIsCollected() [Fact] public void TestUnwrapInspectable() { - using var objRef = MarshalInspectable.CreateMarshaler(TestObject); + using var objRef = MarshalInspectable.CreateMarshaler(TestObject); var inspectable = IInspectable.FromAbi(objRef.ThisPtr); Assert.True(ComWrappersSupport.TryUnwrapObject(inspectable, out _)); - - using var objRef2 = MarshalInspectable.CreateMarshaler(TestObject); - var inspectable2 = IInspectable.FromAbi(objRef2.ThisPtr); - Assert.True(ComWrappersSupport.TryUnwrapObject(inspectable2, out _)); } [Fact] @@ -1783,16 +1773,5 @@ public void TestInterfaceObjectMarshalling() Assert.Equal(TestObject.ReadWriteProperty, nativeProperties.ReadWriteProperty); } - - // Test scenario where type reported by runtimeclass name is not a valid type (i.e. internal type). - [Fact] - public void TestNonProjectedRuntimeClass() - { - string key = "....."; - IBuffer keyMaterial = CryptographicBuffer.ConvertStringToBinary(key, BinaryStringEncoding.Utf8); - MacAlgorithmProvider mac = MacAlgorithmProvider.OpenAlgorithm(MacAlgorithmNames.HmacSha1); - CryptographicKey cryptoKey = mac.CreateKey(keyMaterial); - Assert.NotNull(cryptoKey); - } } } \ No newline at end of file diff --git a/WinRT.Runtime/AgileReference.cs b/WinRT.Runtime/AgileReference.cs index 3ef555d9a..14903d310 100644 --- a/WinRT.Runtime/AgileReference.cs +++ b/WinRT.Runtime/AgileReference.cs @@ -103,7 +103,7 @@ public unsafe AgileReference(IObjectReference instance) public new T Get() { using var objRef = base.Get(); - return ComWrappersSupport.CreateRcwForComObject(objRef?.ThisPtr ?? IntPtr.Zero); + return (T) ComWrappersSupport.CreateRcwForComObject(objRef?.ThisPtr ?? IntPtr.Zero); } } } \ No newline at end of file diff --git a/WinRT.Runtime/CastExtensions.cs b/WinRT.Runtime/CastExtensions.cs index cee9e89ea..eb918b41e 100644 --- a/WinRT.Runtime/CastExtensions.cs +++ b/WinRT.Runtime/CastExtensions.cs @@ -30,7 +30,7 @@ public static TInterface As(this object value) { using (objRef) { - return ComWrappersSupport.CreateRcwForComObject(objRef.ThisPtr); + return (TInterface)ComWrappersSupport.CreateRcwForComObject(objRef.ThisPtr); } } } diff --git a/WinRT.Runtime/ComWrappersSupport.cs b/WinRT.Runtime/ComWrappersSupport.cs index c3b9fdcc8..ff3ee2c70 100644 --- a/WinRT.Runtime/ComWrappersSupport.cs +++ b/WinRT.Runtime/ComWrappersSupport.cs @@ -28,7 +28,6 @@ public static partial class ComWrappersSupport { private readonly static ConcurrentDictionary> TypedObjectFactoryCache = new ConcurrentDictionary>(); private readonly static ConditionalWeakTable CCWTable = new ConditionalWeakTable(); - private readonly static ConcurrentDictionary TypeObjectRefFieldCache = new ConcurrentDictionary(); public static TReturn MarshalDelegateInvoke(IntPtr thisPtr, Func invoke) where TDelegate : class, Delegate @@ -69,33 +68,20 @@ public static bool TryUnwrapObject(object o, out IObjectReference objRef) } Type type = o.GetType(); - - var objRefField = TypeObjectRefFieldCache.GetOrAdd(type, (type) => - { - ObjectReferenceWrapperAttribute objRefWrapper = type.GetCustomAttribute(); - if (objRefWrapper is object) - { - return type.GetField(objRefWrapper.ObjectReferenceField, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly); - } - - ProjectedRuntimeClassAttribute projectedClass = type.GetCustomAttribute(); - if (projectedClass is object && projectedClass.DefaultInterfaceProperty != null) - { - return type.GetProperty(projectedClass.DefaultInterfaceProperty, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly); - } - - return null; - }); - - - if(objRefField is FieldInfo field) + ObjectReferenceWrapperAttribute objRefWrapper = type.GetCustomAttribute(); + if (objRefWrapper is object) { - objRef = (IObjectReference)field.GetValue(o); + objRef = (IObjectReference)type.GetField(objRefWrapper.ObjectReferenceField, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly).GetValue(o); return true; } - else if(objRefField is PropertyInfo defaultProperty) + + ProjectedRuntimeClassAttribute projectedClass = type.GetCustomAttribute(); + + if (projectedClass is object && projectedClass.DefaultInterfaceProperty != null) { - return TryUnwrapObject(defaultProperty.GetValue(o), out objRef); + return TryUnwrapObject( + type.GetProperty(projectedClass.DefaultInterfaceProperty, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly).GetValue(o), + out objRef); } objRef = null; @@ -342,44 +328,6 @@ internal static Func CreateTypedRcwFactory(string runtimeC return CreateFactoryForImplementationType(runtimeClassName, implementationType); } - internal static string GetRuntimeClassForTypeCreation(IInspectable inspectable, Type staticallyDeterminedType) - { - string runtimeClassName = inspectable.GetRuntimeClassName(noThrow: true); - if (staticallyDeterminedType != null && staticallyDeterminedType != typeof(object)) - { - // We have a static type which we can use to construct the object. But, we can't just use it for all scenarios - // and primarily use it for tear off scenarios and for scenarios where runtimeclass isn't accurate. - // For instance if the static type is an interface, we return an IInspectable to represent the interface. - // But it isn't convertable back to the class via the as operator which would be possible if we use runtimeclass. - // Similarly for composable types, they can be statically retrieved using the parent class, but can then no longer - // be cast to the sub class via as operator even if it is really an instance of it per rutimeclass. - // To handle these scenarios, we use the runtimeclass if we find it is assignable to the statically determined type. - // If it isn't, we use the statically determined type as it is a tear off. - - Type implementationType = null; - if (runtimeClassName != null) - { - try - { - (implementationType, _) = TypeNameSupport.FindTypeByName(runtimeClassName.AsSpan()); - } - catch (TypeLoadException) - { - } - } - - if (!(implementationType != null && - (staticallyDeterminedType == implementationType || - staticallyDeterminedType.IsAssignableFrom(implementationType) || - staticallyDeterminedType.IsGenericType && implementationType.GetInterfaces().Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == staticallyDeterminedType.GetGenericTypeDefinition())))) - { - runtimeClassName = TypeNameSupport.GetNameForType(staticallyDeterminedType, TypeNameGenerationFlags.GenerateBoxedName); - } - } - - return runtimeClassName; - } - private static bool ShouldProvideIReference(object obj) { return obj.GetType().IsValueType || obj is string || obj is Type || obj is Delegate; diff --git a/WinRT.Runtime/ComWrappersSupport.net5.cs b/WinRT.Runtime/ComWrappersSupport.net5.cs index bc74b71e2..a250ba418 100644 --- a/WinRT.Runtime/ComWrappersSupport.net5.cs +++ b/WinRT.Runtime/ComWrappersSupport.net5.cs @@ -6,7 +6,6 @@ using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Text; -using System.Threading; using WinRT.Interop; using static System.Runtime.InteropServices.ComWrappers; @@ -14,8 +13,7 @@ namespace WinRT { public static partial class ComWrappersSupport { - internal static readonly ConditionalWeakTable InspectableInfoTable = new ConditionalWeakTable(); - internal static readonly ThreadLocal CreateRCWType = new ThreadLocal(); + internal static readonly ConditionalWeakTable InspectableInfoTable = new ConditionalWeakTable(); private static ComWrappers _comWrappers; private static object _comWrappersLock = new object(); @@ -52,33 +50,26 @@ internal static unsafe InspectableInfo GetInspectableInfo(IntPtr pThis) return InspectableInfoTable.GetValue(_this, o => PregenerateNativeTypeInformation(o).inspectableInfo); } - public static T CreateRcwForComObject(IntPtr ptr) + public static object CreateRcwForComObject(IntPtr ptr) { if (ptr == IntPtr.Zero) { - return default; + return null; } - // CreateRCWType is a thread local which is set here to communicate the statically known type - // when we are called by the ComWrappers API to create the object. We can't pass this through the - // ComWrappers API surface, so we are achieving it via a thread local. We unset it after in case - // there is other calls to it via other means. - CreateRCWType.Value = typeof(T); var rcw = ComWrappers.GetOrCreateObjectForComInstance(ptr, CreateObjectFlags.TrackerObject); - CreateRCWType.Value = null; - // Because .NET will de-duplicate strings and WinRT doesn't, - // our RCW factory returns a wrapper of our string instance. - // This ensures that ComWrappers never sees the same managed object for two different - // native pointers. We unwrap here to ensure that the user-experience is expected - // and consumers get a string object for a Windows.Foundation.IReference. - // We need to do the same thing for System.Type because there can be multiple WUX.Interop.TypeName's - // for a single System.Type. - + // Because .NET will de-duplicate strings and WinRT doesn't, + // our RCW factory returns a wrapper of our string instance. + // This ensures that ComWrappers never sees the same managed object for two different + // native pointers. We unwrap here to ensure that the user-experience is expected + // and consumers get a string object for a Windows.Foundation.IReference. + // We need to do the same thing for System.Type because there can be multiple WUX.Interop.TypeName's + // for a single System.Type. return rcw switch { - ABI.System.Nullable ns => (T)(object) ns.Value, - ABI.System.Nullable nt => (T)(object) nt.Value, - _ => (T) rcw + ABI.System.Nullable ns => ns.Value, + ABI.System.Nullable nt => nt.Value, + _ => rcw }; } @@ -220,13 +211,13 @@ protected override object CreateObject(IntPtr externalComObject, CreateObjectFla { IInspectable inspectable = new IInspectable(inspectableRef); - string runtimeClassName = ComWrappersSupport.GetRuntimeClassForTypeCreation(inspectable, ComWrappersSupport.CreateRCWType.Value); - if (runtimeClassName == null) - { - // If the external IInspectable has not implemented GetRuntimeClassName, - // we use the Inspectable wrapper directly. - return inspectable; - } + string runtimeClassName = inspectable.GetRuntimeClassName(noThrow: true); + if (runtimeClassName == null) + { + // If the external IInspectable has not implemented GetRuntimeClassName, + // we use the Inspectable wrapper directly. + return inspectable; + } return ComWrappersSupport.GetTypedRcwFactory(runtimeClassName)(inspectable); } else if (objRef.TryAs(out var weakRef) == 0) diff --git a/WinRT.Runtime/ComWrappersSupport.netstandard2.0.cs b/WinRT.Runtime/ComWrappersSupport.netstandard2.0.cs index 8e6d8e550..36b3e1161 100644 --- a/WinRT.Runtime/ComWrappersSupport.netstandard2.0.cs +++ b/WinRT.Runtime/ComWrappersSupport.netstandard2.0.cs @@ -18,11 +18,11 @@ public static partial class ComWrappersSupport internal static InspectableInfo GetInspectableInfo(IntPtr pThis) => UnmanagedObject.FindObject(pThis).InspectableInfo; - public static T CreateRcwForComObject(IntPtr ptr) + public static object CreateRcwForComObject(IntPtr ptr) { if (ptr == IntPtr.Zero) { - return default; + return null; } IObjectReference identity = GetObjectReferenceForInterface(ptr).As(); @@ -34,8 +34,8 @@ public static T CreateRcwForComObject(IntPtr ptr) object runtimeWrapper = null; if (identity.TryAs(out var inspectableRef) == 0) { - var inspectable = new IInspectable(identity); - string runtimeClassName = GetRuntimeClassForTypeCreation(inspectable, typeof(T)); + var inspectable = new IInspectable(identity); + string runtimeClassName = inspectable.GetRuntimeClassName(); runtimeWrapper = TypedObjectFactoryCache.GetOrAdd(runtimeClassName, className => CreateTypedRcwFactory(className))(inspectable); } else if (identity.TryAs(out var weakRef) == 0) @@ -60,21 +60,21 @@ public static T CreateRcwForComObject(IntPtr ptr) return oldValue; }).TryGetTarget(out object rcw); - GC.KeepAlive(keepAliveSentinel); - - // Because .NET will de-duplicate strings and WinRT doesn't, - // our RCW factory returns a wrapper of our string instance. - // This ensures that our cache never sees the same managed object for two different - // native pointers. We unwrap here to ensure that the user-experience is expected - // and consumers get a string object for a Windows.Foundation.IReference. - // We need to do the same thing for System.Type because there can be multiple MUX.Interop.TypeName's - // for a single System.Type. + GC.KeepAlive(keepAliveSentinel); + + // Because .NET will de-duplicate strings and WinRT doesn't, + // our RCW factory returns a wrapper of our string instance. + // This ensures that our cache never sees the same managed object for two different + // native pointers. We unwrap here to ensure that the user-experience is expected + // and consumers get a string object for a Windows.Foundation.IReference. + // We need to do the same thing for System.Type because there can be multiple MUX.Interop.TypeName's + // for a single System.Type. return rcw switch { - ABI.System.Nullable ns => (T)(object)ns.Value, - ABI.System.Nullable nt => (T)(object)nt.Value, - _ => (T)rcw - }; + ABI.System.Nullable ns => ns.Value, + ABI.System.Nullable nt => nt.Value, + _ => rcw + }; } public static void RegisterObjectForInterface(object obj, IntPtr thisPtr) diff --git a/WinRT.Runtime/Interop/IAgileReference.net5.cs b/WinRT.Runtime/Interop/IAgileReference.net5.cs index 7d44e59b0..df1639e13 100644 --- a/WinRT.Runtime/Interop/IAgileReference.net5.cs +++ b/WinRT.Runtime/Interop/IAgileReference.net5.cs @@ -103,7 +103,7 @@ private static int Do_Abi_Resolve(IntPtr thisPtr, Guid* riid, IntPtr* objectRefe } finally { - MarshalInspectable.DisposeAbi(ptr); + MarshalInspectable.DisposeAbi(ptr); } } } @@ -187,7 +187,7 @@ public IObjectReference GetInterfaceFromGlobal(IntPtr cookie, Guid riid) } finally { - MarshalInspectable.DisposeAbi(ptr); + MarshalInspectable.DisposeAbi(ptr); } } } diff --git a/WinRT.Runtime/Interop/IAgileReference.netstandard2.0.cs b/WinRT.Runtime/Interop/IAgileReference.netstandard2.0.cs index e7fd6c92d..42266c851 100644 --- a/WinRT.Runtime/Interop/IAgileReference.netstandard2.0.cs +++ b/WinRT.Runtime/Interop/IAgileReference.netstandard2.0.cs @@ -106,7 +106,7 @@ public IObjectReference Resolve(Guid riid) } finally { - MarshalInspectable.DisposeAbi(ptr); + MarshalInspectable.DisposeAbi(ptr); } } } @@ -201,7 +201,7 @@ public IObjectReference GetInterfaceFromGlobal(IntPtr cookie, Guid riid) } finally { - MarshalInspectable.DisposeAbi(ptr); + MarshalInspectable.DisposeAbi(ptr); } } } diff --git a/WinRT.Runtime/Interop/IWeakReferenceSource.net5.cs b/WinRT.Runtime/Interop/IWeakReferenceSource.net5.cs index a64a65bf0..c5114ef8a 100644 --- a/WinRT.Runtime/Interop/IWeakReferenceSource.net5.cs +++ b/WinRT.Runtime/Interop/IWeakReferenceSource.net5.cs @@ -109,7 +109,7 @@ private static int Do_Abi_GetWeakReference(IntPtr thisPtr, IntPtr* weakReference } finally { - MarshalInspectable.DisposeAbi(objRef); + MarshalInspectable.DisposeAbi(objRef); } } } diff --git a/WinRT.Runtime/Interop/IWeakReferenceSource.netstandard2.0.cs b/WinRT.Runtime/Interop/IWeakReferenceSource.netstandard2.0.cs index bc1d44e37..a45da0d60 100644 --- a/WinRT.Runtime/Interop/IWeakReferenceSource.netstandard2.0.cs +++ b/WinRT.Runtime/Interop/IWeakReferenceSource.netstandard2.0.cs @@ -123,7 +123,7 @@ public IWeakReferenceSource(ObjectReference obj) } finally { - MarshalInspectable.DisposeAbi(objRef); + MarshalInspectable.DisposeAbi(objRef); } } } diff --git a/WinRT.Runtime/Marshalers.cs b/WinRT.Runtime/Marshalers.cs index b74c4e893..7598ecc9b 100644 --- a/WinRT.Runtime/Marshalers.cs +++ b/WinRT.Runtime/Marshalers.cs @@ -810,7 +810,23 @@ public static T FromAbi(IntPtr ptr) return (T)(object)null; } - return MarshalInspectable.FromAbi(ptr); + object primaryManagedWrapper = MarshalInspectable.FromAbi(ptr); + + if (primaryManagedWrapper is T obj) + { + return obj; + } +#if !NETSTANDARD2_0 + return (T)(object)IInspectable.FromAbi(ptr); +#else + // If the metadata type doesn't implement the interface, then create a tear-off RCW. + // TODO: Uniqueness of tear-offs? + if (_FromAbi == null) + { + _FromAbi = BindFromAbi(); + } + return _FromAbi(ptr); +#endif } public static IObjectReference CreateMarshaler(T value) @@ -840,7 +856,7 @@ public static IObjectReference CreateMarshaler(T value) _As = BindAs(); } - var inspectable = MarshalInspectable.CreateMarshaler(value, true); + var inspectable = MarshalInspectable.CreateMarshaler(value, true); return _As(inspectable); } @@ -914,9 +930,9 @@ private static Func BindAs() } } - static public class MarshalInspectable + static public class MarshalInspectable { - public static IObjectReference CreateMarshaler(T o, bool unwrapObject = true) + public static IObjectReference CreateMarshaler(object o, bool unwrapObject = true) { if (o is null) { @@ -928,69 +944,69 @@ public static IObjectReference CreateMarshaler(T o, bool unwrapObject = true) return objRef.As(); } using (var ccw = ComWrappersSupport.CreateCCWForObject(ComWrappersSupport.GetRuntimeClassCCWTypeIfAny(o))) - { + { return ccw.As(); } } - public static IntPtr GetAbi(IObjectReference objRef) => - objRef is null ? IntPtr.Zero : MarshalInterfaceHelper.GetAbi(objRef); + public static IntPtr GetAbi(IObjectReference objRef) => + objRef is null ? IntPtr.Zero : MarshalInterfaceHelper.GetAbi(objRef); - public static T FromAbi(IntPtr ptr) + public static object FromAbi(IntPtr ptr) { if (ptr == IntPtr.Zero) { - return default; + return (object)null; } using var objRef = ObjectReference.FromAbi(ptr); using var unknownObjRef = objRef.As(); if (unknownObjRef.IsReferenceToManagedObject) { - return (T) ComWrappersSupport.FindObject(unknownObjRef.ThisPtr); + return ComWrappersSupport.FindObject(unknownObjRef.ThisPtr); } - else if (Projections.TryGetMarshalerTypeForProjectedRuntimeClass(objRef, out Type type)) + else if (Projections.TryGetMarshalerTypeForProjectedRuntimeClass(objRef, out Type type)) { var fromAbiMethod = type.GetMethod("FromAbi", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static); if (fromAbiMethod is null) { throw new MissingMethodException(); } - return (T) fromAbiMethod.Invoke(null, new object[] { ptr }); + return fromAbiMethod.Invoke(null, new object[] { ptr }); } else { - return ComWrappersSupport.CreateRcwForComObject(ptr); + return ComWrappersSupport.CreateRcwForComObject(ptr); } } - public static void DisposeMarshaler(IObjectReference objRef) => MarshalInterfaceHelper.DisposeMarshaler(objRef); + public static void DisposeMarshaler(IObjectReference objRef) => MarshalInterfaceHelper.DisposeMarshaler(objRef); - public static void DisposeAbi(IntPtr ptr) => MarshalInterfaceHelper.DisposeAbi(ptr); - public static IntPtr FromManaged(T o, bool unwrapObject = true) + public static void DisposeAbi(IntPtr ptr) => MarshalInterfaceHelper.DisposeAbi(ptr); + public static IntPtr FromManaged(object o, bool unwrapObject = true) { var objRef = CreateMarshaler(o, unwrapObject); return objRef?.GetRef() ?? IntPtr.Zero; } - public static unsafe void CopyManaged(T o, IntPtr dest, bool unwrapObject = true) + public static unsafe void CopyManaged(object o, IntPtr dest, bool unwrapObject = true) { var objRef = CreateMarshaler(o, unwrapObject); *(IntPtr*)dest.ToPointer() = objRef?.GetRef() ?? IntPtr.Zero; } - public static unsafe MarshalInterfaceHelper.MarshalerArray CreateMarshalerArray(T[] array) => MarshalInterfaceHelper.CreateMarshalerArray(array, (o) => CreateMarshaler(o)); + public static unsafe MarshalInterfaceHelper.MarshalerArray CreateMarshalerArray(object[] array) => MarshalInterfaceHelper.CreateMarshalerArray(array, (o) => CreateMarshaler(o)); - public static (int length, IntPtr data) GetAbiArray(T box) => MarshalInterfaceHelper.GetAbiArray(box); + public static (int length, IntPtr data) GetAbiArray(object box) => MarshalInterfaceHelper.GetAbiArray(box); - public static unsafe T[] FromAbiArray(T box) => MarshalInterfaceHelper.FromAbiArray(box, FromAbi); + public static unsafe object[] FromAbiArray(object box) => MarshalInterfaceHelper.FromAbiArray(box, FromAbi); - public static unsafe (int length, IntPtr data) FromManagedArray(T[] array) => MarshalInterfaceHelper.FromManagedArray(array, (o) => FromManaged(o)); + public static unsafe (int length, IntPtr data) FromManagedArray(object[] array) => MarshalInterfaceHelper.FromManagedArray(array, (o) => FromManaged(o)); - public static unsafe void CopyManagedArray(T[] array, IntPtr data) => MarshalInterfaceHelper.CopyManagedArray(array, data, (o, dest) => CopyManaged(o, dest)); + public static unsafe void CopyManagedArray(object[] array, IntPtr data) => MarshalInterfaceHelper.CopyManagedArray(array, data, (o, dest) => CopyManaged(o, dest)); - public static void DisposeMarshalerArray(T box) => MarshalInterfaceHelper.DisposeMarshalerArray(box); + public static void DisposeMarshalerArray(object box) => MarshalInterfaceHelper.DisposeMarshalerArray(box); - public static unsafe void DisposeAbiArray(T box) => MarshalInterfaceHelper.DisposeAbiArray(box); + public static unsafe void DisposeAbiArray(object box) => MarshalInterfaceHelper.DisposeAbiArray(box); } public class Marshaler @@ -1087,7 +1103,7 @@ static Marshaler() AbiType = typeof(IntPtr); CreateMarshaler = (T value) => MarshalInterface.CreateMarshaler(value); GetAbi = (object objRef) => MarshalInterface.GetAbi((IObjectReference)objRef); - FromAbi = (object value) => MarshalInterface.FromAbi((IntPtr)value); + FromAbi = (object value) => (T)(object)MarshalInterface.FromAbi((IntPtr)value); FromManaged = (T value) => ((IObjectReference)CreateMarshaler(value)).GetRef(); DisposeMarshaler = (object objRef) => MarshalInterface.DisposeMarshaler((IObjectReference)objRef); DisposeAbi = (object box) => MarshalInterface.DisposeAbi((IntPtr)box); @@ -1095,13 +1111,13 @@ static Marshaler() else if (typeof(T) == typeof(object)) { AbiType = typeof(IntPtr); - CreateMarshaler = (T value) => MarshalInspectable.CreateMarshaler(value); - GetAbi = (object objRef) => MarshalInspectable.GetAbi((IObjectReference)objRef); - FromAbi = (object box) => MarshalInspectable.FromAbi((IntPtr)box); - FromManaged = (T value) => MarshalInspectable.FromManaged(value); - CopyManaged = (T value, IntPtr dest) => MarshalInspectable.CopyManaged(value, dest); - DisposeMarshaler = (object objRef) => MarshalInspectable.DisposeMarshaler((IObjectReference)objRef); - DisposeAbi = (object box) => MarshalInspectable.DisposeAbi((IntPtr)box); + CreateMarshaler = (T value) => MarshalInspectable.CreateMarshaler(value); + GetAbi = (object objRef) => MarshalInspectable.GetAbi((IObjectReference)objRef); + FromAbi = (object box) => (T)MarshalInspectable.FromAbi((IntPtr)box); + FromManaged = (T value) => MarshalInspectable.FromManaged(value); + CopyManaged = (T value, IntPtr dest) => MarshalInspectable.CopyManaged(value, dest); + DisposeMarshaler = (object objRef) => MarshalInspectable.DisposeMarshaler((IObjectReference)objRef); + DisposeAbi = (object box) => MarshalInspectable.DisposeAbi((IntPtr)box); } else // delegate, class { diff --git a/WinRT.Runtime/Projections.cs b/WinRT.Runtime/Projections.cs index 9f0293340..83c2200cd 100644 --- a/WinRT.Runtime/Projections.cs +++ b/WinRT.Runtime/Projections.cs @@ -19,7 +19,6 @@ public static class Projections private static readonly Dictionary CustomAbiTypeNameToTypeMappings = new Dictionary(); private static readonly Dictionary CustomTypeToAbiTypeNameMappings = new Dictionary(); private static readonly HashSet ProjectedRuntimeClassNames = new HashSet(); - private static readonly HashSet ProjectedCustomTypeRuntimeClasses = new HashSet(); static Projections() { @@ -95,20 +94,14 @@ private static void RegisterCustomAbiTypeMappingNoLock(Type publicType, Type abi if (isRuntimeClass) { ProjectedRuntimeClassNames.Add(winrtTypeName); - ProjectedCustomTypeRuntimeClasses.Add(publicType); } } - public static Type FindCustomHelperTypeMapping(Type publicType, bool filterToRuntimeClass = false) + public static Type FindCustomHelperTypeMapping(Type publicType) { rwlock.EnterReadLock(); try { - if(filterToRuntimeClass && !ProjectedCustomTypeRuntimeClasses.Contains(publicType)) - { - return null; - } - if (publicType.IsGenericType) { return CustomTypeToHelperTypeMappings.TryGetValue(publicType.GetGenericTypeDefinition(), out Type abiTypeDefinition) @@ -275,38 +268,29 @@ internal static Type GetDefaultInterfaceTypeForRuntimeClassType(Type runtimeClas return defaultInterface; } - internal static bool TryGetMarshalerTypeForProjectedRuntimeClass(IObjectReference objectReference, out Type type) + internal static bool TryGetMarshalerTypeForProjectedRuntimeClass(IObjectReference objectReference, out Type type) { - Type projectedType = typeof(T); - if (projectedType == typeof(object)) + if(objectReference.TryAs(out var inspectablePtr) == 0) { - if (objectReference.TryAs(out var inspectablePtr) == 0) + rwlock.EnterReadLock(); + try { - rwlock.EnterReadLock(); - try + IInspectable inspectable = inspectablePtr; + string runtimeClassName = inspectable.GetRuntimeClassName(true); + if (runtimeClassName is object) { - IInspectable inspectable = inspectablePtr; - string runtimeClassName = inspectable.GetRuntimeClassName(true); - if (runtimeClassName is object) + if (ProjectedRuntimeClassNames.Contains(runtimeClassName)) { - if (ProjectedRuntimeClassNames.Contains(runtimeClassName)) - { - type = CustomTypeToHelperTypeMappings[CustomAbiTypeNameToTypeMappings[runtimeClassName]]; - return true; - } + type = CustomTypeToHelperTypeMappings[CustomAbiTypeNameToTypeMappings[runtimeClassName]]; + return true; } } - finally - { - inspectablePtr.Dispose(); - rwlock.ExitReadLock(); - } } - } - else - { - type = FindCustomHelperTypeMapping(projectedType, true); - return type != null; + finally + { + inspectablePtr.Dispose(); + rwlock.ExitReadLock(); + } } type = null; return false; diff --git a/WinRT.Runtime/Projections/Bindable.net5.cs b/WinRT.Runtime/Projections/Bindable.net5.cs index d9b0181f7..1f190ec3b 100644 --- a/WinRT.Runtime/Projections/Bindable.net5.cs +++ b/WinRT.Runtime/Projections/Bindable.net5.cs @@ -127,7 +127,7 @@ private static unsafe int Do_Abi_get_Current_0(IntPtr thisPtr, IntPtr* value) try { __value = global::WinRT.ComWrappersSupport.FindObject(thisPtr).Current; - *value = MarshalInspectable.FromManaged(__value); + *value = MarshalInspectable.FromManaged(__value); } catch (Exception __exception__) { @@ -177,11 +177,11 @@ private static unsafe int Do_Abi_get_HasCurrent_1(IntPtr thisPtr, byte* value) try { global::WinRT.ExceptionHelpers.ThrowExceptionForHR(_obj.Vftbl.get_Current_0(ThisPtr, &__retval)); - return MarshalInspectable.FromAbi(__retval); + return MarshalInspectable.FromAbi(__retval); } finally { - MarshalInspectable.DisposeAbi(__retval); + MarshalInspectable.DisposeAbi(__retval); } } } @@ -251,7 +251,7 @@ private static unsafe int Do_Abi_GetAt_0(IntPtr thisPtr, uint index, IntPtr* res try { __result = global::WinRT.ComWrappersSupport.FindObject(thisPtr).GetAt(index); - *result = MarshalInspectable.FromManaged(__result); + *result = MarshalInspectable.FromManaged(__result); } catch (Exception __exception__) @@ -274,7 +274,7 @@ private static unsafe int Do_Abi_IndexOf_2(IntPtr thisPtr, IntPtr value, uint* i try { - __returnValue = global::WinRT.ComWrappersSupport.FindObject(thisPtr).IndexOf(MarshalInspectable.FromAbi(value), out __index); + __returnValue = global::WinRT.ComWrappersSupport.FindObject(thisPtr).IndexOf(MarshalInspectable.FromAbi(value), out __index); *index = __index; *returnValue = (byte)(__returnValue ? 1 : 0); @@ -322,11 +322,11 @@ private static unsafe int Do_Abi_get_Size_1(IntPtr thisPtr, uint* value) try { global::WinRT.ExceptionHelpers.ThrowExceptionForHR(_obj.Vftbl.GetAt_0(ThisPtr, index, &__retval)); - return MarshalInspectable.FromAbi(__retval); + return MarshalInspectable.FromAbi(__retval); } finally { - MarshalInspectable.DisposeAbi(__retval); + MarshalInspectable.DisposeAbi(__retval); } } @@ -339,14 +339,14 @@ private static unsafe int Do_Abi_get_Size_1(IntPtr thisPtr, uint* value) byte __retval = default; try { - __value = MarshalInspectable.CreateMarshaler(value); - global::WinRT.ExceptionHelpers.ThrowExceptionForHR(_obj.Vftbl.IndexOf_2(ThisPtr, MarshalInspectable.GetAbi(__value), &__index, &__retval)); + __value = MarshalInspectable.CreateMarshaler(value); + global::WinRT.ExceptionHelpers.ThrowExceptionForHR(_obj.Vftbl.IndexOf_2(ThisPtr, MarshalInspectable.GetAbi(__value), &__index, &__retval)); index = __index; return __retval != 0; } finally { - MarshalInspectable.DisposeMarshaler(__value); + MarshalInspectable.DisposeMarshaler(__value); } } @@ -1026,7 +1026,7 @@ private static unsafe int Do_Abi_GetAt_0(IntPtr thisPtr, uint index, IntPtr* res try { __result = FindAdapter(thisPtr).GetAt(index); - *result = MarshalInspectable.FromManaged(__result); + *result = MarshalInspectable.FromManaged(__result); } catch (Exception __exception__) @@ -1066,7 +1066,7 @@ private static unsafe int Do_Abi_IndexOf_3(IntPtr thisPtr, IntPtr value, uint* i uint __index = default; try { - __returnValue = FindAdapter(thisPtr).IndexOf(MarshalInspectable.FromAbi(value), out __index); + __returnValue = FindAdapter(thisPtr).IndexOf(MarshalInspectable.FromAbi(value), out __index); *index = __index; *returnValue = (byte)(__returnValue ? 1 : 0); } @@ -1084,7 +1084,7 @@ private static unsafe int Do_Abi_SetAt_4(IntPtr thisPtr, uint index, IntPtr valu { try { - FindAdapter(thisPtr).SetAt(index, MarshalInspectable.FromAbi(value)); + FindAdapter(thisPtr).SetAt(index, MarshalInspectable.FromAbi(value)); } catch (Exception __exception__) { @@ -1100,7 +1100,7 @@ private static unsafe int Do_Abi_InsertAt_5(IntPtr thisPtr, uint index, IntPtr v { try { - FindAdapter(thisPtr).InsertAt(index, MarshalInspectable.FromAbi(value)); + FindAdapter(thisPtr).InsertAt(index, MarshalInspectable.FromAbi(value)); } catch (Exception __exception__) { @@ -1132,7 +1132,7 @@ private static unsafe int Do_Abi_Append_7(IntPtr thisPtr, IntPtr value) { try { - FindAdapter(thisPtr).Append(MarshalInspectable.FromAbi(value)); + FindAdapter(thisPtr).Append(MarshalInspectable.FromAbi(value)); } catch (Exception __exception__) { @@ -1218,11 +1218,11 @@ private static FromAbiHelper _VectorToList(IWinRTObject _this) try { global::WinRT.ExceptionHelpers.ThrowExceptionForHR(_obj.Vftbl.GetAt_0(ThisPtr, index, &__retval)); - return MarshalInspectable.FromAbi(__retval); + return MarshalInspectable.FromAbi(__retval); } finally { - MarshalInspectable.DisposeAbi(__retval); + MarshalInspectable.DisposeAbi(__retval); } } @@ -1251,14 +1251,14 @@ private static FromAbiHelper _VectorToList(IWinRTObject _this) byte __retval = default; try { - __value = MarshalInspectable.CreateMarshaler(value); - global::WinRT.ExceptionHelpers.ThrowExceptionForHR(_obj.Vftbl.IndexOf_3(ThisPtr, MarshalInspectable.GetAbi(__value), &__index, &__retval)); + __value = MarshalInspectable.CreateMarshaler(value); + global::WinRT.ExceptionHelpers.ThrowExceptionForHR(_obj.Vftbl.IndexOf_3(ThisPtr, MarshalInspectable.GetAbi(__value), &__index, &__retval)); index = __index; return __retval != 0; } finally { - MarshalInspectable.DisposeMarshaler(__value); + MarshalInspectable.DisposeMarshaler(__value); } } @@ -1269,12 +1269,12 @@ private static FromAbiHelper _VectorToList(IWinRTObject _this) IObjectReference __value = default; try { - __value = MarshalInspectable.CreateMarshaler(value); - global::WinRT.ExceptionHelpers.ThrowExceptionForHR(_obj.Vftbl.SetAt_4(ThisPtr, index, MarshalInspectable.GetAbi(__value))); + __value = MarshalInspectable.CreateMarshaler(value); + global::WinRT.ExceptionHelpers.ThrowExceptionForHR(_obj.Vftbl.SetAt_4(ThisPtr, index, MarshalInspectable.GetAbi(__value))); } finally { - MarshalInspectable.DisposeMarshaler(__value); + MarshalInspectable.DisposeMarshaler(__value); } } @@ -1285,12 +1285,12 @@ private static FromAbiHelper _VectorToList(IWinRTObject _this) IObjectReference __value = default; try { - __value = MarshalInspectable.CreateMarshaler(value); - global::WinRT.ExceptionHelpers.ThrowExceptionForHR(_obj.Vftbl.InsertAt_5(ThisPtr, index, MarshalInspectable.GetAbi(__value))); + __value = MarshalInspectable.CreateMarshaler(value); + global::WinRT.ExceptionHelpers.ThrowExceptionForHR(_obj.Vftbl.InsertAt_5(ThisPtr, index, MarshalInspectable.GetAbi(__value))); } finally { - MarshalInspectable.DisposeMarshaler(__value); + MarshalInspectable.DisposeMarshaler(__value); } } @@ -1308,12 +1308,12 @@ private static FromAbiHelper _VectorToList(IWinRTObject _this) IObjectReference __value = default; try { - __value = MarshalInspectable.CreateMarshaler(value); - global::WinRT.ExceptionHelpers.ThrowExceptionForHR(_obj.Vftbl.Append_7(ThisPtr, MarshalInspectable.GetAbi(__value))); + __value = MarshalInspectable.CreateMarshaler(value); + global::WinRT.ExceptionHelpers.ThrowExceptionForHR(_obj.Vftbl.Append_7(ThisPtr, MarshalInspectable.GetAbi(__value))); } finally { - MarshalInspectable.DisposeMarshaler(__value); + MarshalInspectable.DisposeMarshaler(__value); } } diff --git a/WinRT.Runtime/Projections/Bindable.netstandard2.0.cs b/WinRT.Runtime/Projections/Bindable.netstandard2.0.cs index 6812a7b7f..1afea7abf 100644 --- a/WinRT.Runtime/Projections/Bindable.netstandard2.0.cs +++ b/WinRT.Runtime/Projections/Bindable.netstandard2.0.cs @@ -118,7 +118,7 @@ private static unsafe int Do_Abi_get_Current_0(IntPtr thisPtr, IntPtr* value) try { __value = global::WinRT.ComWrappersSupport.FindObject(thisPtr).Current; - *value = MarshalInspectable.FromManaged(__value); + *value = MarshalInspectable.FromManaged(__value); } catch (Exception __exception__) { @@ -174,11 +174,11 @@ public unsafe object Current try { global::WinRT.ExceptionHelpers.ThrowExceptionForHR(_obj.Vftbl.get_Current_0(ThisPtr, &__retval)); - return MarshalInspectable.FromAbi(__retval); + return MarshalInspectable.FromAbi(__retval); } finally { - MarshalInspectable.DisposeAbi(__retval); + MarshalInspectable.DisposeAbi(__retval); } } } @@ -246,7 +246,7 @@ private static unsafe int Do_Abi_GetAt_0(IntPtr thisPtr, uint index, IntPtr* res try { __result = global::WinRT.ComWrappersSupport.FindObject(thisPtr).GetAt(index); - *result = MarshalInspectable.FromManaged(__result); + *result = MarshalInspectable.FromManaged(__result); } catch (Exception __exception__) @@ -267,7 +267,7 @@ private static unsafe int Do_Abi_IndexOf_2(IntPtr thisPtr, IntPtr value, uint* i try { - __returnValue = global::WinRT.ComWrappersSupport.FindObject(thisPtr).IndexOf(MarshalInspectable.FromAbi(value), out __index); + __returnValue = global::WinRT.ComWrappersSupport.FindObject(thisPtr).IndexOf(MarshalInspectable.FromAbi(value), out __index); *index = __index; *returnValue = (byte)(__returnValue ? 1 : 0); @@ -322,11 +322,11 @@ public unsafe object GetAt(uint index) try { global::WinRT.ExceptionHelpers.ThrowExceptionForHR(_obj.Vftbl.GetAt_0(ThisPtr, index, &__retval)); - return MarshalInspectable.FromAbi(__retval); + return MarshalInspectable.FromAbi(__retval); } finally { - MarshalInspectable.DisposeAbi(__retval); + MarshalInspectable.DisposeAbi(__retval); } } @@ -337,14 +337,14 @@ public unsafe bool IndexOf(object value, out uint index) byte __retval = default; try { - __value = MarshalInspectable.CreateMarshaler(value); - global::WinRT.ExceptionHelpers.ThrowExceptionForHR(_obj.Vftbl.IndexOf_2(ThisPtr, MarshalInspectable.GetAbi(__value), &__index, &__retval)); + __value = MarshalInspectable.CreateMarshaler(value); + global::WinRT.ExceptionHelpers.ThrowExceptionForHR(_obj.Vftbl.IndexOf_2(ThisPtr, MarshalInspectable.GetAbi(__value), &__index, &__retval)); index = __index; return __retval != 0; } finally { - MarshalInspectable.DisposeMarshaler(__value); + MarshalInspectable.DisposeMarshaler(__value); } } @@ -1028,7 +1028,7 @@ private static unsafe int Do_Abi_GetAt_0(IntPtr thisPtr, uint index, IntPtr* res try { __result = FindAdapter(thisPtr).GetAt(index); - *result = MarshalInspectable.FromManaged(__result); + *result = MarshalInspectable.FromManaged(__result); } catch (Exception __exception__) @@ -1064,7 +1064,7 @@ private static unsafe int Do_Abi_IndexOf_3(IntPtr thisPtr, IntPtr value, uint* i uint __index = default; try { - __returnValue = FindAdapter(thisPtr).IndexOf(MarshalInspectable.FromAbi(value), out __index); + __returnValue = FindAdapter(thisPtr).IndexOf(MarshalInspectable.FromAbi(value), out __index); *index = __index; *returnValue = (byte)(__returnValue ? 1 : 0); } @@ -1080,7 +1080,7 @@ private static unsafe int Do_Abi_SetAt_4(IntPtr thisPtr, uint index, IntPtr valu { try { - FindAdapter(thisPtr).SetAt(index, MarshalInspectable.FromAbi(value)); + FindAdapter(thisPtr).SetAt(index, MarshalInspectable.FromAbi(value)); } catch (Exception __exception__) { @@ -1094,7 +1094,7 @@ private static unsafe int Do_Abi_InsertAt_5(IntPtr thisPtr, uint index, IntPtr v { try { - FindAdapter(thisPtr).InsertAt(index, MarshalInspectable.FromAbi(value)); + FindAdapter(thisPtr).InsertAt(index, MarshalInspectable.FromAbi(value)); } catch (Exception __exception__) { @@ -1122,7 +1122,7 @@ private static unsafe int Do_Abi_Append_7(IntPtr thisPtr, IntPtr value) { try { - FindAdapter(thisPtr).Append(MarshalInspectable.FromAbi(value)); + FindAdapter(thisPtr).Append(MarshalInspectable.FromAbi(value)); } catch (Exception __exception__) { @@ -1208,11 +1208,11 @@ public unsafe object GetAt(uint index) try { global::WinRT.ExceptionHelpers.ThrowExceptionForHR(_obj.Vftbl.GetAt_0(ThisPtr, index, &__retval)); - return MarshalInspectable.FromAbi(__retval); + return MarshalInspectable.FromAbi(__retval); } finally { - MarshalInspectable.DisposeAbi(__retval); + MarshalInspectable.DisposeAbi(__retval); } } @@ -1237,14 +1237,14 @@ public unsafe bool IndexOf(object value, out uint index) byte __retval = default; try { - __value = MarshalInspectable.CreateMarshaler(value); - global::WinRT.ExceptionHelpers.ThrowExceptionForHR(_obj.Vftbl.IndexOf_3(ThisPtr, MarshalInspectable.GetAbi(__value), &__index, &__retval)); + __value = MarshalInspectable.CreateMarshaler(value); + global::WinRT.ExceptionHelpers.ThrowExceptionForHR(_obj.Vftbl.IndexOf_3(ThisPtr, MarshalInspectable.GetAbi(__value), &__index, &__retval)); index = __index; return __retval != 0; } finally { - MarshalInspectable.DisposeMarshaler(__value); + MarshalInspectable.DisposeMarshaler(__value); } } @@ -1253,12 +1253,12 @@ public unsafe void SetAt(uint index, object value) IObjectReference __value = default; try { - __value = MarshalInspectable.CreateMarshaler(value); - global::WinRT.ExceptionHelpers.ThrowExceptionForHR(_obj.Vftbl.SetAt_4(ThisPtr, index, MarshalInspectable.GetAbi(__value))); + __value = MarshalInspectable.CreateMarshaler(value); + global::WinRT.ExceptionHelpers.ThrowExceptionForHR(_obj.Vftbl.SetAt_4(ThisPtr, index, MarshalInspectable.GetAbi(__value))); } finally { - MarshalInspectable.DisposeMarshaler(__value); + MarshalInspectable.DisposeMarshaler(__value); } } @@ -1267,12 +1267,12 @@ public unsafe void InsertAt(uint index, object value) IObjectReference __value = default; try { - __value = MarshalInspectable.CreateMarshaler(value); - global::WinRT.ExceptionHelpers.ThrowExceptionForHR(_obj.Vftbl.InsertAt_5(ThisPtr, index, MarshalInspectable.GetAbi(__value))); + __value = MarshalInspectable.CreateMarshaler(value); + global::WinRT.ExceptionHelpers.ThrowExceptionForHR(_obj.Vftbl.InsertAt_5(ThisPtr, index, MarshalInspectable.GetAbi(__value))); } finally { - MarshalInspectable.DisposeMarshaler(__value); + MarshalInspectable.DisposeMarshaler(__value); } } @@ -1286,12 +1286,12 @@ public unsafe void Append(object value) IObjectReference __value = default; try { - __value = MarshalInspectable.CreateMarshaler(value); - global::WinRT.ExceptionHelpers.ThrowExceptionForHR(_obj.Vftbl.Append_7(ThisPtr, MarshalInspectable.GetAbi(__value))); + __value = MarshalInspectable.CreateMarshaler(value); + global::WinRT.ExceptionHelpers.ThrowExceptionForHR(_obj.Vftbl.Append_7(ThisPtr, MarshalInspectable.GetAbi(__value))); } finally { - MarshalInspectable.DisposeMarshaler(__value); + MarshalInspectable.DisposeMarshaler(__value); } } diff --git a/WinRT.Runtime/Projections/DataErrorsChangedEventArgs.cs b/WinRT.Runtime/Projections/DataErrorsChangedEventArgs.cs index e3c5b1bbf..570fed9f4 100644 --- a/WinRT.Runtime/Projections/DataErrorsChangedEventArgs.cs +++ b/WinRT.Runtime/Projections/DataErrorsChangedEventArgs.cs @@ -62,7 +62,7 @@ public unsafe IObjectReference CreateInstance(string name) finally { MarshalString.DisposeMarshaler(__name); - MarshalInspectable.DisposeAbi(__retval); + MarshalInspectable.DisposeAbi(__retval); } } } diff --git a/WinRT.Runtime/Projections/EventHandler.cs b/WinRT.Runtime/Projections/EventHandler.cs index 923c8cac1..b1704ad5f 100644 --- a/WinRT.Runtime/Projections/EventHandler.cs +++ b/WinRT.Runtime/Projections/EventHandler.cs @@ -77,15 +77,15 @@ public void Invoke(object sender, T args) var __params = new object[] { ThisPtr, null, null }; try { - __sender = MarshalInspectable.CreateMarshaler(sender); - __params[1] = MarshalInspectable.GetAbi(__sender); + __sender = MarshalInspectable.CreateMarshaler(sender); + __params[1] = MarshalInspectable.GetAbi(__sender); __args = Marshaler.CreateMarshaler(args); __params[2] = Marshaler.GetAbi(__args); abiInvoke.DynamicInvokeAbi(__params); } finally { - MarshalInspectable.DisposeMarshaler(__sender); + MarshalInspectable.DisposeMarshaler(__sender); Marshaler.DisposeMarshaler(__args); } @@ -105,7 +105,7 @@ private static unsafe int Do_Abi_Invoke(void* thisPtr, IntPtr sender, TAbi { global::WinRT.ComWrappersSupport.MarshalDelegateInvoke(new IntPtr(thisPtr), (global::System.Delegate invoke) => { - invoke.DynamicInvoke(MarshalInspectable.FromAbi(sender), Marshaler.FromAbi(args)); + invoke.DynamicInvoke(MarshalInspectable.FromAbi(sender), Marshaler.FromAbi(args)); }); } catch (global::System.Exception __exception__) diff --git a/WinRT.Runtime/Projections/ICommand.net5.cs b/WinRT.Runtime/Projections/ICommand.net5.cs index 67b2aa39f..ef6987559 100644 --- a/WinRT.Runtime/Projections/ICommand.net5.cs +++ b/WinRT.Runtime/Projections/ICommand.net5.cs @@ -76,16 +76,16 @@ public void Invoke(object sender, EventArgs args) var __params = new object[] { ThisPtr, null, null }; try { - __sender = MarshalInspectable.CreateMarshaler(sender); - __params[1] = MarshalInspectable.GetAbi(__sender); - __args = MarshalInspectable.CreateMarshaler(args); - __params[2] = MarshalInspectable.GetAbi(__args); + __sender = MarshalInspectable.CreateMarshaler(sender); + __params[1] = MarshalInspectable.GetAbi(__sender); + __args = MarshalInspectable.CreateMarshaler(args); + __params[2] = MarshalInspectable.GetAbi(__args); abiInvoke.DynamicInvokeAbi(__params); } finally { - MarshalInspectable.DisposeMarshaler(__sender); - MarshalInspectable.DisposeMarshaler(__args); + MarshalInspectable.DisposeMarshaler(__sender); + MarshalInspectable.DisposeMarshaler(__args); } } @@ -105,8 +105,8 @@ private static unsafe int Do_Abi_Invoke(IntPtr thisPtr, IntPtr sender, IntPtr ar global::WinRT.ComWrappersSupport.MarshalDelegateInvoke(thisPtr, (global::System.Delegate invoke) => { invoke.DynamicInvoke( - MarshalInspectable.FromAbi(sender), - MarshalInspectable.FromAbi(args) ?? EventArgs.Empty); + MarshalInspectable.FromAbi(sender), + MarshalInspectable.FromAbi(args) as EventArgs ?? EventArgs.Empty); }); } catch (global::System.Exception __exception__) @@ -187,7 +187,7 @@ private static unsafe int Do_Abi_CanExecute_2(IntPtr thisPtr, IntPtr parameter, try { - __result = global::WinRT.ComWrappersSupport.FindObject(thisPtr).CanExecute(MarshalInspectable.FromAbi(parameter)); *result = (byte)(__result ? 1 : 0); + __result = global::WinRT.ComWrappersSupport.FindObject(thisPtr).CanExecute(MarshalInspectable.FromAbi(parameter)); *result = (byte)(__result ? 1 : 0); } catch (global::System.Exception __exception__) @@ -205,7 +205,7 @@ private static unsafe int Do_Abi_Execute_3(IntPtr thisPtr, IntPtr parameter) { try { - global::WinRT.ComWrappersSupport.FindObject(thisPtr).Execute(MarshalInspectable.FromAbi(parameter)); + global::WinRT.ComWrappersSupport.FindObject(thisPtr).Execute(MarshalInspectable.FromAbi(parameter)); } catch (global::System.Exception __exception__) { @@ -275,13 +275,13 @@ private static CanExecuteChangedEventSource _CanExecuteChanged(IWinRTObject _thi { var _obj = ((ObjectReference)((IWinRTObject)this).GetObjectReferenceForType(typeof(global::System.Windows.Input.ICommand).TypeHandle)); var ThisPtr = _obj.ThisPtr; - __parameter = MarshalInspectable.CreateMarshaler(parameter); - global::WinRT.ExceptionHelpers.ThrowExceptionForHR(_obj.Vftbl.CanExecute_2(ThisPtr, MarshalInspectable.GetAbi(__parameter), out __retval)); + __parameter = MarshalInspectable.CreateMarshaler(parameter); + global::WinRT.ExceptionHelpers.ThrowExceptionForHR(_obj.Vftbl.CanExecute_2(ThisPtr, MarshalInspectable.GetAbi(__parameter), out __retval)); return __retval != 0; } finally { - MarshalInspectable.DisposeMarshaler(__parameter); + MarshalInspectable.DisposeMarshaler(__parameter); } } @@ -292,12 +292,12 @@ private static CanExecuteChangedEventSource _CanExecuteChanged(IWinRTObject _thi IObjectReference __parameter = default; try { - __parameter = MarshalInspectable.CreateMarshaler(parameter); - global::WinRT.ExceptionHelpers.ThrowExceptionForHR(_obj.Vftbl.Execute_3(ThisPtr, MarshalInspectable.GetAbi(__parameter))); + __parameter = MarshalInspectable.CreateMarshaler(parameter); + global::WinRT.ExceptionHelpers.ThrowExceptionForHR(_obj.Vftbl.Execute_3(ThisPtr, MarshalInspectable.GetAbi(__parameter))); } finally { - MarshalInspectable.DisposeMarshaler(__parameter); + MarshalInspectable.DisposeMarshaler(__parameter); } } diff --git a/WinRT.Runtime/Projections/ICommand.netstandard2.0.cs b/WinRT.Runtime/Projections/ICommand.netstandard2.0.cs index db0319c66..13fb26bed 100644 --- a/WinRT.Runtime/Projections/ICommand.netstandard2.0.cs +++ b/WinRT.Runtime/Projections/ICommand.netstandard2.0.cs @@ -76,16 +76,16 @@ public void Invoke(object sender, EventArgs args) var __params = new object[] { ThisPtr, null, null }; try { - __sender = MarshalInspectable.CreateMarshaler(sender); - __params[1] = MarshalInspectable.GetAbi(__sender); - __args = MarshalInspectable.CreateMarshaler(args); - __params[2] = MarshalInspectable.GetAbi(__args); + __sender = MarshalInspectable.CreateMarshaler(sender); + __params[1] = MarshalInspectable.GetAbi(__sender); + __args = MarshalInspectable.CreateMarshaler(args); + __params[2] = MarshalInspectable.GetAbi(__args); abiInvoke.DynamicInvokeAbi(__params); } finally { - MarshalInspectable.DisposeMarshaler(__sender); - MarshalInspectable.DisposeMarshaler(__args); + MarshalInspectable.DisposeMarshaler(__sender); + MarshalInspectable.DisposeMarshaler(__args); } } @@ -105,8 +105,8 @@ private static unsafe int Do_Abi_Invoke(IntPtr thisPtr, IntPtr sender, IntPtr ar global::WinRT.ComWrappersSupport.MarshalDelegateInvoke(thisPtr, (global::System.Delegate invoke) => { invoke.DynamicInvoke( - MarshalInspectable.FromAbi(sender), - MarshalInspectable.FromAbi(args) ?? EventArgs.Empty); + MarshalInspectable.FromAbi(sender), + MarshalInspectable.FromAbi(args) as EventArgs ?? EventArgs.Empty); }); } catch (global::System.Exception __exception__) @@ -185,7 +185,7 @@ private static unsafe int Do_Abi_CanExecute_2(IntPtr thisPtr, IntPtr parameter, try { - __result = global::WinRT.ComWrappersSupport.FindObject(thisPtr).CanExecute(MarshalInspectable.FromAbi(parameter)); *result = (byte)(__result ? 1 : 0); + __result = global::WinRT.ComWrappersSupport.FindObject(thisPtr).CanExecute(MarshalInspectable.FromAbi(parameter)); *result = (byte)(__result ? 1 : 0); } catch (global::System.Exception __exception__) @@ -201,7 +201,7 @@ private static unsafe int Do_Abi_Execute_3(IntPtr thisPtr, IntPtr parameter) { try { - global::WinRT.ComWrappersSupport.FindObject(thisPtr).Execute(MarshalInspectable.FromAbi(parameter)); + global::WinRT.ComWrappersSupport.FindObject(thisPtr).Execute(MarshalInspectable.FromAbi(parameter)); } catch (global::System.Exception __exception__) { @@ -275,13 +275,13 @@ public unsafe bool CanExecute(object parameter) byte __retval = default; try { - __parameter = MarshalInspectable.CreateMarshaler(parameter); - global::WinRT.ExceptionHelpers.ThrowExceptionForHR(_obj.Vftbl.CanExecute_2(ThisPtr, MarshalInspectable.GetAbi(__parameter), out __retval)); + __parameter = MarshalInspectable.CreateMarshaler(parameter); + global::WinRT.ExceptionHelpers.ThrowExceptionForHR(_obj.Vftbl.CanExecute_2(ThisPtr, MarshalInspectable.GetAbi(__parameter), out __retval)); return __retval != 0; } finally { - MarshalInspectable.DisposeMarshaler(__parameter); + MarshalInspectable.DisposeMarshaler(__parameter); } } @@ -290,12 +290,12 @@ public unsafe void Execute(object parameter) IObjectReference __parameter = default; try { - __parameter = MarshalInspectable.CreateMarshaler(parameter); - global::WinRT.ExceptionHelpers.ThrowExceptionForHR(_obj.Vftbl.Execute_3(ThisPtr, MarshalInspectable.GetAbi(__parameter))); + __parameter = MarshalInspectable.CreateMarshaler(parameter); + global::WinRT.ExceptionHelpers.ThrowExceptionForHR(_obj.Vftbl.Execute_3(ThisPtr, MarshalInspectable.GetAbi(__parameter))); } finally { - MarshalInspectable.DisposeMarshaler(__parameter); + MarshalInspectable.DisposeMarshaler(__parameter); } } diff --git a/WinRT.Runtime/Projections/ICustomPropertyProvider.net5.cs b/WinRT.Runtime/Projections/ICustomPropertyProvider.net5.cs index ec0d2ba47..84dc22353 100644 --- a/WinRT.Runtime/Projections/ICustomPropertyProvider.net5.cs +++ b/WinRT.Runtime/Projections/ICustomPropertyProvider.net5.cs @@ -76,8 +76,8 @@ private static unsafe int Do_Abi_GetValue_2(IntPtr thisPtr, IntPtr target, IntPt try { - __result = global::WinRT.ComWrappersSupport.FindObject(thisPtr).GetValue(MarshalInspectable.FromAbi(target)); - *result = MarshalInspectable.FromManaged(__result); + __result = global::WinRT.ComWrappersSupport.FindObject(thisPtr).GetValue(MarshalInspectable.FromAbi(target)); + *result = MarshalInspectable.FromManaged(__result); } catch (Exception __exception__) @@ -96,7 +96,7 @@ private static unsafe int Do_Abi_SetValue_3(IntPtr thisPtr, IntPtr target, IntPt try { - global::WinRT.ComWrappersSupport.FindObject(thisPtr).SetValue(MarshalInspectable.FromAbi(target), MarshalInspectable.FromAbi(value)); + global::WinRT.ComWrappersSupport.FindObject(thisPtr).SetValue(MarshalInspectable.FromAbi(target), MarshalInspectable.FromAbi(value)); } catch (Exception __exception__) { @@ -114,8 +114,8 @@ private static unsafe int Do_Abi_GetIndexedValue_4(IntPtr thisPtr, IntPtr target try { - __result = global::WinRT.ComWrappersSupport.FindObject(thisPtr).GetIndexedValue(MarshalInspectable.FromAbi(target), MarshalInspectable.FromAbi(index)); - *result = MarshalInspectable.FromManaged(__result); + __result = global::WinRT.ComWrappersSupport.FindObject(thisPtr).GetIndexedValue(MarshalInspectable.FromAbi(target), MarshalInspectable.FromAbi(index)); + *result = MarshalInspectable.FromManaged(__result); } catch (Exception __exception__) @@ -134,7 +134,7 @@ private static unsafe int Do_Abi_SetIndexedValue_5(IntPtr thisPtr, IntPtr target try { - global::WinRT.ComWrappersSupport.FindObject(thisPtr).SetIndexedValue(MarshalInspectable.FromAbi(target), MarshalInspectable.FromAbi(value), MarshalInspectable.FromAbi(index)); + global::WinRT.ComWrappersSupport.FindObject(thisPtr).SetIndexedValue(MarshalInspectable.FromAbi(target), MarshalInspectable.FromAbi(value), MarshalInspectable.FromAbi(index)); } catch (Exception __exception__) { diff --git a/WinRT.Runtime/Projections/ICustomPropertyProvider.netstandard2.0.cs b/WinRT.Runtime/Projections/ICustomPropertyProvider.netstandard2.0.cs index 5e0b6e36d..f9b8683b7 100644 --- a/WinRT.Runtime/Projections/ICustomPropertyProvider.netstandard2.0.cs +++ b/WinRT.Runtime/Projections/ICustomPropertyProvider.netstandard2.0.cs @@ -76,8 +76,8 @@ private static unsafe int Do_Abi_GetValue_2(IntPtr thisPtr, IntPtr target, IntPt try { - __result = global::WinRT.ComWrappersSupport.FindObject(thisPtr).GetValue(MarshalInspectable.FromAbi(target)); - *result = MarshalInspectable.FromManaged(__result); + __result = global::WinRT.ComWrappersSupport.FindObject(thisPtr).GetValue(MarshalInspectable.FromAbi(target)); + *result = MarshalInspectable.FromManaged(__result); } catch (Exception __exception__) @@ -94,7 +94,7 @@ private static unsafe int Do_Abi_SetValue_3(IntPtr thisPtr, IntPtr target, IntPt try { - global::WinRT.ComWrappersSupport.FindObject(thisPtr).SetValue(MarshalInspectable.FromAbi(target), MarshalInspectable.FromAbi(value)); + global::WinRT.ComWrappersSupport.FindObject(thisPtr).SetValue(MarshalInspectable.FromAbi(target), MarshalInspectable.FromAbi(value)); } catch (Exception __exception__) { @@ -110,8 +110,8 @@ private static unsafe int Do_Abi_GetIndexedValue_4(IntPtr thisPtr, IntPtr target try { - __result = global::WinRT.ComWrappersSupport.FindObject(thisPtr).GetIndexedValue(MarshalInspectable.FromAbi(target), MarshalInspectable.FromAbi(index)); - *result = MarshalInspectable.FromManaged(__result); + __result = global::WinRT.ComWrappersSupport.FindObject(thisPtr).GetIndexedValue(MarshalInspectable.FromAbi(target), MarshalInspectable.FromAbi(index)); + *result = MarshalInspectable.FromManaged(__result); } catch (Exception __exception__) @@ -128,7 +128,7 @@ private static unsafe int Do_Abi_SetIndexedValue_5(IntPtr thisPtr, IntPtr target try { - global::WinRT.ComWrappersSupport.FindObject(thisPtr).SetIndexedValue(MarshalInspectable.FromAbi(target), MarshalInspectable.FromAbi(value), MarshalInspectable.FromAbi(index)); + global::WinRT.ComWrappersSupport.FindObject(thisPtr).SetIndexedValue(MarshalInspectable.FromAbi(target), MarshalInspectable.FromAbi(value), MarshalInspectable.FromAbi(index)); } catch (Exception __exception__) { diff --git a/WinRT.Runtime/Projections/IPropertyValue.net5.cs b/WinRT.Runtime/Projections/IPropertyValue.net5.cs index a462bc323..5ffcbe63b 100644 --- a/WinRT.Runtime/Projections/IPropertyValue.net5.cs +++ b/WinRT.Runtime/Projections/IPropertyValue.net5.cs @@ -982,7 +982,7 @@ private static unsafe int Do_Abi_GetInspectableArray_32(IntPtr thisPtr, int* __v try { __value = CoerceArray(global::WinRT.ComWrappersSupport.FindObject(thisPtr)); - (*__valueSize, *value) = MarshalInspectable.FromManagedArray(__value); + (*__valueSize, *value) = MarshalInspectable.FromManagedArray(__value); } catch (global::System.Exception __exception__) { @@ -1733,11 +1733,11 @@ public struct Vftbl try { global::WinRT.ExceptionHelpers.ThrowExceptionForHR(_obj.Vftbl.GetInspectableArray_32(ThisPtr, out __value_length, out __value_data)); - value = MarshalInspectable.FromAbiArray((__value_length, __value_data)); + value = MarshalInspectable.FromAbiArray((__value_length, __value_data)); } finally { - MarshalInspectable.DisposeAbiArray((__value_length, __value_data)); + MarshalInspectable.DisposeAbiArray((__value_length, __value_data)); } } diff --git a/WinRT.Runtime/Projections/IPropertyValue.netstandard2.0.cs b/WinRT.Runtime/Projections/IPropertyValue.netstandard2.0.cs index 6f47ffae8..3b62fc6ee 100644 --- a/WinRT.Runtime/Projections/IPropertyValue.netstandard2.0.cs +++ b/WinRT.Runtime/Projections/IPropertyValue.netstandard2.0.cs @@ -922,7 +922,7 @@ private static unsafe int Do_Abi_GetInspectableArray_32(IntPtr thisPtr, int* __v try { __value = CoerceArray(global::WinRT.ComWrappersSupport.FindObject(thisPtr)); - (*__valueSize, *value) = MarshalInspectable.FromManagedArray(__value); + (*__valueSize, *value) = MarshalInspectable.FromManagedArray(__value); } catch (global::System.Exception __exception__) { @@ -1610,11 +1610,11 @@ public unsafe void GetInspectableArray(out object[] value) try { global::WinRT.ExceptionHelpers.ThrowExceptionForHR(_obj.Vftbl.GetInspectableArray_32(ThisPtr, out __value_length, out __value_data)); - value = MarshalInspectable.FromAbiArray((__value_length, __value_data)); + value = MarshalInspectable.FromAbiArray((__value_length, __value_data)); } finally { - MarshalInspectable.DisposeAbiArray((__value_length, __value_data)); + MarshalInspectable.DisposeAbiArray((__value_length, __value_data)); } } diff --git a/WinRT.Runtime/Projections/IServiceProvider.cs b/WinRT.Runtime/Projections/IServiceProvider.cs index 4a339e604..fe57edafc 100644 --- a/WinRT.Runtime/Projections/IServiceProvider.cs +++ b/WinRT.Runtime/Projections/IServiceProvider.cs @@ -57,7 +57,7 @@ private static unsafe int Do_Abi_GetService_0(IntPtr thisPtr, global::ABI.System try { __result = global::WinRT.ComWrappersSupport.FindObject(thisPtr).GetService(global::ABI.System.Type.FromAbi(type)); - *result = MarshalInspectable.FromManaged(__result); + *result = MarshalInspectable.FromManaged(__result); } catch (global::System.Exception __exception__) { @@ -89,12 +89,12 @@ public unsafe object GetService(global::System.Type type) { __type = global::ABI.System.Type.CreateMarshaler(type); global::WinRT.ExceptionHelpers.ThrowExceptionForHR(_obj.Vftbl.GetService_0(ThisPtr, global::ABI.System.Type.GetAbi(__type), &__retval)); - return MarshalInspectable.FromAbi(__retval); + return MarshalInspectable.FromAbi(__retval); } finally { global::ABI.System.Type.DisposeMarshaler(__type); - MarshalInspectable.DisposeAbi(__retval); + MarshalInspectable.DisposeAbi(__retval); } } } diff --git a/WinRT.Runtime/Projections/NotifyCollectionChangedEventArgs.cs b/WinRT.Runtime/Projections/NotifyCollectionChangedEventArgs.cs index a8d815b0e..9f7f0c7e3 100644 --- a/WinRT.Runtime/Projections/NotifyCollectionChangedEventArgs.cs +++ b/WinRT.Runtime/Projections/NotifyCollectionChangedEventArgs.cs @@ -144,8 +144,8 @@ public unsafe IObjectReference CreateInstanceWithAllParameters(global::System.Co { __newItems = MarshalInterface.CreateMarshaler(newItems); __oldItems = MarshalInterface.CreateMarshaler(oldItems); - __baseInterface = MarshalInspectable.CreateMarshaler(baseInterface); - global::WinRT.ExceptionHelpers.ThrowExceptionForHR(_obj.Vftbl.CreateInstanceWithAllParameters_0(ThisPtr, action, MarshalInterface.GetAbi(__newItems), MarshalInterface.GetAbi(__oldItems), newIndex, oldIndex, MarshalInspectable.GetAbi(__baseInterface), out __innerInterface, out __retval)); + __baseInterface = MarshalInspectable.CreateMarshaler(baseInterface); + global::WinRT.ExceptionHelpers.ThrowExceptionForHR(_obj.Vftbl.CreateInstanceWithAllParameters_0(ThisPtr, action, MarshalInterface.GetAbi(__newItems), MarshalInterface.GetAbi(__oldItems), newIndex, oldIndex, MarshalInspectable.GetAbi(__baseInterface), out __innerInterface, out __retval)); innerInterface = ObjectReference.FromAbi(__innerInterface); return ObjectReference.FromAbi(__retval); } @@ -153,9 +153,9 @@ public unsafe IObjectReference CreateInstanceWithAllParameters(global::System.Co { MarshalInterface.DisposeMarshaler(__newItems); MarshalInterface.DisposeMarshaler(__oldItems); - MarshalInspectable.DisposeMarshaler(__baseInterface); - MarshalInspectable.DisposeAbi(__innerInterface); - MarshalInspectable.DisposeAbi(__retval); + MarshalInspectable.DisposeMarshaler(__baseInterface); + MarshalInspectable.DisposeAbi(__innerInterface); + MarshalInspectable.DisposeAbi(__retval); } } } diff --git a/WinRT.Runtime/Projections/NotifyCollectionChangedEventHandler.cs b/WinRT.Runtime/Projections/NotifyCollectionChangedEventHandler.cs index be9727f67..4448430f7 100644 --- a/WinRT.Runtime/Projections/NotifyCollectionChangedEventHandler.cs +++ b/WinRT.Runtime/Projections/NotifyCollectionChangedEventHandler.cs @@ -72,13 +72,13 @@ public void Invoke(object sender, global::System.Collections.Specialized.NotifyC IObjectReference __e = default; try { - __sender = MarshalInspectable.CreateMarshaler(sender); + __sender = MarshalInspectable.CreateMarshaler(sender); __e = global::ABI.System.Collections.Specialized.NotifyCollectionChangedEventArgs.CreateMarshaler(e); - global::WinRT.ExceptionHelpers.ThrowExceptionForHR(abiInvoke(ThisPtr, MarshalInspectable.GetAbi(__sender), global::ABI.System.Collections.Specialized.NotifyCollectionChangedEventArgs.GetAbi(__e))); + global::WinRT.ExceptionHelpers.ThrowExceptionForHR(abiInvoke(ThisPtr, MarshalInspectable.GetAbi(__sender), global::ABI.System.Collections.Specialized.NotifyCollectionChangedEventArgs.GetAbi(__e))); } finally { - MarshalInspectable.DisposeMarshaler(__sender); + MarshalInspectable.DisposeMarshaler(__sender); global::ABI.System.Collections.Specialized.NotifyCollectionChangedEventArgs.DisposeMarshaler(__e); } @@ -98,7 +98,7 @@ private static unsafe int Do_Abi_Invoke(IntPtr thisPtr, IntPtr sender, IntPtr e) { global::WinRT.ComWrappersSupport.MarshalDelegateInvoke(thisPtr, (global::System.Collections.Specialized.NotifyCollectionChangedEventHandler invoke) => { - invoke(MarshalInspectable.FromAbi(sender), global::ABI.System.Collections.Specialized.NotifyCollectionChangedEventArgs.FromAbi(e)); + invoke(MarshalInspectable.FromAbi(sender), global::ABI.System.Collections.Specialized.NotifyCollectionChangedEventArgs.FromAbi(e)); }); } catch (global::System.Exception __exception__) diff --git a/WinRT.Runtime/Projections/PropertyChangedEventArgs.cs b/WinRT.Runtime/Projections/PropertyChangedEventArgs.cs index 0acc29810..ad231a7b4 100644 --- a/WinRT.Runtime/Projections/PropertyChangedEventArgs.cs +++ b/WinRT.Runtime/Projections/PropertyChangedEventArgs.cs @@ -53,17 +53,17 @@ public unsafe IObjectReference CreateInstance(string name, object baseInterface, try { __name = MarshalString.CreateMarshaler(name); - __baseInterface = MarshalInspectable.CreateMarshaler(baseInterface); - global::WinRT.ExceptionHelpers.ThrowExceptionForHR(_obj.Vftbl.CreateInstance_0(ThisPtr, MarshalString.GetAbi(__name), MarshalInspectable.GetAbi(__baseInterface), &__innerInterface, &__retval)); + __baseInterface = MarshalInspectable.CreateMarshaler(baseInterface); + global::WinRT.ExceptionHelpers.ThrowExceptionForHR(_obj.Vftbl.CreateInstance_0(ThisPtr, MarshalString.GetAbi(__name), MarshalInspectable.GetAbi(__baseInterface), &__innerInterface, &__retval)); innerInterface = ObjectReference.FromAbi(__innerInterface); return ObjectReference.Attach(ref __retval); } finally { MarshalString.DisposeMarshaler(__name); - MarshalInspectable.DisposeMarshaler(__baseInterface); - MarshalInspectable.DisposeAbi(__innerInterface); - MarshalInspectable.DisposeAbi(__retval); + MarshalInspectable.DisposeMarshaler(__baseInterface); + MarshalInspectable.DisposeAbi(__innerInterface); + MarshalInspectable.DisposeAbi(__retval); } } } diff --git a/WinRT.Runtime/Projections/PropertyChangedEventHandler.cs b/WinRT.Runtime/Projections/PropertyChangedEventHandler.cs index 95f3c885e..faf539856 100644 --- a/WinRT.Runtime/Projections/PropertyChangedEventHandler.cs +++ b/WinRT.Runtime/Projections/PropertyChangedEventHandler.cs @@ -71,13 +71,13 @@ public void Invoke(object sender, global::System.ComponentModel.PropertyChangedE IObjectReference __e = default; try { - __sender = MarshalInspectable.CreateMarshaler(sender); + __sender = MarshalInspectable.CreateMarshaler(sender); __e = global::ABI.System.ComponentModel.PropertyChangedEventArgs.CreateMarshaler(e); - global::WinRT.ExceptionHelpers.ThrowExceptionForHR(abiInvoke(ThisPtr, MarshalInspectable.GetAbi(__sender), global::ABI.System.ComponentModel.PropertyChangedEventArgs.GetAbi(__e))); + global::WinRT.ExceptionHelpers.ThrowExceptionForHR(abiInvoke(ThisPtr, MarshalInspectable.GetAbi(__sender), global::ABI.System.ComponentModel.PropertyChangedEventArgs.GetAbi(__e))); } finally { - MarshalInspectable.DisposeMarshaler(__sender); + MarshalInspectable.DisposeMarshaler(__sender); global::ABI.System.ComponentModel.PropertyChangedEventArgs.DisposeMarshaler(__e); } @@ -99,7 +99,7 @@ private static unsafe int Do_Abi_Invoke(IntPtr thisPtr, IntPtr sender, IntPtr e) { global::WinRT.ComWrappersSupport.MarshalDelegateInvoke(thisPtr, (global::System.ComponentModel.PropertyChangedEventHandler invoke) => { - invoke(MarshalInspectable.FromAbi(sender), global::ABI.System.ComponentModel.PropertyChangedEventArgs.FromAbi(e)); + invoke(MarshalInspectable.FromAbi(sender), global::ABI.System.ComponentModel.PropertyChangedEventArgs.FromAbi(e)); }); } catch (global::System.Exception __exception__) diff --git a/WinRT.Runtime/Projections/Uri.cs b/WinRT.Runtime/Projections/Uri.cs index 6755437a7..ad55ff0da 100644 --- a/WinRT.Runtime/Projections/Uri.cs +++ b/WinRT.Runtime/Projections/Uri.cs @@ -112,7 +112,7 @@ public static IObjectReference CreateMarshaler(global::System.Uri value) return null; } - using var uri = ObjectReference.FromAbi(ptr); + using var uri = ObjectReference.FromAbi(ptr).As(); IntPtr rawUri = IntPtr.Zero; try { diff --git a/WinRT.Runtime/WeakReference.cs b/WinRT.Runtime/WeakReference.cs index 37a0dc201..d4899a1b4 100644 --- a/WinRT.Runtime/WeakReference.cs +++ b/WinRT.Runtime/WeakReference.cs @@ -60,7 +60,7 @@ private static object ResolveNativeWeakReference(IWeakReference reference) return null; } using var resolved = reference.Resolve(typeof(IUnknownVftbl).GUID); - return ComWrappersSupport.CreateRcwForComObject(resolved.ThisPtr); + return ComWrappersSupport.CreateRcwForComObject(resolved.ThisPtr); } } } diff --git a/cswinrt/code_writers.h b/cswinrt/code_writers.h index 34c4c1d1e..d95484094 100644 --- a/cswinrt/code_writers.h +++ b/cswinrt/code_writers.h @@ -1153,7 +1153,7 @@ return %(ComWrappersSupport.GetObjectReferenceForInterface(ptr)); } finally { -MarshalInspectable.DisposeAbi(ptr); +MarshalInspectable.DisposeAbi(ptr); } }))()) { @@ -1218,7 +1218,7 @@ ComWrappersSupport.RegisterObjectForInterface(this, ThisPtr); } finally { -MarshalInspectable.DisposeAbi(ptr); +MarshalInspectable.DisposeAbi(ptr); } } )", @@ -1255,7 +1255,7 @@ ComWrappersSupport.RegisterObjectForInterface(this, ThisPtr); } finally { -MarshalInspectable.DisposeAbi(ptr); +MarshalInspectable.DisposeAbi(ptr); } } )", @@ -2244,7 +2244,7 @@ event % %;)", { if (is_out() && (local_type == "IntPtr" && param_type != "IntPtr")) { - w.write("MarshalInspectable.DisposeAbi(%);\n", get_marshaler_local(w)); + w.write("MarshalInspectable.DisposeAbi(%);\n", get_marshaler_local(w)); } return; } @@ -2346,7 +2346,7 @@ event % %;)", call(semantics, [&](object_type) { - m.marshaler_type = "MarshalInspectable"; + m.marshaler_type = "MarshalInspectable"; if (m.is_array()) { m.local_type = "MarshalInterfaceHelper.MarshalerArray"; @@ -3539,7 +3539,7 @@ remove => %.Unsubscribe(value); call(semantics, [&](object_type const&) { - m.marshaler_type = "MarshalInspectable"; + m.marshaler_type = "MarshalInspectable"; m.local_type = "object"; }, [&](type_definition const& type) @@ -4645,7 +4645,8 @@ return new %(comp); public static % FromAbi(IntPtr thisPtr) { if (thisPtr == IntPtr.Zero) return null; -return MarshalInspectable<%>.FromAbi(thisPtr); +var obj = MarshalInspectable.FromAbi(thisPtr); +return obj as %; } % private readonly % _comp; @@ -4703,7 +4704,8 @@ private % _default => _defaultLazy.Value; public static %% FromAbi(IntPtr thisPtr) { if (thisPtr == IntPtr.Zero) return null; -return MarshalInspectable<%>.FromAbi(thisPtr); +var obj = MarshalInspectable.FromAbi(thisPtr); +return obj is % ? (%)obj : new %((%)obj); } % %(% ifc)% @@ -4740,6 +4742,9 @@ private % AsInternal(InterfaceTag<%> _) => _default; derived_new, type_name, type_name, + type_name, + type_name, + default_interface_abi_name, type.Flags().Sealed() ? "internal" : "protected internal", type_name, default_interface_abi_name, @@ -4833,7 +4838,13 @@ private % _default => _defaultLazy.Value; public static %% FromAbi(IntPtr thisPtr) { if (thisPtr == IntPtr.Zero) return null; -return MarshalInspectable<%>.FromAbi(thisPtr); +var obj = MarshalInspectable.FromAbi(thisPtr); +if (obj is % result) +{ +return result; +} +using IObjectReference objRef = MarshalInspectable.CreateMarshaler(obj); +return new %(objRef); } % %(IObjectReference objRef)% @@ -4870,6 +4881,7 @@ private % AsInternal(InterfaceTag<%> _) => _default; derived_new, type_name, type_name, + type_name, // ObjectReference constructor type.Flags().Sealed() ? "internal" : "protected internal", type_name, @@ -4943,10 +4955,10 @@ public static unsafe MarshalInterfaceHelper<%>.MarshalerArray CreateMarshalerArr public static (int length, IntPtr data) GetAbiArray(object box) => MarshalInterfaceHelper<%>.GetAbiArray(box); public static unsafe %[] FromAbiArray(object box) => MarshalInterfaceHelper<%>.FromAbiArray(box, FromAbi); public static (int length, IntPtr data) FromManagedArray(%[] array) => MarshalInterfaceHelper<%>.FromManagedArray(array, (o) => FromManaged(o)); -public static void DisposeMarshaler(IObjectReference value) => MarshalInspectable.DisposeMarshaler(value); +public static void DisposeMarshaler(IObjectReference value) => MarshalInspectable.DisposeMarshaler(value); public static void DisposeMarshalerArray(MarshalInterfaceHelper<%>.MarshalerArray array) => MarshalInterfaceHelper<%>.DisposeMarshalerArray(array); -public static void DisposeAbi(IntPtr abi) => MarshalInspectable.DisposeAbi(abi); -public static unsafe void DisposeAbiArray(object box) => MarshalInspectable.DisposeAbiArray(box); +public static void DisposeAbi(IntPtr abi) => MarshalInspectable.DisposeAbi(abi); +public static unsafe void DisposeAbiArray(object box) => MarshalInspectable.DisposeAbiArray(box); } )", abi_type_name, @@ -4960,8 +4972,7 @@ public static unsafe void DisposeAbiArray(object box) => MarshalInspectable obj is null ? null : MarshalInspectable<%>.CreateMarshaler(obj).As<%.Vftbl>();", - projected_type_name, + w.write("public static IObjectReference CreateMarshaler(% obj) => obj is null ? null : MarshalInspectable.CreateMarshaler(obj).As<%.Vftbl>();", projected_type_name, default_interface_abi_name); } @@ -5716,7 +5727,7 @@ internal class % : ComponentActivationFactory<%>% public static IntPtr Make() { -using var marshaler = MarshalInspectable<%>.CreateMarshaler(_factory).As(); +using var marshaler = MarshalInspectable.CreateMarshaler(_factory).As(); return marshaler.GetRef(); } @@ -5735,7 +5746,6 @@ type_name, bind(type), factory_type_name, factory_type_name, -factory_type_name, bind(type) ); } diff --git a/cswinrt/strings/WinRT.cs b/cswinrt/strings/WinRT.cs index e8c578c71..3cec017b4 100644 --- a/cswinrt/strings/WinRT.cs +++ b/cswinrt/strings/WinRT.cs @@ -298,7 +298,7 @@ public unsafe ObjectReference _ActivateInstance() } finally { - MarshalInspectable.DisposeAbi(instancePtr); + MarshalInspectable.DisposeAbi(instancePtr); } } @@ -320,7 +320,8 @@ public ActivationFactory() : base(typeof(T).Namespace, typeof(T).FullName) { } public IntPtr ActivateInstance() { T comp = new T(); - return MarshalInspectable.FromManaged(comp); + using var marshaler = MarshalInspectable.CreateMarshaler(comp); + return marshaler.GetRef(); } }