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

  • Live Event Date: May 11, 2015 @ 1:00 p.m. ET / 10:00 a.m. PT One of the languages that have always been supported with the Intel® RealSense™ SDK (Software Developer Kit) is JavaScript, specifically so that web-enabled apps could be created. Come hear from Intel Expert Bob Duffy as he reviews his own little "space shooting" game where the orientation of your face controls the aiming reticle to help teach developers how to write apps and games in JavaScript that can use facial and gesture …

  • Live Event Date: May 6, 2015 @ 1:00 p.m. ET / 10:00 a.m. PT Where are you in your plans to adopt Disaster Recovery-as-a-Service? Are you just getting started? Fighting an uphill battle with management? At Cisco, Zerto and iland, we've seen it all – from the early adopters who excitedly rushed to implement DRaaS with us nine years ago to the IT folks dragging their business leaders into the future. With our years of experience, we've learned there are six types of DRaaS leaders – but which type …

Most Popular Programming Stories

More for Developers

RSS Feeds

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