获取结构的 UUID

Get UUID of struct

背景

我有一个 IDL file 定义了许多 struct 像这样

typedef[uuid(68D81983-793D-43BE-AC16-C74254C90607)] struct Foo
{
    // some members
} Foo;

编译时(使用 Visual Studio 2013)它会生成一个 .h 文件,其中相应的 struct 被翻译成

typedef /* [uuid] */ DECLSPEC_UUID("68D81983-793D-43BE-AC16-C74254C90607") struct Foo
{
    // some members
} Foo;

DECLSPEC_UUID扩展为

#define DECLSPEC_UUID(x)    __declspec(uuid(x))

问题

如何在别处检索此 struct 的 UUID?

包含生成的 header 后,我尝试使用 __uuidof

static const auto idFoo = __uuidof(Foo);

但是我得到一个编译错误

error C2787: 'Foo' : no GUID has been associated with this object

问题似乎是试图同时声明 typedef struct

typedef[uuid(68D81983-793D-43BE-AC16-C74254C90607)] struct Foo
{
    // some members
} Foo;

解决方法是声明struct,然后单独typedef。然后 __uuidof 可以正常工作。

[uuid(68D81983-793D-43BE-AC16-C74254C90607)] struct Foo
{
    // some members
};
typedef struct Foo Foo;