Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

declspec

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++?

like image 520
ezpresso Avatar asked Jun 02 '11 20:06

ezpresso


2 Answers

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.

like image 91
ildjarn Avatar answered Oct 04 '22 02:10

ildjarn


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);
like image 37
Steed Avatar answered Oct 04 '22 02:10

Steed