Exposing an STL Vector of my objects via COM Interface Using ATL

Posted on 2005-05-06
Last Modified: 2013-12-14
I'm trying to expose an STL Vector of my objects via COM Interface Using ATL.

I can find examples where built in types are being used e.g. the STL vector is storing wstring objects and clients are expecting BSTR objects.  But I have a COM Server that stores a list of Widget objects.  So I want the STL vector to store Widget objects and the client to also expect Widget objects.
COM Server is in process. Client is VB or C++.

I create my Simple Object using the ATL COM Wizard which generates an interface IWidgetManager.
I then open the IDL file and add the three properties necessary to turn IWidgetManager into a read-only collection interface i.e. Count, Item, _NewEnum.
If the Client is expecting BSTR objects, the resulting IDL file would look like this

interface IWords : IDispatch
    [id(DISPID_NEWENUM), propget]
    HRESULT _NewEnum([out, retval] IUnknown** ppUnk);

    [id(DISPID_VALUE), propget]
    HRESULT Item(
        [in] long Index,
        [out, retval] BSTR* pVal);    

    [id(0x00000001), propget]    
    HRESULT Count([out, retval] long* pVal);

But I want my client to expect the the same objects that I am queuing i.e. Widgets

 [id(DISPID_VALUE), propget]
    HRESULT Item(
        [in] long Index,
        [out, retval] Widget* pVal);

I have 2 questions:-
1. Is what I am doing sensible i.e. My COM Server is returning a list of Widgets and I expect the Client to then understand Widgets.  Am I doing some close coupling here that will come back to haunt me when I have learned a bit more i .e. should I actually be returning a list of Interface objects i.e. IWidgets ??
2. Given the description above - how do I tell the idl/midl about the Widget class ?
Question by:ascot
    LVL 86

    Expert Comment

    >>Client is VB or C++.

    If your client is VB, you should create an IDispatch-derived IWidget interface to ensure the compatibility, e.g.

    interface IWebBrowser : IDispatch {

    // IWidget methods here

    Author Comment

    Thanks for the feedback.  Would you mind expanding on that though please jkr - I don't have as much knowledge as it might seem I have from the question

    LVL 86

    Accepted Solution

    Hmm, since there are not too many details given, the explanation will be a tad bit general, so, you'd start declaring

    helpstring("IWidget interface"),
    interface IWidget : IDispatch
    // IWidget methods here
        [id(DISPID_GETNAME), propget]
       HRESULT GetName( [out, retval] BSTR* pNamel);    


    you can use the IWidget type like you wrote above:

     [id(DISPID_VALUE), propget]
       HRESULT Item(
           [in] long Index,
            [out, retval] IWidget* pVal);

    Author Comment

    Thanks for the further explanation jkr.  I understand what you mean now.  However, I have concluded that it makes sense to develop my component as a .NET component and use CCW to call from existing COM clients.
    That way all the COM headache stuff is hidden from me.
    I am closing this question and awarding the points to jkr.  Don't suppose you can help on the threading model question that I have outstanding can you ?

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Join & Write a Comment

    Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
    After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
    The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
    The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

    731 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now