runtime: [API Proposal]: Provide an `IUnknown` IID constant

Background and motivation

The IUnknown interface ID (IID) is a well known constant that has been set since the early 90’s. This IID can be used in a myriad of cases, particularly with the new COM source generator. This constant should be defined in a way that can be used both as a declaration on an interface definition and as a way to easily implement QueryInterface().

Additional IIDs that could also be considered:

  • IID_IClassFactory
  • IID_IClassFactory2

API Proposal

The suggestion is a Guid member, but this could be a string, ReadOnlySpan<byte> or another type the Guid constructor can take. Another consideration is the constructor for GuidAttribute, which takes a string.

namespace System.Runtime.InteropServices;

public abstract class ComWrappers
{
+    public static readonly Guid IID_IUnknown;
}

API Usage

Used as a constant when implementing QueryInterface().

Alternative Designs

Define an IUnknown interface type in C# and define the value using the GuidAttribute. This approach is unfortunately not AOT compatible.

Risks

None. This is a well known value and one that should be available.

About this issue

  • Original URL
  • State: open
  • Created 10 months ago
  • Comments: 16 (16 by maintainers)

Most upvoted comments

It’d require the new feature, since you have a struct with fields of multiple different types (uint, ushort, ushort, byte[8]) and so you can’t represent that with a single endianness fixup. You need something that does fixups per field.

Alternative Designs Define an IUnknown interface type in C# and define the value using the GuidAttribute.

The COM interop packages out there use several different patterns: