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

3

简短版

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

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

较长版

Windows.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程序集,而该数据库是基本事实。

它在头文件中

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

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

它甚至在winmd中

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

enter image description here

但是当使用文档中记录的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.

额外阅读材料

  • MSDN 博客:元数据非托管 API (一份旧的 Word 文档的初步 PDF 版本,据我所知,这是 Metadata API 的唯一 Microsoft 文档) (存档)

1
你需要调用 GetCustomAttributeByName 方法来检索 Windows.Foundation.Metadata.GuidAttribute 属性的值,然后解析结果 blob。 - Kenny Kerr
1个回答

1

简短版本

blob自定义属性是C#序列化的Guid类格式:

3.2.2 DefineCustomAttribute

定义自定义属性的pBlob格式在本规范中稍后定义(广义上讲,blob记录了类构造函数的参数值,以及零个或多个命名字段/属性的值——换句话说,需要实例化元数据发出时指定的对象的信息)。如果构造函数不需要参数,则无需提供blob参数。

4.3.6 GetCustomAttributeProps

自定义属性存储为一个blob,其格式由元数据引擎和反射理解;基本上是一个参数值列表,用于创建自定义属性实例的构造方法。

为了获取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

您可以尝试使用属性类的完整名称:

  • "System.Runtime.InteropServices.GuidAttribute"

但是这也失败了,因为这是.NET框架中GuidAttribute类的名称。在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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接