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)
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.The COM interop packages out there use several different patterns:
ref readonly Guid
returning property instatic class IID
: https://github.com/terrafx/terrafx.interop.windows/blob/fadce5a41fa5e6f0282e80e96f033d0a2c130991/sources/Interop/Windows/Windows/um/Unknwnbase/IID.cs#L15interface IComIID
withstatic
property that returns the Guid: https://github.com/microsoft/CsWin32/blob/990484777aa161aebf62ee02ba457b2f5f8286cc/src/Microsoft.Windows.CsWin32/Generator.Com.cs#L1039-L1042