diff --git a/src/WinRT.Runtime/MatchingRefApiCompatBaseline.net5.0.txt b/src/WinRT.Runtime/MatchingRefApiCompatBaseline.net5.0.txt index bd2818fed..347a8206a 100644 --- a/src/WinRT.Runtime/MatchingRefApiCompatBaseline.net5.0.txt +++ b/src/WinRT.Runtime/MatchingRefApiCompatBaseline.net5.0.txt @@ -5,4 +5,5 @@ MembersMustExist : Member 'public void WinRT.ComWrappersSupport.RegisterObjectFo MembersMustExist : Member 'protected void WinRT.IObjectReference.AddRef(System.Boolean)' does not exist in the reference but it does exist in the implementation. TypesMustExist : Type 'WinRT.Interop.IWeakReference' does not exist in the reference but it does exist in the implementation. TypesMustExist : Type 'WinRT.Interop.IWeakReferenceSource' does not exist in the reference but it does exist in the implementation. -Total Issues: 6 +MembersMustExist : Member 'public System.Int32 WinRT.IObjectReference.TryAs(System.Guid, System.IntPtr)' does not exist in the reference but it does exist in the implementation. +Total Issues: 7 diff --git a/src/WinRT.Runtime/ObjectReference.cs b/src/WinRT.Runtime/ObjectReference.cs index 63d5b43c3..005d6d9a6 100644 --- a/src/WinRT.Runtime/ObjectReference.cs +++ b/src/WinRT.Runtime/ObjectReference.cs @@ -168,6 +168,23 @@ public virtual unsafe int TryAs(Guid iid, out ObjectReference objRef) objRef.ReferenceTrackerPtr = ReferenceTrackerPtr; } return hr; + } + + // Used only as part of the GetInterface implementation where the + // result is an reference passed across the ABI and doesn't need to + // be tracked as an internal reference. This is separate to handle + // tear off aggregate scenario where releasing an reference can end up + // deleting the tear off interface. + public virtual unsafe int TryAs(Guid iid, out IntPtr ppv) + { + ppv = IntPtr.Zero; + ThrowIfDisposed(); + int hr = VftblIUnknown.QueryInterface(ThisPtr, ref iid, out IntPtr thatPtr); + if (hr >= 0) + { + ppv = thatPtr; + } + return hr; } public unsafe IObjectReference As(Guid iid) => As(iid); diff --git a/src/cswinrt/code_writers.h b/src/cswinrt/code_writers.h index 4e0cc6d5a..4c9f90cc0 100644 --- a/src/cswinrt/code_writers.h +++ b/src/cswinrt/code_writers.h @@ -5135,14 +5135,10 @@ if (IsOverridableInterface(iid) || typeof(global::WinRT.IInspectable).GUID == ii return global::System.Runtime.InteropServices.CustomQueryInterfaceResult.NotHandled; } -if (%.TryAs(iid, out ObjectReference objRef) >= 0) +if (%.TryAs(iid, out ppv) >= 0) { -using (objRef) -{ -ppv = objRef.GetRef(); return global::System.Runtime.InteropServices.CustomQueryInterfaceResult.Handled; } -} return global::System.Runtime.InteropServices.CustomQueryInterfaceResult.NotHandled; })",