Skip to content

Commit

Permalink
Remove/move internal vftbl types (#1493)
Browse files Browse the repository at this point in the history
* Remove IActivationFactoryVftbl type

* Move IReferenceTrackerTargetVftbl to runtime

* Move or hide IDelegateVftbl type

* Delete WinRT_Interop file

* Actually delete IReferenceTrackerTargetVftbl

* Fix Invoke references in code writer

* Fix comment
  • Loading branch information
Sergio0694 authored Feb 2, 2024
1 parent 5b02525 commit 5861cf9
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 68 deletions.
10 changes: 5 additions & 5 deletions src/WinRT.Runtime/ActivationFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ private DllModule(string fileName, IntPtr moduleHandle, void* getActivationFacto
}
}

public (ObjectReference<IActivationFactoryVftbl> obj, int hr) GetActivationFactory(string runtimeClassId)
public (ObjectReference<IUnknownVftbl> obj, int hr) GetActivationFactory(string runtimeClassId)
{
IntPtr instancePtr = IntPtr.Zero;
try
Expand All @@ -110,7 +110,7 @@ private DllModule(string fileName, IntPtr moduleHandle, void* getActivationFacto
int hr = _GetActivationFactory(MarshalString.GetAbi(ref __runtimeClassId), &instancePtr);
if (hr == 0)
{
var objRef = ObjectReference<IActivationFactoryVftbl>.Attach(ref instancePtr);
var objRef = ObjectReference<IUnknownVftbl>.Attach(ref instancePtr);
return (objRef, hr);
}
else
Expand Down Expand Up @@ -205,8 +205,8 @@ public static IObjectReference Get(string typeName)
{
// Prefer the RoGetActivationFactory HRESULT failure over the LoadLibrary/etc. failure
int hr;
ObjectReference<IActivationFactoryVftbl> factory;
(factory, hr) = WinRTModule.GetActivationFactory<IActivationFactoryVftbl>(typeName, InterfaceIIDs.IActivationFactory_IID);
ObjectReference<IUnknownVftbl> factory;
(factory, hr) = WinRTModule.GetActivationFactory<IUnknownVftbl>(typeName, InterfaceIIDs.IActivationFactory_IID);
if (factory != null)
{
return factory;
Expand Down Expand Up @@ -271,7 +271,7 @@ public static ObjectReference<I> Get<I>(string typeName, Guid iid)
DllModule module = null;
if (DllModule.TryLoad(moduleName + ".dll", out module))
{
ObjectReference<IActivationFactoryVftbl> activationFactory;
ObjectReference<IUnknownVftbl> activationFactory;
(activationFactory, hr) = module.GetActivationFactory(typeName);
if (activationFactory != null)
{
Expand Down
21 changes: 21 additions & 0 deletions src/WinRT.Runtime/Interop/IDelegateVftbl.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System;

namespace WinRT.Interop
{
// This is internal both in the embedded case, but also if we're on modern .NET,
// because projections only use this vftbl type downlevel, and running older
// projections against a newer version of CsWinRT is not supported.
#if EMBED || NET
internal
#else
public
#endif
struct IDelegateVftbl
{
public IUnknownVftbl IUnknownVftbl;
public IntPtr Invoke;
}
}
4 changes: 2 additions & 2 deletions src/WinRT.Runtime/Marshalers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1668,10 +1668,10 @@ public static IObjectReference CreateMarshaler(object o, Guid delegateIID, bool

if (unwrapObject && ComWrappersSupport.TryUnwrapObject(o, out var objRef))
{
return objRef.As<IDelegateVftbl>(delegateIID);
return objRef.As<IUnknownVftbl>(delegateIID);
}

return ComWrappersSupport.CreateCCWForObject<IDelegateVftbl>(o, delegateIID);
return ComWrappersSupport.CreateCCWForObject<IUnknownVftbl>(o, delegateIID);
}

public static ObjectReferenceValue CreateMarshaler2(object o, Guid delegateIID, bool unwrapObject = true)
Expand Down
18 changes: 9 additions & 9 deletions src/cswinrt/code_writers.h
Original file line number Diff line number Diff line change
Expand Up @@ -8652,7 +8652,7 @@ return MarshalDelegate.FromAbi<%>(nativeDelegate);

public static % CreateRcw(IntPtr ptr)
{
return new %(new NativeDelegateWrapper(ComWrappersSupport.GetObjectReferenceForInterface<IDelegateVftbl>(ptr, GuidGenerator.GetIID(typeof(@%)))).Invoke);
return new %(new NativeDelegateWrapper(ComWrappersSupport.GetObjectReferenceForInterface<IUnknownVftbl>(ptr, GuidGenerator.GetIID(typeof(@%)))).Invoke);
}

[global::WinRT.ObjectReferenceWrapper(nameof(_nativeDelegate))]
Expand All @@ -8662,9 +8662,9 @@ private sealed class NativeDelegateWrapper
private sealed class NativeDelegateWrapper : IWinRTObject
#endif
{
private readonly ObjectReference<global::WinRT.Interop.IDelegateVftbl> _nativeDelegate;
private readonly ObjectReference<global::WinRT.Interop.IUnknownVftbl> _nativeDelegate;

public NativeDelegateWrapper(ObjectReference<global::WinRT.Interop.IDelegateVftbl> nativeDelegate)
public NativeDelegateWrapper(ObjectReference<global::WinRT.Interop.IUnknownVftbl> nativeDelegate)
{
_nativeDelegate = nativeDelegate;
}
Expand Down Expand Up @@ -8792,7 +8792,7 @@ AbiToProjectionVftable = new global::WinRT.Interop.IDelegateVftbl
IUnknownVftbl = global::WinRT.Interop.IUnknownVftbl.AbiToProjectionVftbl,
Invoke = Marshal.GetFunctionPointerForDelegate(AbiInvokeDelegate)
};
var nativeVftbl = ComWrappersSupport.AllocateVtableMemory(typeof(@%), Marshal.SizeOf<global::WinRT.Interop.IDelegateVftbl>());
var nativeVftbl = ComWrappersSupport.AllocateVtableMemory(typeof(@%), sizeof(IntPtr) * 4);
Marshal.StructureToPtr(AbiToProjectionVftable, nativeVftbl, false);
AbiToProjectionVftablePtr = nativeVftbl;
)",
Expand All @@ -8802,7 +8802,7 @@ AbiToProjectionVftablePtr = nativeVftbl;
else if (!is_generic)
{
w.write(R"(
AbiToProjectionVftablePtr = ComWrappersSupport.AllocateVtableMemory(typeof(@), sizeof(global::WinRT.Interop.IDelegateVftbl));
AbiToProjectionVftablePtr = ComWrappersSupport.AllocateVtableMemory(typeof(@), sizeof(IntPtr) * 4);
*(global::WinRT.Interop.IUnknownVftbl*)AbiToProjectionVftablePtr = global::WinRT.Interop.IUnknownVftbl.AbiToProjectionVftbl;
((delegate* unmanaged[Stdcall]<%, int>*)AbiToProjectionVftablePtr)[3] = &Do_Abi_Invoke;
)",
Expand All @@ -8817,7 +8817,7 @@ AbiToProjectionVftablePtr = ComWrappersSupport.AllocateVtableMemory(typeof(@), s
if (RuntimeFeature.IsDynamicCodeCompiled && %.AbiToProjectionVftablePtr == default)
{
AbiInvokeDelegate = %;
AbiToProjectionVftablePtr = ComWrappersSupport.AllocateVtableMemory(typeof(@%), sizeof(global::WinRT.Interop.IDelegateVftbl));
AbiToProjectionVftablePtr = ComWrappersSupport.AllocateVtableMemory(typeof(@%), sizeof(IntPtr) * 4);
*(global::WinRT.Interop.IUnknownVftbl*)AbiToProjectionVftablePtr = global::WinRT.Interop.IUnknownVftbl.AbiToProjectionVftbl;
((IntPtr*)AbiToProjectionVftablePtr)[3] = Marshal.GetFunctionPointerForDelegate(AbiInvokeDelegate);
}
Expand Down Expand Up @@ -8915,17 +8915,17 @@ else
{
if (have_generic_params)
{
w.write("var abiInvoke = Marshal.GetDelegateForFunctionPointer(_nativeDelegate.Vftbl.Invoke, Abi_Invoke_Type);");
w.write("var abiInvoke = Marshal.GetDelegateForFunctionPointer((IntPtr)(*(void***)_nativeDelegate.ThisPtr)[3], Abi_Invoke_Type);");
}
else
{
w.write("var abiInvoke = Marshal.GetDelegateForFunctionPointer<%>(_nativeDelegate.Vftbl.Invoke);",
w.write("var abiInvoke = Marshal.GetDelegateForFunctionPointer<%>((IntPtr)(*(void***)_nativeDelegate.ThisPtr)[3]);",
is_generic ? w.write_temp("@_Delegates.Invoke", type.TypeName()) : "Abi_Invoke");
}
}
else
{
w.write("var abiInvoke = (delegate* unmanaged[Stdcall]<%, int>)(_nativeDelegate.Vftbl.Invoke);",
w.write("var abiInvoke = (delegate* unmanaged[Stdcall]<%, int>)(*(void***)_nativeDelegate.ThisPtr)[3];",
bind<write_abi_parameter_types_pointer>(signature));
}
}),
Expand Down
1 change: 0 additions & 1 deletion src/cswinrt/cswinrt.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@
<None Include="strings\additions\Windows.UI\Windows.UI.cs" />
<None Include="strings\ComInteropHelpers.cs" />
<None Include="strings\WinRT.cs" />
<None Include="strings\WinRT_Interop.cs" />
</ItemGroup>
<ItemGroup>
<Midl Include="WinRT.Interop.idl" />
Expand Down
3 changes: 0 additions & 3 deletions src/cswinrt/cswinrt.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,6 @@
<Filter>strings</Filter>
</None>
<None Include="packages.config" />
<None Include="strings\WinRT_Interop.cs">
<Filter>strings</Filter>
</None>
<None Include="strings\additions\Windows.Foundation\Windows.Foundation.cs">
<Filter>strings\additions\Windows.Foundation</Filter>
</None>
Expand Down
15 changes: 11 additions & 4 deletions src/cswinrt/strings/WinRT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public void Dispose()
public void InitalizeReferenceTracking(IntPtr ptr)
{
eventInvokePtr = ptr;
int hr = Marshal.QueryInterface(ptr, ref Unsafe.AsRef(IReferenceTrackerTargetVftbl.IID), out referenceTrackerTargetPtr);
int hr = Marshal.QueryInterface(ptr, ref Unsafe.AsRef(in InterfaceIIDs.IReferenceTrackerTarget_IID), out referenceTrackerTargetPtr);
if (hr != 0)
{
referenceTrackerTargetPtr = default;
Expand Down Expand Up @@ -152,9 +152,14 @@ public unsafe bool HasComReferences()

if (referenceTrackerTargetPtr != default)
{
IReferenceTrackerTargetVftbl vftblReferenceTracker = **(IReferenceTrackerTargetVftbl**)referenceTrackerTargetPtr;
vftblReferenceTracker.AddRefFromReferenceTracker(referenceTrackerTargetPtr);
uint refTrackerCount = vftblReferenceTracker.ReleaseFromReferenceTracker(referenceTrackerTargetPtr);
void** vftblReferenceTracker = *(void***)referenceTrackerTargetPtr;

// AddRefFromReferenceTracker
_ = ((delegate* unmanaged[Stdcall]<IntPtr, uint>)(vftblReferenceTracker[3]))(referenceTrackerTargetPtr);

// ReleaseFromReferenceTracker
uint refTrackerCount = ((delegate* unmanaged[Stdcall]<IntPtr, uint>)(vftblReferenceTracker[4]))(referenceTrackerTargetPtr);

if (refTrackerCount != 0)
{
// Note we can't tell if the reference tracker ref is pegged or not, so this is best effort where if there
Expand Down Expand Up @@ -465,6 +470,7 @@ internal static class InterfaceIIDs
internal static readonly Guid IMarshal_IID = new Guid(new global::System.ReadOnlySpan<byte>(new byte[] { 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46 }));
internal static readonly Guid IContextCallback_IID = new Guid(new global::System.ReadOnlySpan<byte>(new byte[] { 0xDA, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46 }));
internal static readonly Guid ICallbackWithNoReentrancyToApplicationSTA_IID = new Guid(new global::System.ReadOnlySpan<byte>(new byte[] { 0x74, 0x97, 0x29, 0x0A, 0x4E, 0x3E, 0x42, 0xFC, 0x1D, 0x9D, 0x72, 0xCE, 0xE1, 0x05, 0xCA, 0x57 }));
internal static readonly Guid IReferenceTrackerTarget_IID = new Guid(new global::System.ReadOnlySpan<byte>(new byte[] { 0xF8, 0x43, 0xBD, 0x64, 0xEE, 0xBF, 0xC4, 0x4E, 0xB7, 0xEB, 0x29, 0x35, 0x15, 0x8D, 0xAE, 0x21 }));
#else
internal static readonly Guid IInspectable_IID = new(0xAF86E2E0, 0xB12D, 0x4c6a, 0x9C, 0x5A, 0xD7, 0xAA, 0x65, 0x10, 0x1E, 0x90);
internal static readonly Guid IUnknown_IID = new(0, 0, 0, 0xC0, 0, 0, 0, 0, 0, 0, 0x46);
Expand All @@ -475,6 +481,7 @@ internal static class InterfaceIIDs
internal static readonly Guid IMarshal_IID = new(0x00000003, 0, 0, 0xc0, 0, 0, 0, 0, 0, 0, 0x46);
internal static readonly Guid IContextCallback_IID = new(0x000001da, 0, 0, 0xC0, 0, 0, 0, 0, 0, 0, 0x46);
internal static readonly Guid ICallbackWithNoReentrancyToApplicationSTA_IID = new(0x0A299774, 0x3E4E, 0xFC42, 0x1D, 0x9D, 0x72, 0xCE, 0xE1, 0x05, 0xCA, 0x57);
internal static readonly Guid IReferenceTrackerTarget_IID = new(0x64BD43F8, 0xbFEE, 0x4EC4, 0xB7, 0xEB, 0x29, 0x35, 0x15, 0x8D, 0xAE, 0x21);
#endif
}
}
Expand Down
44 changes: 0 additions & 44 deletions src/cswinrt/strings/WinRT_Interop.cs

This file was deleted.

0 comments on commit 5861cf9

Please sign in to comment.