导入WinRT winmd时如何获取接口的Interface ID(IID,即GUID)?

How to get the Interface ID (IID, i.e. the GUID) of an interface when importing a WinRT winmd?

简短版

使用 IMetadataImport 时,如何从 *.winmd 文件中获取接口的接口标识符 (IID)?

例如Windows.Globalization.ICalendar: {CA30221D-86D9-40FB-A26B-D44EB7CF08EA}

加长版

一个good exampleWindows.Globalization.ICalendar接口。它的 IID 是 CA30221D-86D9-40FB-A26B-D44EB7CF08EA.

它在 IDL 中

您可以在源Windows.Globalization.idl文件中找到它:

[exclusiveto(Windows.Globalization.Calendar)]
[uuid(CA30221D-86D9-40FB-A26B-D44EB7CF08EA)]
[version(0x06020000)]
interface ICalendar : IInspectable
{
   //...snip...
}

提醒:您不应该解析这些文件。它被编译成一个 *.winmd 程序集,该数据库是 ground-truth.

它在 header

您可以在 windows.globalization.h 文件中找到它,该文件是使用导入工具从 *.winmd 生成的:

namespace ABI {
    namespace Windows {
        namespace Globalization {
            
            MIDL_INTERFACE("CA30221D-86D9-40FB-A26B-D44EB7CF08EA")
            ICalendar : public IInspectable
            {
               //...snip...
            }

甚至在 winmd 中

您甚至可以在编译后的 *.winmd 程序集数据库中找到 InterfaceID:

但是如何在使用记录的IMetadataImporter API时得到它?

代码

如何起床和运行阅读winmd元数据文件的

// Create your metadata dispenser:
IMetadataDispsener dispener;
MetaDataGetDispenser(CLSID_CorMetaDataDispenser, IMetaDataDispenser, out dispenser);

//Open the winmd file we want to dump
String filename = "C:\Windows\System32\WinMetadata\Windows.Globalization.winmd";

IMetaDataImport reader; //IMetadataImport2 supports generics
dispenser.OpenScope(filename, ofRead, IMetaDataImport, out reader); //"Import" is used to read metadata. "Emit" is used to write metadata.

红利阅读

精简版

自定义属性 blobGuid class:

的 C# 序列化格式

3.2.2 DefineCustomAttribute

The format of pBlob for defining a custom attribute is defined in later in this spec. (broadly speaking, the blob records the argument values to the class constructor, together with zero or more values for named fields/properites – in other words, the information needed to instantiate the object specified at the time the metadata was emitted). If the constructor requires no arguments, then there is no need to provide a blob argument.

4.3.6 GetCustomAttributeProps

A custom attribute is stored as a blob whose format is understood by the metadata engine, and by Reflection; essentially a list of argument values to a constructor method which will create an instance of the custom attribute.

为了获得 GuidAttriute guid 值,您必须模拟 C# 从流中反序列化 Guid 对象。

长版

从您的 IMetadataImport 开始,您调用 IMetaDataImport.GetCustomAttributeByName.

第一个棘手的部分是找出我要查找的属性的名称。我知道在 IDL 或 C# 中查看时是 Guid:

[Guid("CA30221D-86D9-40FB-A26B-D44EB7CF08EA")]
interface ICalendar
{
    //...
}

而它下面的那个实际上被称为 "GuidAttribute"。但这些都不起作用:

  • "Guid":失败 S_FALSE
  • "GuidAttribute":失败 S_FALSE

您可以尝试 完整 属性名称 class:

  • "System.Runtime.InteropServices.GuidAttribute"

但这也失败了,因为那是 .NET 框架中 GuidAttribute class 的名称。在 WinRT 库中你必须使用 "Windows.Foundation.Metadata.GuidAttribute":

  • "Guid":失败 S_FALSE
  • "GuidAttribute":失败 S_FALSE
  • "System.Runtime.InteropServices.GuidAttribute":失败 S_FALSE(仅限 CLR)
  • "Windows.Foundation.Metadata.GuidAttribute":有效

既然我们知道了要查找的属性的名称,我们就可以查询它了:

mdToken calendarTokenID = 0x02000022; //Windows.Globalization.ICalendar
String  attributeName   = "Windows.Foundation.Metadata.GuidAttribute";

Pointer blob;
UInt32 blobLen;
reader.GetCustomAttributeByName(calendarTokenID, attributeName, out blob, out blobLen);

下一个棘手的部分是解码 blob。

解码 blob

每个自定义属性都有不同的序列化格式。 blob 基本上传递给属性的构造函数。序列化格式与C#序列化格式相同。

对于GuidAttribute属性,二进制序列化格式为20字节:

01 00                                            Prolog (2-bytes)       0x0001 ==> version 1
1D 22 30 CA D9 86 FB 40 A2 6B D4 4E B7 CF 08 EA  Guid (16-bytes)        "CA30221D-86D9-40FB-A26B-D44EB7CF08EA"
00 00                                            Trailing null (2-bytes)

我提取 Guid 的最简单方法是声明匹配结构,将返回的指针转换为该结构的类型,然后访问 Guid 成员:

struct SerializedGuidAttribute
{
   UInt16 prolog; //2-bytes. 0x0001 
   Guid   guid;   //16-byte guid
   UInt16 footer; //2-byte footer
}
typedef SerializedGuidAttribute* PSerializedGuidAttribute;

Guid guidAttriute = PSerializedGuidAttribute(blob).guid;

你拥有它

Guid GetGuidAttribute(IMetadataReader reader, mdToken intf)
{
   Pointer blob;
   UInt32 blobLen;
   reader.GetCustomAttributeByName(intf, "Windows.Foundation.Metadata.GuidAttribute", 
         out blob, out blobLen);

   //if (blobLen != 20) { throw new Exception("Something") };

   return PSerializedGuidAttribute(blob).guid;
}

奖金

  • Microsoft 元数据 API 文档
    • 2000 年 9 月 8 日:.docx
    • 2001 年 8 月 2 日:.pdf