Repository navigation
Closed
Labels
api-approvedAPI was approved in API review, it can be implementedarea-System.Runtime.InteropServices
Milestone
Description
Background and motivation
It would be nice to have some wrappers that would return other interfaces pointers, something like:
ComWrappers.TryGetComInstance<T>(unwrapped, out nint iface);
that would do the QI so I could call it like this:
ComWrappers.TryGetComInstance<IFoo>(unwrapped, out nint ifoo);
As a "frequent COM flyer", It's quite perturbing to have to juggle between ComWrappers and Marshal classes, actually it would be nice with all that new code to be able to avoid using Marshal completely :-)
Originally posted by @smourier in #106978 (comment)
public abstract partial class ComWrappers
{
+ public bool TryGetComInstance<TInterface>(object instance, CreateComInterfaceFlags flags, out IntPtr result);
}This could also be an auto-generated method that would be provided on a derived ComWrappers instance. Looking up the IID on TInterface would need to be AOT friendly, which means typeof(TInterface).GUID is out.
API Proposal
The below API is modified from the OP proposal to align with existing API shape and motivating example below.
public abstract class ComWrappers
{
public IntPtr GetOrCreateComInterfaceForObject(object instance, CreateComInterfaceFlags flags);
+ public IntPtr GetOrCreateComInterfaceForObject(object instance, CreateComInterfaceFlags flags, in Guid interfaceId);
}public class StrategyBasedComWrappers
{
+ public IntPtr GetOrCreateComInterfaceForObject<TInterface>(object instance, CreateComInterfaceFlags flags);
}- Failing to QI for
TInterfacewill trigger anInvalidCastException. - Failing to QI for
TInterfacewill still insert into the map.
API Usage
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.Marshalling;
[assembly: DisableRuntimeMarshalling]
namespace ConsoleAotAuthor
{
internal partial class Program
{
static void Main()
{
var cw = new StrategyBasedComWrappers();
var foo = new Foo();
// Will throw if foo doesn't QI for IFoo.
nint ifoo = cw.GetOrCreateComInterfaceForObject<Foo, IFoo>(foo, CreateComInterfaceFlags.None);
GivePointerToComInterface(ifoo);
}
[LibraryImport("Dll1.dll")]
private static partial void GivePointerToComInterface(nint comObject);
}
[GeneratedComInterface, Guid("217104c3-ce28-4d13-a32e-ef65ee75edf1")]
public partial interface IFoo
{
void Method(int i);
}
[GeneratedComClass]
public partial class Foo : IFoo
{
public void Method(int i) => Console.WriteLine(".NET i:" + i);
}
}Alternative Designs
No response
Risks
No response
Reacted by Paulus Pärssinen
Reacted by Copilot
Metadata
Metadata
Assignees
Labels
api-approvedAPI was approved in API review, it can be implementedarea-System.Runtime.InteropServices
Type
Projects
- StatusShow more project fieldsNo status
Activity
dotnet-policy-service commentedon Aug 26, 2024
Tagging subscribers to this area: @dotnet/interop-contrib
See info in area-owners.md if you want to be subscribed.
jkoritzinsky commentedon Aug 26, 2024
We could do this as a method on StrategyBasedComWrappers without too much trouble as we have a mechanism to look up interface details.
On ComWrappers, we'd have to require the user to pass in the IID as there's no Type->IID mechanism at that level.
AaronRobinsonMSFT commentedon Aug 26, 2024
I like that. We could follow what @smourier is alluding to and simply apply the
Marshal.QueryInterfacesignature to the above. That way we have the low level API that we can build upon in derivedComWrappers?jkoritzinsky commentedon Aug 26, 2024
Yep! I think that's a great way to do it.
jkoritzinsky commentedon Aug 26, 2024
I've updated the top of the issue to be in the API Proposal format and added one more convenience overload that I think would be quite useful.
13 remaining items
bartonjs commentedon Aug 25, 2026
Video
Tparameter from the StrategyBasedComWrappers version as it seems like T : TInterface is implied by context.AaronRobinsonMSFT commentedon Aug 25, 2026
This was discussed offline with the interop team. We're going to keep with this approach. We considered additional APIs on
ComWrappersbut none seemed to improve the ergonomics and largely became just callingMarshal.QueryInterface()andMarshal.Release()in a macro style. Since the use case here was framed as simply asking for an affordance to bundle the known target interface as the return, the proposal is to keep as-is.Done.
bartonjs commentedon Sep 8, 2026
Video
Looks good as proposed.
Add interface-selecting ComWrappers overloads (#134354)