如何初始化常量CLSID

如何初始化常量CLSID

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

问题描述

类ID(GUID)通常用以虚线分隔的十六进制数序列来指定。 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));

这不是什么特别的:例如,当类型库 ed,同样的方法用于附加 CLSID 到基于coclass的类型,然后如果。


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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 05:49