Design of array for holding interface pointer
ive been lucky enough to avoid COM programming but unfortunetly ive
been asked to write a project and it requires a the development of a
COM server.
Im writing a COM interface which will hold an array of my IItem3
interface pointers. I have got the array to work successfully with
putItem and getItem function but my question is whether the method im
using for adding and removing the items from my array is correct. Im
using QueryInterface to make copies of the object to add / remove from
the array. The array is defined as:
std::vector < CAdapt< CComPtr<IItem3> > > m_aItems;
i would appreciate if anyone can verify my code to see im coding this
behaviour correctly, or is there an another method which is more
efficient (or possibly safer!?).
Any info is appreciated.
Andy
STDMETHODIMP CMyArray::putItem(IItem3 *pVal)
{
USES_CONVERSION;
CComPtr<IItem3> item3;
HRESULT hr = pVal->QueryInterface(__uuidof(IItem3),
(void**)&item3);
if(FAILED(hr))
return hr;
m_aItems.push_back (item3);
return S_OK;
}
STDMETHODIMP CMyArray::getItem(IItem3 * * pVal)
{
if (m_aItems.size() == 0)
return E_FAIL;
CComPtr <IItem3> item3 = ( CComPtr<IItem3> )m_aItems.front ();
HRESULT hr = item3->QueryInterface(__uuidof(IItem3),
(void**)pVal);
if(FAILED(hr))
return hr;
m_aItems.pop_back();
return S_OK;
}
STDMETHODIMP CMyArray::Count(LONG * lCount)
{
*lCount = m_aItems.size();
return S_OK;
}