C++ – Creating an ATL COM object that implements a specific interface

atlccom

I need to implement a simple ATL COM object that implements a specific interface for which I have been given both a .tlb file and a .idl file. The interface is very simple and only consists of a single method. I have created many ATL objects in the past but never one that has to implement a specific interface. What do I need to achieve this? I'm assuming that I somehow need to reference the interface's IDL or TLB in my new objects IDL somewhere?

Any pointers are welcome.

Best Answer

It's much more automatic than the other answers here are suggesting. All the boilerplate code is written for you by Visual Studio.

You're lucky you have the .idl, it's by far the most conveninent, I think.

You could paste the contents of the .idl file into your ATL COM project's existing .idl file, which would give you access to the declarations in it. For example, something like this could be pasted into an IDL file:

[
    object,
    uuid(ecaac0b8-08e6-45e8-a075-c6349bc2d0ac),
    dual,
    nonextensible,
    helpstring("IJim Interface"),
    pointer_default(unique)
]
interface IJim : IDispatch
{
    [id(1), helpstring("method SpliceMainbrace")] HRESULT SpliceMainbrace(BSTR* avast);
};

Then in Class View, right click your class and choose Add | Implement Interface.

Notice that in this dialog, you can actually browse for a .tlb file, but I think it's better to have plain-text source for these things, for version control and the like.

Pick IJim from the list, press the > button to add it to the list to be implemented. Press Finish.

Visual Studio will add this to your class (along with a bunch of other crap to make it work):

// IJim Methods
public:
    STDMETHOD(SpliceMainbrace)(BSTR * avast)
    {
        // Add your function implementation here.
        return E_NOTIMPL;
    }
Related Topic