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 2 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
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
14 changes: 11 additions & 3 deletions src/WinRT.Runtime/Interop/IActivationFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,22 @@ internal IActivationFactory(ObjectReference<Vftbl> obj)

public unsafe IntPtr ActivateInstance()
{
IntPtr __retval = default;
IntPtr __retval = default;
bool success = false;
try
{
global::WinRT.ExceptionHelpers.ThrowExceptionForHR(_obj.Vftbl.ActivateInstance_0(ThisPtr, &__retval));
{
_obj.DangerousAddRef(ref success);
var thisPtr = _obj.DangerousGetPtr();

global::WinRT.ExceptionHelpers.ThrowExceptionForHR(_obj.Vftbl.ActivateInstance_0(thisPtr, &__retval));
return __retval;
}
finally
{
if (success)
{
_obj.DangerousRelease();
}
}
}
}
Expand Down
Loading