【问题标题】:How to get the Interface ID (IID, i.e. the GUID) of an interface when importing a WinRT winmd?导入WinRT winmd时如何获取接口的接口ID(IID,即GUID)?
【发布时间】:2019-07-13 10:07:30
【问题描述】:

短版

在使用 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 程序集,而那个数据库就是事实。

在标题中

您可以在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 时如何获取它?

代码

abridged version 如何启动并运行读取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 博客:Metadata Unmanaged API (据我所知,旧 Word 文档的初步 PDF 版本是元数据 API 的唯一 Microsoft 文档) (@ 987654325@)

【问题讨论】:

  • 您需要调用GetCustomAttributeByName 来检索Windows.Foundation.Metadata.GuidAttribute 属性的值,然后解析生成的blob。

标签: winapi com windows-runtime midl winmd


【解决方案1】:

短版

自定义属性 blobGuid 类的 C# 序列化格式:

3.2.2 定义自定义属性

用于定义自定义属性的 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 本质上是传递给 Attribute 的构造函数。序列化格式与 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

【讨论】:

    猜你喜欢
    • 2019-08-31
    • 1970-01-01
    • 2014-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-25
    相关资源
    最近更新 更多