Cook Computing

embedded_idl and Attributed COM Programming

June 13, 2003 Written by Charles Cook

I've just started using VS2003 for some COM coding (we had to wait until VS2003 before moving from VS6 because migrating to VS2002 and then later to VS2003 would have required an extra round of project file conversions). After several years of coding COM objects the traditional ATL way I thought I'd use the new feature of VS2003 which allows you to implement COM objects using attributes. I tried to import the interface definitions from a type library (which was generated from a .NET assembly) but the compiler spat out this error:


c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\atlmfc\include\atlcom.h
(1759): error C2259: 'ATL::CComObject<Base>' : cannot instantiate abstract class 
        with
        [
            Base=CUmCLRHost
        ]

This is caused because the header file generated by #import does not contain the necessary attributes:


struct __declspec(uuid("abcc0308-262a-11d2-aa95-00c04fa31dd1"))
ICLRManager : IDispatch
{
      virtual HRESULT __stdcall InitializeCLR ( ) = 0;
      virtual HRESULT __stdcall ShutdownCLR ( ) = 0;
};

The solution is to use the embedded_idl attribute on the #import:


#import "../interfaces/interfaces/bin/debug/interfaces.tlb" \
	no_namespace, raw_interfaces_only, embedded_idl 

The generated header definition now looks like this:


[uuid("abcc0308-262a-11d2-aa95-00c04fa31dd1"),dual]
__interface ICLRManager : IDispatch
{
      [id(0x60020000)] HRESULT __stdcall InitializeCLR ( );
      [id(0x60020001)] HRESULT __stdcall ShutdownCLR ( );
};