如何初始化常量CLSID [英] How to initialize a constant CLSID

查看:757
本文介绍了如何初始化常量CLSID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

类ID(GUID)通常用以虚线分隔的十六进制数序列来指定。 {557cf406-1a04-11d3-9a73-0000f81ef32e} 。这不是一个可以直接初始化一个CLSID结构的文字。



我发现了两种方式来初始化结构,但是它们都是尴尬的。第一个不允许声明 const ,必须在运行时完成,而第二个则需要对十六进制常量进行大量的重新格式化。

  CLSID clsid1; 
CLSIDFromString(CComBSTR({557cf406-1a04-11d3-9a73-0000f81ef32e}),& clsid1);

const CLSID clsid2 = {0x557cf406,0x1a04,0x11d3,{0x9a,0x73,0x00,0x00,0xf8,0x1e,0xf3,0x2e}};

我知道Visual Studio可以自动生成一个类型,如果你有一个类型与UUID关联,使用 __ uuidof 运算符。如果你只有十六进制字符串,有办法吗?

解决方案 code>从字符串初始化(不需要运行时转换助手):

  class __declspec(uuid({557cf406-1a04 -11d3-9a73-0000f81ef32e}))Foo; 
static const CLSID CLSID_Foo = __uuidof(Foo);
// ...
CComPtr< IUnknown> pUnknown;
pUnknown.CoCreateInstance(CLSID_Foo);

或直接使用 __ uuidof 会将GUID值视为常量并生成最少的必要代码):

  class __declspec(uuid({557cf406-1a04- 11d3-9a73-0000f81ef32e}))Foo; 
// ...
CComPtr< IUnknown> pUnknown;
pUnknown.CoCreateInstance(__ uuidof(Foo));

这不是什么特别的:例如,当类型库 #import ed,同样的方法用于附加 CLSID 到基于coclass的类型,然后如果 CLSID_xxx .microsoft.com / en-us / library / xybakhaarel =nofollow>另外要求


A class ID (GUID) is generally specified with a sequence of hex numbers separated by dashes, e.g. {557cf406-1a04-11d3-9a73-0000f81ef32e}. This is not a literal that can be used to initialize a CLSID structure directly.

I've discovered two ways to initialize the structure, but they're both kind of awkward. The first doesn't allow it to be declared const and must be done at run time, while the second requires extensive reformatting of the hex constants.

CLSID clsid1;
CLSIDFromString(CComBSTR("{557cf406-1a04-11d3-9a73-0000f81ef32e}"), &clsid1);

const CLSID clsid2 = { 0x557cf406, 0x1a04, 0x11d3, { 0x9a,0x73,0x00,0x00,0xf8,0x1e,0xf3,0x2e } };

I know that Visual Studio can generate one automatically if you have a type that's associated with a UUID, using the __uuidof operator. Is there a way to do it if you only have the hex string?

解决方案

Static CLSID initialization from string (no runtime conversion helper needed):

class __declspec(uuid("{557cf406-1a04-11d3-9a73-0000f81ef32e}")) Foo;
static const CLSID CLSID_Foo = __uuidof(Foo);       
// ...
CComPtr<IUnknown> pUnknown;
pUnknown.CoCreateInstance(CLSID_Foo);

or simply direct use of __uuidof (compiler will treat the GUID value as a constant and generate minimal necessary code):

class __declspec(uuid("{557cf406-1a04-11d3-9a73-0000f81ef32e}")) Foo;
// ...
CComPtr<IUnknown> pUnknown;
pUnknown.CoCreateInstance(__uuidof(Foo));

It is not anything special: for example when type libraries are #imported, the same method is used to attach CLSIDs to coclass based types, and then additional CLSID_xxx identifiers might be generated if additionally requested.

这篇关于如何初始化常量CLSID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆