精简版
如何在使用 IMetadataImport 时从 *.winmd
文件获取接口(interface)的接口(interface)标识符 (IID)?
例如Windows.Globalization.ICalendar:{CA30221D-86D9-40FB-A26B-D44EB7CF08EA}
加长版
A good example是 Windows.Globalization.ICalendar 接口(interface)。它的 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 文档) ( archive )<
最佳答案
精简版
自定义属性 blob 是 Guid 类的 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
您可以尝试属性类的完整名称:
“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;
}
奖金
关于winapi - 导入WinRT winmd时如何获取接口(interface)的Interface ID(IID,即GUID)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54774126/