Skip to content

Move IActivationFactoryMethods.ActivateInstance into WinRT.Runtime #1489

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

Merged
Merged
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
31 changes: 31 additions & 0 deletions src/WinRT.Runtime/Interop/IActivationFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
// Licensed under the MIT License.

using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using WinRT;
using WinRT.Interop;

namespace WinRT.Interop
{
Expand Down Expand Up @@ -38,6 +40,35 @@ static class IActivationFactoryMethods
#endif

public static IntPtr AbiToProjectionVftablePtr => IActivationFactory.Vftbl.AbiToProjectionVftablePtr;

/// <summary>
/// Activates an instance from a given activation factory <see cref="IObjectReference"/> instance.
/// </summary>
/// <param name="objectReference">The input activation factory <see cref="IObjectReference"/> instance to use.</param>
/// <returns>The resulting <see cref="IObjectReference"/> instance created from <paramref name="objectReference"/>.</returns>
/// <remarks>
/// <para>This method assumes <paramref name="objectReference"/> is wrapping an <c>IActivationFactory</c> instance (with no validation).</para>
/// <para>This method is only meant to be used by the generated projections, and not by consumers of CsWinRT directly.</para>
/// </remarks>
[EditorBrowsable(EditorBrowsableState.Never)]
public static unsafe IObjectReference ActivateInstanceUnsafe(IObjectReference objectReference)
{
IntPtr thisPtr = objectReference.ThisPtr;
IntPtr instancePtr;

ExceptionHelpers.ThrowExceptionForHR((*(delegate* unmanaged[Stdcall]<IntPtr, IntPtr*, int>**)thisPtr)[6](thisPtr, &instancePtr));

GC.KeepAlive(objectReference);

try
{
return ComWrappersSupport.GetObjectReferenceForInterface<IUnknownVftbl>(instancePtr);
}
finally
{
MarshalInspectable<object>.DisposeAbi(instancePtr);
}
}
}

[global::WinRT.ObjectReferenceWrapper(nameof(_obj))]
Expand Down
2 changes: 1 addition & 1 deletion src/cswinrt/code_writers.h
Original file line number Diff line number Diff line change
Expand Up @@ -2138,7 +2138,7 @@ ComWrappersSupport.RegisterObjectForInterface(this, ThisPtr);
auto objrefname = w.write_temp("%", bind<write_objref_type_name>(class_type));

w.write(R"(
public %() : this(%(WinRT.IActivationFactoryMethods.ActivateInstance<IUnknownVftbl>(%)))
public %() : this(%(global::ABI.WinRT.Interop.IActivationFactoryMethods.ActivateInstanceUnsafe(%)))
{
ComWrappersSupport.RegisterObjectForInterface(this, ThisPtr);
%
Expand Down
17 changes: 0 additions & 17 deletions src/cswinrt/strings/WinRT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,6 @@

namespace WinRT
{
internal static class IActivationFactoryMethods
{
public static unsafe ObjectReference<I> ActivateInstance<I>(IObjectReference obj)
{
IntPtr instancePtr;
global::WinRT.ExceptionHelpers.ThrowExceptionForHR((*(delegate* unmanaged[Stdcall]<IntPtr, IntPtr*, int>**)obj.ThisPtr)[6](obj.ThisPtr, &instancePtr));
try
{
return ComWrappersSupport.GetObjectReferenceForInterface<I>(instancePtr);
}
finally
{
MarshalInspectable<object>.DisposeAbi(instancePtr);
}
}
}

internal class ComponentActivationFactory : global::WinRT.Interop.IActivationFactory
{
public IntPtr ActivateInstance()
Expand Down