Creating a Template Interface in IDL with its Template Implementation in C++

Having the possibility of creating a template object, like a "CMap", with a COM interface for accessing remotly objects might come handy. Although, far from being trivial, it's possible to achieve with ATL. The "anti-VARIANT" programmers will cheer this opportunity. Here follow step-by-step instructions to untrangle this use-to-be problem.

I will use an object with one simple method as an exemple. This object is fully implemented in the source code. The method will return the minimum of two objects.

The interface will look like this:

interface IMinumumTYPE : IUnknown
	HRESULT Min( TYPE a, TYPE b, TYPE *pMim );

The steps to create this object are: 1) Implementing the object template. 2) Defining the interface in IDL using preprocessor macros. 3) Adding entry in the main object map.

Implementing the object template.

template<class TYPE, class T, const CLSID *pclsid, WCHAR wszModulename[] >
class CMinimumImpl : public CComObjectRootEx<CComMultiThreadModel>,
		     public CComCoClass< CMinimumImpl<TYPE,T,pclsid,wszModulename>, pclsid >,
		     public T

	HRESULT __stdcall Min( TYPE a, TYPE b, TYPE *pMin )
		*pMin = (a < b ? a : b );
		return S_OK;

	static HRESULT WINAPI UpdateRegistry(BOOL bRegister)
		StringFromGUID2( *pclsid, wszCLSID, 45 );

		_ATL_REGMAP_ENTRY regMap[] = { { L"ObjName", wszModulename },
																		{ L"ObjCLSID", wszCLSID },
																		{ NULL, NULL } };
		return _Module.UpdateRegistryFromResourceD( IDR_POLYREG, bRegister, regMap );


There is only one tricky part: the registration of the object in the registry. To achieve this, we are using replacable parameters in the registry resource file (.RGS). That file is included with the project. Every instance of %ObjName% and %ObjCLSID% will be replaced with the content of the vars wszModulename and wszCLISD. The object support only one interface, it could support more but not needed in that case. The interface supported is given as a template parameter. We will see now how to define that interface in your IDL file.

Using prepocessor macros to define the interface in IDL.

Four information is needed in the template object to be able to create the interface ant register it. The name of the object, the object entry map (CLSID, object implementation), the interface definition and the library associated with the interface.

We define the interface in IDL as:

#define CPP_QUOTE( name ) cpp_quote( #name )

			[								\
				uuid( RIID),						\
				pointer_default(unique)					\
			]								\
			interface I##NAME : IUnknown					\
			{								\
				HRESULT Min( [in]TYPE a, [in]TYPE b, [out]TYPE *pMin );	\
			};								\
				cpp_quote( "#ifdef __cplusplus" )			\
				cpp_quote( "#ifdef __MINIMUM_H_" )			\
				CPP_QUOTE( extern OLECHAR g_wsz##NAME[]; )		\
				CPP_QUOTE( typedef CMinimumImpl<TYPE )			\
				cpp_quote( "," )					\
				CPP_QUOTE( I##NAME )					\
				cpp_quote( "," )					\
				CPP_QUOTE( &CLSID_##NAME )				\
				cpp_quote( "," )					\
				CPP_QUOTE( g_wsz##NAME )				\
				CPP_QUOTE( > C##NAME;	)				\
				cpp_quote( "#endif" )					\
				cpp_quote( "#endif" )

The first part is the declaration of the interface, the second part write a typedef of the template implementation in the generated header file. Because the preprocessor will think the "," as argument separator, we must use multiple lines. You can see we are passing the TYPE, the Interface, the CLSID and the NAME of the object as template argument. The name is an extern var, it will be defined later, the interface is made as I appended to the name.

We can declare the interface for int and double as follow:

MINIMUM_INTERFACE( A91EBC05-87C0-11d2-9947-C26F54DDB3BE, int, MinimumInt )
MINIMUM_INTERFACE( A91EBC09-87C0-11d2-9947-C26F54DDB3BE, double, MinimumDouble )

The interfaces generated will be: IMinimumDouble and IMinimumInt.

But it's not over, we must declare the name which will be written in the registry.

Here are the macros:

#define BEGIN_NAME_MAP() cpp_quote( "#define NAME_MAP() \\" )
#define END_NAME_MAP()    cpp_quote(" ")

The macro BEGIN_NAME_MAP define a macro in the generated header file. Calling that macro once in a .cpp file will define the name for all your objects in the map. So we must add:

	NAME_ENTRY( MinimumInt )
	NAME_ENTRY( MinimumDouble )

Now we will generate another macro in the genarated header file to add all objects in the object map.

#define BEGIN_OBJECT_MAP()	cpp_quote( "#define OBJECT_MAP() \\" )
#define END_OBJECT_MAP()	cpp_quote(" " )

We use it in the IDL file like that:

	OBJECT_ENTRY( MinimumInt )
	OBJECT_ENTRY( MinimumDouble )

The last step is the library declaration:

#define BEGIN_LIBRARY()								\
			[							\
				uuid(0968E542-87B7-11D2-9947-C26F54DDB3BE),	\
				version(1.0),					\
				helpstring("codeguru 1.0 Type Library")		\
			]							\
			library CODEGURULib					\
			{							\
				importlib("stdole32.tlb");			\

#define END_LIBRARY() };

			[					\
				uuid(CLSID)			\
			]					\
			coclass NAME				\
			{					\
				[default] interface I##NAME;	\

It look like this for our two objects:

	LIBRARY_ENTRY( A91EBC0E-87C0-11d2-9947-C26F54DDB3BE, MinimumInt )
	LIBRARY_ENTRY( A91EBC11-87C0-11d2-9947-C26F54DDB3BE, MinimumDouble )

Adding entry in the main object map.

In your main project file, you must add two lines:

NAME_MAP()  //a macro we have generated 

	OBJECT_MAP()  another macro we have generated


The concept is kind is simple viewing by this angle. But be careful, as more extragavancies usage of the problem the complexity rise as well. Fortunatly for us, in the end, the versality usage of the whole would be much greater.

Download source - 12KB


  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Microsoft® Office 365 is a top choice for enterprises that want a cloud–based suite of productivity collaboration applications. With Office 365, you get access to Microsoft™ Office solutions practically anytime, anywhere, on virtually any device. It's a great option for current Microsoft users who can now build on their experience with Microsoft™ solutions while enjoying the flexibility of a cloud-based delivery. But even organizations with no previous investment in Microsoft will find that …

  • On-demand Event Event Date: March 23, 2017 As you adopt the use of cloud services, whether in public/IaaS, SaaS or hybrid environments, the attack surface expands and, if breached, the costs increase exponentially. This session is designed to help IT and security leaders understand and address the unique challenges that enterprises typically face when they deploy their applications in the public cloud. It summarizes the areas that the public cloud vendors typically take care of and highlights the areas …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date