Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new api's to IObjectReference to safely use the underlying pointer. #1474

Draft
wants to merge 6 commits into
base: staging/AOT
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/WinRT.Runtime/ApiCompatBaseline.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ TypesMustExist : Type 'ABI.System.ComponentModel.INotifyDataErrorInfo' does not
CannotRemoveAttribute : Attribute 'System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute' exists on 'System.Type WinRT.Marshaler<T>.AbiType' in the contract but not the implementation.
CannotMakeMemberNonVirtual : Member 'public System.Int32 WinRT.IObjectReference.TryAs<T>(System.Guid, WinRT.ObjectReference<T>)' is non-virtual in the implementation but is virtual in the contract.
MembersMustExist : Member 'protected System.Boolean System.Boolean WinRT.IObjectReference.disposed' does not exist in the implementation but it does exist in the contract.
MembersMustExist : Member 'protected void WinRT.IObjectReference.Dispose(System.Boolean)' does not exist in the implementation but it does exist in the contract.
CannotChangeAttribute : Attribute 'System.Diagnostics.CodeAnalysis.UnconditionalSuppressMessageAttribute' on 'ABI.System.Type.FromAbi(ABI.System.Type)' changed from '[UnconditionalSuppressMessageAttribute("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode", Justification="Any types which are trimmed are not used by managed user code and there is fallback logic to handle that.")]' in the contract to '[UnconditionalSuppressMessageAttribute("ReflectionAnalysis", "IL2057", Justification="Any types which are trimmed are not used by managed user code and there is fallback logic to handle that.")]' in the implementation.
Total Issues: 16
MembersMustExist : Member 'protected void WinRT.IObjectReference.ThrowIfDisposed()' does not exist in the implementation but it does exist in the contract.
MembersMustExist : Member 'protected WinRT.Interop.IUnknownVftbl WinRT.IObjectReference.VftblIUnknown.get()' does not exist in the implementation but it does exist in the contract.
Total Issues: 17
2 changes: 1 addition & 1 deletion src/WinRT.Runtime/CastExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static TInterface As<TInterface>(this object value)
{
using (objRef)
{
return ComWrappersSupport.CreateRcwForComObject<TInterface>(objRef.ThisPtr);
return ComWrappersSupport.CreateRcwForComObject<TInterface>(objRef.DangerousGetPtr());
}
}
}
Expand Down
18 changes: 14 additions & 4 deletions src/WinRT.Runtime/ComWrappersSupport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,20 @@ internal unsafe static bool IsFreeThreaded(IntPtr iUnknown)

internal unsafe static bool IsFreeThreaded(IObjectReference objRef)
{
var isFreeThreaded = IsFreeThreaded(objRef.ThisPtr);
// ThisPtr is owned by objRef, so need to make sure objRef stays alive.
GC.KeepAlive(objRef);
return isFreeThreaded;
bool success = false;
try
{
objRef.DangerousAddRef(ref success);
var thisPtr = objRef.DangerousGetPtr();
return IsFreeThreaded(thisPtr);
}
finally
{
if (success)
{
objRef.DangerousRelease();
}
}
}

public static IObjectReference GetObjectReferenceForInterface(IntPtr externalComObject)
Expand Down
2 changes: 1 addition & 1 deletion src/WinRT.Runtime/ComWrappersSupport.net5.cs
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ public unsafe static void Init(
// otherwise the new instance will be used. Since the inner was composed
// it should answer immediately without going through the outer. Either way
// the reference count will go to the new instance.
int hr = Marshal.QueryInterface(objRef.ThisPtr, ref Unsafe.AsRef(IReferenceTrackerVftbl.IID), out referenceTracker);
int hr = Marshal.QueryInterface(objRef.DangerousGetPtr(), ref Unsafe.AsRef(IReferenceTrackerVftbl.IID), out referenceTracker);
if (hr != 0)
{
referenceTracker = default;
Expand Down
4 changes: 2 additions & 2 deletions src/WinRT.Runtime/ComWrappersSupport.netstandard2.0.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,15 @@ private static T CreateRcwForComObject<T>(IntPtr ptr, bool tryUseCache)

keepAliveSentinel = runtimeWrapper; // We don't take a strong reference on runtimeWrapper at any point, so we need to make sure it lives until it can get assigned to rcw.
var runtimeWrapperReference = new System.WeakReference<object>(runtimeWrapper);
var cleanupSentinel = new RuntimeWrapperCleanup(identity.ThisPtr, runtimeWrapperReference);
var cleanupSentinel = new RuntimeWrapperCleanup(identity.DangerousGetPtr(), runtimeWrapperReference);
return runtimeWrapperReference;
};

object rcw;
if (tryUseCache)
{
RuntimeWrapperCache.AddOrUpdate(
identity.ThisPtr,
identity.DangerousGetPtr(),
rcwFactory,
(ptr, oldValue) =>
{
Expand Down
2 changes: 1 addition & 1 deletion src/WinRT.Runtime/ExceptionHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ public static unsafe void SetErrorInfo(Exception ex)
{
// If the exception has information for an IRestrictedErrorInfo, use that
// as our error so as to propagate the error through WinRT end-to-end.
if (ex.TryGetRestrictedLanguageErrorObject(out var restrictedErrorObject))
if (ex.TryGetRestrictedLanguageErrorObject(out IObjectReference restrictedErrorObject))
{
using (restrictedErrorObject)
{
Expand Down
9 changes: 8 additions & 1 deletion src/WinRT.Runtime/IInspectable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,12 @@ public IInspectable(ObjectReference<Vftbl> obj)
public unsafe string GetRuntimeClassName(bool noThrow = false)
{
IntPtr __retval = default;
bool success = false;
try
{
var hr = _obj.Vftbl.GetRuntimeClassName(ThisPtr, &__retval);
_obj.DangerousAddRef(ref success);
var thisPtr = _obj.DangerousGetPtr();
var hr = _obj.Vftbl.GetRuntimeClassName(thisPtr, &__retval);
if (hr != 0)
{
if (noThrow)
Expand All @@ -151,6 +154,10 @@ public unsafe string GetRuntimeClassName(bool noThrow = false)
finally
{
Platform.WindowsDeleteString(__retval);
if (success)
{
_obj.DangerousRelease();
}
}
}
}
Expand Down
166 changes: 122 additions & 44 deletions src/WinRT.Runtime/Interop/ExceptionErrorInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,8 @@ private static int Do_Abi_GetHelpFileContent_4(IntPtr thisPtr, IntPtr* helpFileC
public static implicit operator IErrorInfo(IObjectReference obj) => (obj != null) ? new IErrorInfo(obj) : null;
public static implicit operator IErrorInfo(ObjectReference<Vftbl> obj) => (obj != null) ? new IErrorInfo(obj) : null;
protected readonly ObjectReference<Vftbl> _obj;
public IObjectReference ObjRef { get => _obj; }
[Obsolete]
public IntPtr ThisPtr => _obj.ThisPtr;
public ObjectReference<I> AsInterface<I>() => _obj.As<I>();
public A As<A>() => _obj.AsType<A>();
Expand All @@ -275,63 +277,104 @@ public IErrorInfo(ObjectReference<Vftbl> obj)
public Guid GetGuid()
{
Guid __return_value__;
Marshal.ThrowExceptionForHR(_obj.Vftbl.GetGuid_0(ThisPtr, &__return_value__));
return __return_value__;
bool success = false;
try
{
_obj.DangerousAddRef(ref success);
var thisPtr = _obj.DangerousGetPtr();
Marshal.ThrowExceptionForHR(_obj.Vftbl.GetGuid_0(thisPtr, &__return_value__));
return __return_value__;
}
finally
{
if (success)
{
_obj.DangerousRelease();
}
}
}

public string GetSource()
{
IntPtr __retval = default;
try
{
Marshal.ThrowExceptionForHR(_obj.Vftbl.GetSource_1(ThisPtr, &__retval));
return __retval != IntPtr.Zero ? Marshal.PtrToStringBSTR(__retval) : string.Empty;
bool success = false;
try
{
_obj.DangerousAddRef(ref success);
var thisPtr = _obj.DangerousGetPtr();
Marshal.ThrowExceptionForHR(_obj.Vftbl.GetSource_1(thisPtr, &__retval));
return __retval != IntPtr.Zero ? Marshal.PtrToStringBSTR(__retval) : string.Empty;
}
finally
finally
{
Marshal.FreeBSTR(__retval);
Marshal.FreeBSTR(__retval);
if (success)
{
_obj.DangerousRelease();
}
}
}

public string GetDescription()
{
IntPtr __retval = default;
try
{
Marshal.ThrowExceptionForHR(_obj.Vftbl.GetDescription_2(ThisPtr, &__retval));
return __retval != IntPtr.Zero ? Marshal.PtrToStringBSTR(__retval) : string.Empty;
bool success = false;
try
{
_obj.DangerousAddRef(ref success);
var thisPtr = _obj.DangerousGetPtr();
Marshal.ThrowExceptionForHR(_obj.Vftbl.GetDescription_2(thisPtr, &__retval));
return __retval != IntPtr.Zero ? Marshal.PtrToStringBSTR(__retval) : string.Empty;
}
finally
finally
{
Marshal.FreeBSTR(__retval);
Marshal.FreeBSTR(__retval);
if (success)
{
_obj.DangerousRelease();
}
}
}

public string GetHelpFile()
{
IntPtr __retval = default;
try
{
Marshal.ThrowExceptionForHR(_obj.Vftbl.GetHelpFile_3(ThisPtr, &__retval));
return __retval != IntPtr.Zero ? Marshal.PtrToStringBSTR(__retval) : string.Empty;
bool success = false;
try
{
_obj.DangerousAddRef(ref success);
var thisPtr = _obj.DangerousGetPtr();
Marshal.ThrowExceptionForHR(_obj.Vftbl.GetHelpFile_3(thisPtr, &__retval));
return __retval != IntPtr.Zero ? Marshal.PtrToStringBSTR(__retval) : string.Empty;
}
finally
finally
{
Marshal.FreeBSTR(__retval);
Marshal.FreeBSTR(__retval);
if (success)
{
_obj.DangerousRelease();
}
}
}

public string GetHelpFileContent()
{
{
IntPtr __retval = default;
try
{
Marshal.ThrowExceptionForHR(_obj.Vftbl.GetHelpFileContent_4(ThisPtr, &__retval));
return __retval != IntPtr.Zero ? Marshal.PtrToStringBSTR(__retval) : string.Empty;
bool success = false;
try
{
_obj.DangerousAddRef(ref success);
var thisPtr = _obj.DangerousGetPtr();
Marshal.ThrowExceptionForHR(_obj.Vftbl.GetHelpFileContent_4(thisPtr, &__retval));
return __retval != IntPtr.Zero ? Marshal.PtrToStringBSTR(__retval) : string.Empty;
}
finally
finally
{
Marshal.FreeBSTR(__retval);
Marshal.FreeBSTR(__retval);
if (success)
{
_obj.DangerousRelease();
}
}
}
}
Expand All @@ -352,7 +395,7 @@ public struct Vftbl
public static implicit operator ILanguageExceptionErrorInfo(IObjectReference obj) => (obj != null) ? new ILanguageExceptionErrorInfo(obj) : null;
public static implicit operator ILanguageExceptionErrorInfo(ObjectReference<Vftbl> obj) => (obj != null) ? new ILanguageExceptionErrorInfo(obj) : null;
protected readonly ObjectReference<Vftbl> _obj;
public IntPtr ThisPtr => _obj.ThisPtr;
public IntPtr ThisPtr => _obj.ThisPtr;
public ObjectReference<I> AsInterface<I>() => _obj.As<I>();
public A As<A>() => _obj.AsType<A>();
public ILanguageExceptionErrorInfo(IObjectReference obj) : this(obj.As<Vftbl>()) { }
Expand All @@ -364,17 +407,20 @@ public ILanguageExceptionErrorInfo(ObjectReference<Vftbl> obj)
public IObjectReference GetLanguageException()
{
IntPtr __return_value__ = IntPtr.Zero;

bool success = false;
try
{
Marshal.ThrowExceptionForHR(_obj.Vftbl.GetLanguageException_0(ThisPtr, &__return_value__));
_obj.DangerousAddRef(ref success);
var thisPtr = _obj.DangerousGetPtr();
Marshal.ThrowExceptionForHR(_obj.Vftbl.GetLanguageException_0(thisPtr, &__return_value__));
return ObjectReference<global::WinRT.Interop.IUnknownVftbl>.Attach(ref __return_value__);
}
finally
{
if (__return_value__ != IntPtr.Zero)
{
(*(global::WinRT.Interop.IUnknownVftbl**)__return_value__)->Release(__return_value__);
MarshalInspectable<object>.DisposeAbi(__return_value__);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why MarshalInspectable<object> rather than just the Release call like before? It's more efficient.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is pretty much how this is done everywhere else. I can revert but consistency is nice.

if (success)
{
_obj.DangerousRelease();
}
}
}
Expand Down Expand Up @@ -435,6 +481,8 @@ private static int Do_Abi_InterfaceSupportsErrorInfo_0(IntPtr thisPtr, Guid* gui
public static implicit operator ISupportErrorInfo(IObjectReference obj) => (obj != null) ? new ISupportErrorInfo(obj) : null;
public static implicit operator ISupportErrorInfo(ObjectReference<Vftbl> obj) => (obj != null) ? new ISupportErrorInfo(obj) : null;
protected readonly ObjectReference<Vftbl> _obj;
public IObjectReference ObjRef { get => _obj; }
[Obsolete]
public IntPtr ThisPtr => _obj.ThisPtr;
public ObjectReference<I> AsInterface<I>() => _obj.As<I>();
public A As<A>() => _obj.AsType<A>();
Expand All @@ -445,8 +493,21 @@ public ISupportErrorInfo(ObjectReference<Vftbl> obj)
}

public bool InterfaceSupportsErrorInfo(Guid riid)
{
return _obj.Vftbl.InterfaceSupportsErrorInfo_0(ThisPtr, &riid) == 0;
{
bool success = false;
try
{
_obj.DangerousAddRef(ref success);
var thisPtr = _obj.DangerousGetPtr();
return _obj.Vftbl.InterfaceSupportsErrorInfo_0(thisPtr, &riid) == 0;
}
finally
{
if (success)
{
_obj.DangerousRelease();
}
}
}
}

Expand All @@ -468,7 +529,6 @@ public struct Vftbl
public static implicit operator IRestrictedErrorInfo(IObjectReference obj) => (obj != null) ? new IRestrictedErrorInfo(obj) : null;
public static implicit operator IRestrictedErrorInfo(ObjectReference<Vftbl> obj) => (obj != null) ? new IRestrictedErrorInfo(obj) : null;
protected readonly ObjectReference<Vftbl> _obj;
public IntPtr ThisPtr => _obj.ThisPtr;
public ObjectReference<I> AsInterface<I>() => _obj.As<I>();
public A As<A>() => _obj.AsType<A>();
public IRestrictedErrorInfo(IObjectReference obj) : this(obj.As<Vftbl>()) { }
Expand All @@ -485,12 +545,16 @@ public void GetErrorDetails(
{
IntPtr _description = IntPtr.Zero;
IntPtr _restrictedDescription = IntPtr.Zero;
IntPtr _capabilitySid = IntPtr.Zero;
IntPtr _capabilitySid = IntPtr.Zero;
bool success = false;
try
{
{
_obj.DangerousAddRef(ref success);
var thisPtr = _obj.DangerousGetPtr();

fixed (int* pError = &error)
{
Marshal.ThrowExceptionForHR(_obj.Vftbl.GetErrorDetails_0(ThisPtr, &_description, pError, &_restrictedDescription, &_capabilitySid));
Marshal.ThrowExceptionForHR(_obj.Vftbl.GetErrorDetails_0(thisPtr, &_description, pError, &_restrictedDescription, &_capabilitySid));
}
description = _description != IntPtr.Zero ? Marshal.PtrToStringBSTR(_description) : string.Empty;
restrictedDescription = _restrictedDescription != IntPtr.Zero ? Marshal.PtrToStringBSTR(_restrictedDescription) : string.Empty;
Expand All @@ -500,21 +564,35 @@ public void GetErrorDetails(
{
Marshal.FreeBSTR(_description);
Marshal.FreeBSTR(_restrictedDescription);
Marshal.FreeBSTR(_capabilitySid);
Marshal.FreeBSTR(_capabilitySid);

if (success)
{
_obj.DangerousRelease();
}
}
}

public string GetReference()
{
IntPtr __retval = default;
IntPtr __retval = default;
bool success = false;
try
{
Marshal.ThrowExceptionForHR(_obj.Vftbl.GetReference_1(ThisPtr, &__retval));
{
_obj.DangerousAddRef(ref success);
var thisPtr = _obj.DangerousGetPtr();

Marshal.ThrowExceptionForHR(_obj.Vftbl.GetReference_1(thisPtr, &__retval));
return __retval != IntPtr.Zero ? Marshal.PtrToStringBSTR(__retval) : string.Empty;
}
finally
{
Marshal.FreeBSTR(__retval);
Marshal.FreeBSTR(__retval);

if (success)
{
_obj.DangerousRelease();
}
}
}
}
Expand Down
Loading