-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
IVTable
interfaces to COM structs
Closes #831
- Loading branch information
Showing
13 changed files
with
207 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/// <summary> | ||
/// Non generic interface that allows constraining against a COM wrapper type directly. COM structs should | ||
/// implement <see cref="IVTable{TComInterface, TVTable}"/>. | ||
/// </summary> | ||
internal unsafe interface IVTable | ||
{ | ||
static abstract System.Com.IUnknown.Vtbl* VTable { get; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
internal unsafe interface IVTable<TComInterface, TVTable> : IVTable | ||
where TVTable : unmanaged | ||
where TComInterface : unmanaged, IVTable<TComInterface, TVTable> | ||
{ | ||
private protected static abstract void PopulateVTable(TVTable* vtable); | ||
|
||
static System.Com.IUnknown.Vtbl* IVTable.VTable { get; } = (System.Com.IUnknown.Vtbl*)CreateVTable(); | ||
|
||
private static TVTable* CreateVTable() | ||
{ | ||
TVTable* vtbl = (TVTable*)RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(TVTable), sizeof(TVTable)); | ||
ComHelpers.PopulateIUnknown<TComInterface>((System.Com.IUnknown.Vtbl*)vtbl); | ||
TComInterface.PopulateVTable(vtbl); | ||
return vtbl; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
#if NET7_0_OR_GREATER | ||
|
||
using System.Runtime.InteropServices; | ||
using Windows.Win32.Foundation; | ||
using Windows.Win32.System.Com; | ||
|
||
namespace Windows.Win32; | ||
|
||
// The `unsafe` modifier is only allowed to appear on the class declaration -- not the partial method declaration. | ||
// See https://github.com/dotnet/csharplang/discussions/7298 for more. | ||
internal unsafe partial class ComHelpers | ||
{ | ||
static partial void PopulateIUnknownImpl<TComInterface>(IUnknown.Vtbl* vtable) | ||
where TComInterface : unmanaged | ||
{ | ||
// IUnknown member initialization of the v-table would go here. | ||
vtable->QueryInterface_1 = TestComWrappers.ComWrappersForIUnknown.QueryInterface_1; | ||
vtable->AddRef_2 = TestComWrappers.ComWrappersForIUnknown.AddRef_2; | ||
vtable->Release_3 = TestComWrappers.ComWrappersForIUnknown.Release_3; | ||
} | ||
|
||
private unsafe class TestComWrappers : ComWrappers | ||
{ | ||
internal static readonly IUnknown.Vtbl ComWrappersForIUnknown = GetComWrappersUnknown(); | ||
|
||
// Abstracts that need implementation | ||
protected override unsafe ComInterfaceEntry* ComputeVtables(object obj, CreateComInterfaceFlags flags, out int count) | ||
{ | ||
count = 0; | ||
return null; | ||
} | ||
|
||
protected override object? CreateObject(nint externalComObject, CreateObjectFlags flags) => null; | ||
|
||
protected override void ReleaseObjects(global::System.Collections.IEnumerable objects) => throw new NotImplementedException(); | ||
|
||
private static IUnknown.Vtbl GetComWrappersUnknown() | ||
{ | ||
GetIUnknownImpl(out IntPtr fpQueryInterface, out IntPtr fpAddRef, out IntPtr fpRelease); | ||
return new IUnknown.Vtbl() | ||
{ | ||
QueryInterface_1 = (delegate* unmanaged[Stdcall]<IUnknown*, Guid*, void**, HRESULT>)fpQueryInterface, | ||
AddRef_2 = (delegate* unmanaged[Stdcall]<IUnknown*, uint>)fpAddRef, | ||
Release_3 = (delegate* unmanaged[Stdcall]<IUnknown*, uint>)fpRelease, | ||
}; | ||
} | ||
} | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.