Skip to content

Commit 900a0e5

Browse files
authored
Move IActivationFactoryMethods.ActivateInstance into WinRT.Runtime (#1489)
* Move IActivationFactoryMethods to WinRT.Runtime * Update projections writer
1 parent 3ae6c79 commit 900a0e5

File tree

3 files changed

+32
-18
lines changed

3 files changed

+32
-18
lines changed

src/WinRT.Runtime/Interop/IActivationFactory.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
// Licensed under the MIT License.
33

44
using System;
5+
using System.ComponentModel;
56
using System.Runtime.CompilerServices;
67
using System.Runtime.InteropServices;
78
using WinRT;
9+
using WinRT.Interop;
810

911
namespace WinRT.Interop
1012
{
@@ -38,6 +40,35 @@ static class IActivationFactoryMethods
3840
#endif
3941

4042
public static IntPtr AbiToProjectionVftablePtr => IActivationFactory.Vftbl.AbiToProjectionVftablePtr;
43+
44+
/// <summary>
45+
/// Activates an instance from a given activation factory <see cref="IObjectReference"/> instance.
46+
/// </summary>
47+
/// <param name="objectReference">The input activation factory <see cref="IObjectReference"/> instance to use.</param>
48+
/// <returns>The resulting <see cref="IObjectReference"/> instance created from <paramref name="objectReference"/>.</returns>
49+
/// <remarks>
50+
/// <para>This method assumes <paramref name="objectReference"/> is wrapping an <c>IActivationFactory</c> instance (with no validation).</para>
51+
/// <para>This method is only meant to be used by the generated projections, and not by consumers of CsWinRT directly.</para>
52+
/// </remarks>
53+
[EditorBrowsable(EditorBrowsableState.Never)]
54+
public static unsafe IObjectReference ActivateInstanceUnsafe(IObjectReference objectReference)
55+
{
56+
IntPtr thisPtr = objectReference.ThisPtr;
57+
IntPtr instancePtr;
58+
59+
ExceptionHelpers.ThrowExceptionForHR((*(delegate* unmanaged[Stdcall]<IntPtr, IntPtr*, int>**)thisPtr)[6](thisPtr, &instancePtr));
60+
61+
GC.KeepAlive(objectReference);
62+
63+
try
64+
{
65+
return ComWrappersSupport.GetObjectReferenceForInterface<IUnknownVftbl>(instancePtr);
66+
}
67+
finally
68+
{
69+
MarshalInspectable<object>.DisposeAbi(instancePtr);
70+
}
71+
}
4172
}
4273

4374
[global::WinRT.ObjectReferenceWrapper(nameof(_obj))]

src/cswinrt/code_writers.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2138,7 +2138,7 @@ ComWrappersSupport.RegisterObjectForInterface(this, ThisPtr);
21382138
auto objrefname = w.write_temp("%", bind<write_objref_type_name>(class_type));
21392139

21402140
w.write(R"(
2141-
public %() : this(%(WinRT.IActivationFactoryMethods.ActivateInstance<IUnknownVftbl>(%)))
2141+
public %() : this(%(global::ABI.WinRT.Interop.IActivationFactoryMethods.ActivateInstanceUnsafe(%)))
21422142
{
21432143
ComWrappersSupport.RegisterObjectForInterface(this, ThisPtr);
21442144
%

src/cswinrt/strings/WinRT.cs

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,6 @@
1919

2020
namespace WinRT
2121
{
22-
internal static class IActivationFactoryMethods
23-
{
24-
public static unsafe ObjectReference<I> ActivateInstance<I>(IObjectReference obj)
25-
{
26-
IntPtr instancePtr;
27-
global::WinRT.ExceptionHelpers.ThrowExceptionForHR((*(delegate* unmanaged[Stdcall]<IntPtr, IntPtr*, int>**)obj.ThisPtr)[6](obj.ThisPtr, &instancePtr));
28-
try
29-
{
30-
return ComWrappersSupport.GetObjectReferenceForInterface<I>(instancePtr);
31-
}
32-
finally
33-
{
34-
MarshalInspectable<object>.DisposeAbi(instancePtr);
35-
}
36-
}
37-
}
38-
3922
internal class ComponentActivationFactory : global::WinRT.Interop.IActivationFactory
4023
{
4124
public IntPtr ActivateInstance()

0 commit comments

Comments
 (0)