Skip to content

Commit

Permalink
Move COM source generator APIs to System.Runtime.InteropServices.dll (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
jkoritzinsky authored Apr 13, 2023
1 parent 18e9c66 commit b9df364
Show file tree
Hide file tree
Showing 26 changed files with 559 additions and 117 deletions.
1 change: 1 addition & 0 deletions src/libraries/NetCoreAppLibrary.props
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@
</NetCoreAppLibraryNoReference>
<!-- List .NETCoreApp shared framework generator project names below. -->
<NetCoreAppLibraryGenerator>
ComInterfaceGenerator;
LibraryImportGenerator;
JSImportGenerator;
Microsoft.Interop.SourceGeneration;
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,144 @@ public enum TYMED
TYMED_ENHMF = 64,
}
}
namespace System.Runtime.InteropServices.Marshalling
{
[System.AttributeUsageAttribute(System.AttributeTargets.Class, Inherited = false)]
[System.CLSCompliantAttribute(false)]
public sealed partial class ComExposedClassAttribute<T> : System.Attribute, System.Runtime.InteropServices.Marshalling.IComExposedDetails where T : System.Runtime.InteropServices.Marshalling.IComExposedClass
{
public ComExposedClassAttribute() { }
public unsafe System.Runtime.InteropServices.ComWrappers.ComInterfaceEntry* GetComInterfaceEntries(out int count) { throw null; }
}
public sealed partial class ComObject : System.Runtime.InteropServices.IDynamicInterfaceCastable, System.Runtime.InteropServices.Marshalling.IUnmanagedVirtualMethodTableProvider
{
internal ComObject() { }
public void FinalRelease() { }
~ComObject() { }
System.RuntimeTypeHandle System.Runtime.InteropServices.IDynamicInterfaceCastable.GetInterfaceImplementation(System.RuntimeTypeHandle interfaceType) { throw null; }
bool System.Runtime.InteropServices.IDynamicInterfaceCastable.IsInterfaceImplemented(System.RuntimeTypeHandle interfaceType, bool throwIfNotImplemented) { throw null; }
System.Runtime.InteropServices.Marshalling.VirtualMethodTableInfo System.Runtime.InteropServices.Marshalling.IUnmanagedVirtualMethodTableProvider.GetVirtualMethodTableInfoForKey(System.Type type) { throw null; }
}
[System.Runtime.InteropServices.Marshalling.CustomMarshallerAttribute(typeof(System.Exception), System.Runtime.InteropServices.Marshalling.MarshalMode.UnmanagedToManagedOut, typeof(System.Runtime.InteropServices.Marshalling.ExceptionAsDefaultMarshaller<>))]
public static partial class ExceptionAsDefaultMarshaller<T> where T : struct
{
public static T ConvertToUnmanaged(System.Exception e) { throw null; }
}
[System.Runtime.InteropServices.Marshalling.CustomMarshallerAttribute(typeof(System.Exception), System.Runtime.InteropServices.Marshalling.MarshalMode.UnmanagedToManagedOut, typeof(System.Runtime.InteropServices.Marshalling.ExceptionAsHResultMarshaller<>))]
public static partial class ExceptionAsHResultMarshaller<T> where T : struct
{
public static T ConvertToUnmanaged(System.Exception e) { throw null; }
}
[System.Runtime.InteropServices.Marshalling.CustomMarshallerAttribute(typeof(System.Exception), System.Runtime.InteropServices.Marshalling.MarshalMode.UnmanagedToManagedOut, typeof(System.Runtime.InteropServices.Marshalling.ExceptionAsNaNMarshaller<>))]
public static partial class ExceptionAsNaNMarshaller<T> where T : struct
{
public static T ConvertToUnmanaged(System.Exception e) { throw null; }
}
[System.Runtime.InteropServices.Marshalling.CustomMarshallerAttribute(typeof(System.Exception), System.Runtime.InteropServices.Marshalling.MarshalMode.UnmanagedToManagedOut, typeof(System.Runtime.InteropServices.Marshalling.ExceptionAsVoidMarshaller))]
public static partial class ExceptionAsVoidMarshaller
{
public static void ConvertToUnmanaged(System.Exception e) { }
}
[System.AttributeUsageAttribute(System.AttributeTargets.Class)]
public sealed partial class GeneratedComClassAttribute : System.Attribute
{
public GeneratedComClassAttribute() { }
}
[System.AttributeUsageAttribute(System.AttributeTargets.Interface)]
public partial class GeneratedComInterfaceAttribute : System.Attribute
{
public GeneratedComInterfaceAttribute() { }
}
[System.CLSCompliantAttribute(false)]
public partial interface IComExposedClass
{
unsafe static abstract System.Runtime.InteropServices.ComWrappers.ComInterfaceEntry* GetComInterfaceEntries(out int count);
}
[System.CLSCompliantAttribute(false)]
public partial interface IComExposedDetails
{
unsafe System.Runtime.InteropServices.ComWrappers.ComInterfaceEntry* GetComInterfaceEntries(out int count);
}
[System.CLSCompliantAttribute(false)]
public partial interface IIUnknownCacheStrategy
{
void Clear(System.Runtime.InteropServices.Marshalling.IIUnknownStrategy unknownStrategy);
unsafe System.Runtime.InteropServices.Marshalling.IIUnknownCacheStrategy.TableInfo ConstructTableInfo(System.RuntimeTypeHandle handle, System.Runtime.InteropServices.Marshalling.IIUnknownDerivedDetails interfaceDetails, void* ptr);
bool TryGetTableInfo(System.RuntimeTypeHandle handle, out System.Runtime.InteropServices.Marshalling.IIUnknownCacheStrategy.TableInfo info);
bool TrySetTableInfo(System.RuntimeTypeHandle handle, System.Runtime.InteropServices.Marshalling.IIUnknownCacheStrategy.TableInfo info);
public readonly partial struct TableInfo
{
private readonly object _dummy;
private readonly int _dummyPrimitive;
public System.RuntimeTypeHandle ManagedType { get { throw null; } init { } }
public unsafe void** Table { get { throw null; } init { } }
public unsafe void* ThisPtr { get { throw null; } init { } }
}
}
[System.CLSCompliantAttribute(false)]
public partial interface IIUnknownDerivedDetails
{
System.Guid Iid { get; }
System.Type Implementation { get; }
unsafe void** ManagedVirtualMethodTable { get; }
}
[System.CLSCompliantAttribute(false)]
public partial interface IIUnknownInterfaceDetailsStrategy
{
System.Runtime.InteropServices.Marshalling.IComExposedDetails? GetComExposedTypeDetails(System.RuntimeTypeHandle type);
System.Runtime.InteropServices.Marshalling.IIUnknownDerivedDetails? GetIUnknownDerivedDetails(System.RuntimeTypeHandle type);
}
[System.CLSCompliantAttribute(false)]
public partial interface IIUnknownInterfaceType
{
static abstract System.Guid Iid { get; }
unsafe static abstract void** ManagedVirtualMethodTable { get; }
}
[System.CLSCompliantAttribute(false)]
public partial interface IIUnknownStrategy
{
unsafe void* CreateInstancePointer(void* unknown);
unsafe int QueryInterface(void* instancePtr, in System.Guid iid, out void* ppObj);
unsafe int Release(void* instancePtr);
}
[System.AttributeUsageAttribute(System.AttributeTargets.Interface, Inherited = false)]
[System.CLSCompliantAttribute(false)]
public partial class IUnknownDerivedAttribute<T, TImpl> : System.Attribute, System.Runtime.InteropServices.Marshalling.IIUnknownDerivedDetails where T : System.Runtime.InteropServices.Marshalling.IIUnknownInterfaceType
{
public IUnknownDerivedAttribute() { }
public System.Guid Iid { get { throw null; } }
public System.Type Implementation { get { throw null; } }
public unsafe void** ManagedVirtualMethodTable { get { throw null; } }
}
[System.CLSCompliantAttribute(false)]
public partial interface IUnmanagedVirtualMethodTableProvider
{
System.Runtime.InteropServices.Marshalling.VirtualMethodTableInfo GetVirtualMethodTableInfoForKey(System.Type type);
}
[System.CLSCompliantAttribute(false)]
public partial class StrategyBasedComWrappers : System.Runtime.InteropServices.ComWrappers
{
public StrategyBasedComWrappers() { }
public static System.Runtime.InteropServices.Marshalling.IIUnknownInterfaceDetailsStrategy DefaultIUnknownInterfaceDetailsStrategy { get { throw null; } }
public static System.Runtime.InteropServices.Marshalling.IIUnknownStrategy DefaultIUnknownStrategy { get { throw null; } }
protected unsafe sealed override System.Runtime.InteropServices.ComWrappers.ComInterfaceEntry* ComputeVtables(object obj, System.Runtime.InteropServices.CreateComInterfaceFlags flags, out int count) { throw null; }
protected virtual System.Runtime.InteropServices.Marshalling.IIUnknownCacheStrategy CreateCacheStrategy() { throw null; }
protected static System.Runtime.InteropServices.Marshalling.IIUnknownCacheStrategy CreateDefaultCacheStrategy() { throw null; }
protected sealed override object CreateObject(nint externalComObject, System.Runtime.InteropServices.CreateObjectFlags flags) { throw null; }
protected virtual System.Runtime.InteropServices.Marshalling.IIUnknownInterfaceDetailsStrategy GetOrCreateInterfaceDetailsStrategy() { throw null; }
protected virtual System.Runtime.InteropServices.Marshalling.IIUnknownStrategy GetOrCreateIUnknownStrategy() { throw null; }
protected sealed override void ReleaseObjects(System.Collections.IEnumerable objects) { }
}
[System.CLSCompliantAttribute(false)]
public readonly partial struct VirtualMethodTableInfo
{
private readonly int _dummyPrimitive;
public unsafe VirtualMethodTableInfo(void* thisPointer, void** virtualMethodTable) { throw null; }
public unsafe void* ThisPointer { get { throw null; } }
public unsafe void** VirtualMethodTable { get { throw null; } }
public unsafe void Deconstruct(out void* thisPointer, out void** virtualMethodTable) { throw null; }
}
}
namespace System.Security
{
public static partial class SecureStringMarshal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,28 @@
<Compile Include="System\Runtime\InteropServices\HandleCollector.cs" />
<Compile Include="System\Runtime\InteropServices\ImportedFromTypeLibAttribute.cs" />
<Compile Include="System\Runtime\InteropServices\ManagedToNativeComInteropStubAttribute.cs" />
<Compile Include="System\Runtime\InteropServices\Marshalling\ComExposedClassAttribute.cs" />
<Compile Include="System\Runtime\InteropServices\Marshalling\ComObject.cs" />
<Compile Include="System\Runtime\InteropServices\Marshalling\DefaultCaching.cs" />
<Compile Include="System\Runtime\InteropServices\Marshalling\DefaultIUnknownInterfaceDetailsStrategy.cs" />
<Compile Include="System\Runtime\InteropServices\Marshalling\ExceptionAsDefaultMarshaller.cs" />
<Compile Include="System\Runtime\InteropServices\Marshalling\ExceptionAsHResultMarshaller.cs" />
<Compile Include="System\Runtime\InteropServices\Marshalling\ExceptionAsNaNMarshaller.cs" />
<Compile Include="System\Runtime\InteropServices\Marshalling\ExceptionAsVoidMarshaller.cs" />
<Compile Include="System\Runtime\InteropServices\Marshalling\FreeThreadedStrategy.cs" />
<Compile Include="System\Runtime\InteropServices\Marshalling\GeneratedComClassAttribute.cs" />
<Compile Include="System\Runtime\InteropServices\Marshalling\GeneratedComInterfaceAttribute.cs" />
<Compile Include="System\Runtime\InteropServices\Marshalling\IComExposedClass.cs" />
<Compile Include="System\Runtime\InteropServices\Marshalling\IComExposedDetails.cs" />
<Compile Include="System\Runtime\InteropServices\Marshalling\IIUnknownCacheStrategy.cs" />
<Compile Include="System\Runtime\InteropServices\Marshalling\IIUnknownDerivedDetails.cs" />
<Compile Include="System\Runtime\InteropServices\Marshalling\IIUnknownInterfaceDetailsStrategy.cs" />
<Compile Include="System\Runtime\InteropServices\Marshalling\IIUnknownInterfaceType.cs" />
<Compile Include="System\Runtime\InteropServices\Marshalling\IIUnknownStrategy.cs" />
<Compile Include="System\Runtime\InteropServices\Marshalling\IUnknownDerivedAttribute.cs" />
<Compile Include="System\Runtime\InteropServices\Marshalling\IUnmanagedVirtualMethodTableProvider.cs" />
<Compile Include="System\Runtime\InteropServices\Marshalling\StrategyBasedComWrappers.cs" />
<Compile Include="System\Runtime\InteropServices\Marshalling\VirtualMethodTableInfo.cs" />
<Compile Include="System\Runtime\InteropServices\PrimaryInteropAssemblyAttribute.cs" />
<Compile Include="System\Runtime\InteropServices\RegistrationClassContext.cs" />
<Compile Include="System\Runtime\InteropServices\RegistrationConnectionType.cs" />
Expand All @@ -49,6 +71,7 @@

<ItemGroup>
<ProjectReference Include="$(CoreLibProject)" />
<ProjectReference Include="$(LibrariesProjectRoot)System.Collections.Concurrent\src\System.Collections.Concurrent.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace System.Runtime.InteropServices.Marshalling
{
/// <summary>
/// An attribute to mark this class as a type whose instances should be exposed to COM.
/// </summary>
/// <typeparam name="T">The type that provides information about how to expose the attributed type to COM.</typeparam>
[AttributeUsage(AttributeTargets.Class, Inherited = false)]
[CLSCompliant(false)]
public sealed class ComExposedClassAttribute<T> : Attribute, IComExposedDetails
where T : IComExposedClass
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,23 @@ internal ComObject(IIUnknownInterfaceDetailsStrategy interfaceDetailsStrategy, I
/// </summary>
private IIUnknownCacheStrategy CacheStrategy { get; }

internal bool UniqueInstance { get; init; }

/// <summary>
/// Returns an IDisposable that can be used to perform a final release
/// on this COM object wrapper.
/// Releases all references owned by this ComObject if it is a unique instance.
/// </summary>
/// <remarks>
/// This property will only be non-null if the ComObject was created using
/// This method does nothing if the ComObject was not created with
/// CreateObjectFlags.UniqueInstance.
/// </remarks>
public IDisposable? FinalRelease { get; internal init; }
public void FinalRelease()
{
if (UniqueInstance)
{
CacheStrategy.Clear(IUnknownStrategy);
IUnknownStrategy.Release(_instancePointer);
}
}

/// <inheritdoc />
RuntimeTypeHandle IDynamicInterfaceCastable.GetInterfaceImplementation(RuntimeTypeHandle interfaceType)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;

namespace System.Runtime.InteropServices.Marshalling
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;

// This type is only needed for the VTable source generator or to provide abstract concepts that the COM generator would use under the hood.
// These are types that we can exclude from the API proposals and either inline into the generated code, provide as file-scoped types, or not provide publicly (indicated by comments on each type).
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,23 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;

namespace System.Runtime.InteropServices.Marshalling
{
/// <summary>
/// Converts all exceptions to <see cref="T.NaN"/>.
/// Converts all exceptions to <see cref="IFloatingPointIeee754{T}.NaN"/>.
/// </summary>
/// <typeparam name="T">The unmanaged type to return the <c>NaN</c> value for.</typeparam>
[CustomMarshaller(typeof(Exception), MarshalMode.UnmanagedToManagedOut, typeof(ExceptionAsNaNMarshaller<>))]
public static class ExceptionAsNaNMarshaller<T>
where T : unmanaged, IFloatingPointIeee754<T>
{
/// <summary>
/// Convert the exception to <see cref="T.NaN"/>.
/// Convert the exception to <see cref="IFloatingPointIeee754{T}.NaN"/>.
/// </summary>
/// <param name="e">The exception</param>
/// <returns><see cref="T.NaN"/>.</returns>
/// <returns><see cref="IFloatingPointIeee754{T}.NaN"/>.</returns>
public static T ConvertToUnmanaged(Exception e)
{
// Use GetHRForException to ensure the runtime sets up the IErrorInfo object
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;

namespace System.Runtime.InteropServices.Marshalling
{
/// <summary>
/// Marshaller that swallows the exception.
/// </summary>
#pragma warning disable SYSLIB1057 // Marshaller type does not have the required shape
// This marshaller has 'void' as the unmanaged type,
// which is technically invalid but is the correct type for this case.
// This scenario is specially handled for exception marshalling to make this work.
[CustomMarshaller(typeof(Exception), MarshalMode.UnmanagedToManagedOut, typeof(ExceptionAsVoidMarshaller))]
#pragma warning restore SYSLIB1057 // Marshaller type does not have the required shape
public static class ExceptionAsVoidMarshaller
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;

namespace System.Runtime.InteropServices.Marshalling
{
[AttributeUsage(AttributeTargets.Class)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace System.Runtime.InteropServices.Marshalling
/// <summary>
/// Type level information for managed class types exposed to COM.
/// </summary>
[CLSCompliant(false)]
public unsafe interface IComExposedClass
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Reflection;

namespace System.Runtime.InteropServices.Marshalling
{
/// <summary>
/// Details about a managed class type exposed to COM.
/// </summary>
[CLSCompliant(false)]
public unsafe interface IComExposedDetails
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace System.Runtime.InteropServices.Marshalling
/// <summary>
/// Unmanaged virtual method table look up strategy.
/// </summary>
[CLSCompliant(false)]
public unsafe interface IIUnknownCacheStrategy
{
public readonly struct TableInfo
Expand All @@ -22,9 +23,9 @@ public readonly struct TableInfo
/// Construct a <see cref="TableInfo"/> instance.
/// </summary>
/// <param name="handle">RuntimeTypeHandle instance</param>
/// <param name="interfaceDetails">An <see cref="IIUnknownDerivedDetails"/> instance</param>
/// <param name="ptr">Pointer to the instance to query</param>
/// <param name="info">A <see cref="TableInfo"/> instance</param>
/// <returns>True if success, otherwise false.</returns>
/// <returns>The constructed <see cref="TableInfo"/> instance for the provided information.</returns>
TableInfo ConstructTableInfo(RuntimeTypeHandle handle, IIUnknownDerivedDetails interfaceDetails, void* ptr);

/// <summary>
Expand Down
Loading

0 comments on commit b9df364

Please sign in to comment.