开发者

What does __declspec(uuid(" ComObjectGUID ")) expand to?

I have a piece of code that uses Microsoft-specific extension to the C++:

interface __declspec(uuid("F614FB00-6702-11d4-B0B7-0050BABFC904"开发者_开发技巧))
ICalculator : public IUnknown
{ 
    //...
};

What does this sentence expand to? How can I rewrite it with ANSI C++?


It's not a macro so it doesn't "expand" to anything. It merely decorates the type with a given UUID in the object file metadata, which can then be extracted later with the __uuidof operator.


You can also use templates (traits), if you need to statically "attach" a guid to the interface. Consider:

In a common h-file you create an empty unspecialized template:

template<typename TInterface> struct TInterfaceTraits {}

When defining your interface, write a template specialization for it (or you can write it in any other place including just before usage):

class ICalculator : public IUnknown
{ 
    //...
};
template<> struct TInterfaceTraits<class ICalculator > { 
    static GUID guid() { 
        return IID_ICalculator ; 
    } 
};

Then to get it's uuid you can write something like:

ICalculator *pCalcFace;
pObject->QueryInterface(TInterfaceTraits<ICalculator>::guid(), (void**)pCalcFace);

Of cause you can write (I leave it to you) a template wrapper to QueryInterface, which will use the traits to automatically supply proper guid, and that will be even easier to use, i.e.

ICalculator *pCalcFace = QueryInterface<ICalculator>(pObject);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜