array of COM object

I have two interfaces IList and IItem (as IDispatch interface). I would like to keep array of Items in List.
 Now I use COleSafeArray, but it's complicated to use.
Can you advise me better way (template ...).

IList have methods:
Add([in]IDispatch* pItem)
GetList([out,retval] VARIANT* pArrayOfItems)
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Use CTypedPtrList internaly
and do not forget to call AddRef() in Add([in] IDispatch * pItem) method

What to me OLESAFEARRAY is better
because it do much work for us.
Hi mizera,

You can use an underlined C++ object that will be wrapped by your COM object.

For example, the prototype could be written as following:

class YourObjectListImpl
    CList<IDispatch*, IDispatch*>  m_listItems;
    void CleanUp ();

     (STDMETHOD)(Add) (IDispatch* pItem);
     (STDMETHOD)(GetList)(VARIANT* pArrayOfItems);

//---------- imlementation --------

STDMETHODIMP YourObjectListImpl::Add (IDispatch* pItem)
// skip error checking e.t.c ....

// notice the AddRef call - you're
// going to keep the object alive and
// therefore the list should be the
// "ownwer"

    pItem->AddRef ();
    m_listItems.AddTail (pItem);

    return S_OK;

STDMETHODIMP YourObjectListImpl::GetList(VARIANT* pArrayOfItems)
// skip error checking as well
   VariantInit (pArrayOfItems);

// AFAIK, the easy way to return variant
// array is still COleSafeArray

   COleSafeArray arRetArray;
   arRetArray.CreateOneDim (VT_DISPATCH, m_listItems.GetCount ());

   long lIndex = 0;
   for (POSITION pos = m_listItems.GetHeadPosition (); pos != NULL; lIndex++)
        IDispatch* pItem = m_listItems.GetNext (pos);
// put element AddRef's each item      
        arRetArray.PutElement (&lIndex, pItem);

   *pArrayOfItems = arRetArray.Detach ();

    return S_OK;

Don't forget to call Release for every item that's being removed from the list.

void YourObjectListImpl::CleanUp ()
    POSITION pos = m_listItems.GetHeadPosition();

In this way you can implement wrappers for arrays, lists, maps e.t.c.
If you don't like MFC template collections, you can use STL using the same technique.

Last note: <afxtempl.h> required for MFC template collections by default is not included by ATL Wizard.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.