组件对象模型与ATL实现补遗
cheungmine
2007-10-11
组件对象模型与ATL实现文章写好了,但是遗漏了一个关键问题。就是还有一个需要修改atlcom.h文件的地方,我把它更正在这里。打开按上篇文章改正后的atlcom0.h,找到下面的函数:
STDMETHODIMP IEnumOnSTLImpl<Base, piid, T, Copy, CollType>::Next
更正为(只需要改变粗体的地方,一句话而已)
hr = Copy::copy(pelt, &*m_iter);
改为
hr = Copy::copy(pelt, *m_iter);
改过之后的完整的函数如下:
template
<
class
Base,
const
IID
*
piid,
class
T,
class
Copy,
class
CollType
>
STDMETHODIMPIEnumOnSTLImpl < Base,piid,T,Copy,CollType > ::Next(ULONGcelt,T * rgelt,
ULONG * pceltFetched)
{
if (rgelt == NULL || (celt != 1 && pceltFetched == NULL))
return E_POINTER;
if (pceltFetched != NULL)
* pceltFetched = 0 ;
if (m_pcollection == NULL)
return E_FAIL;
ULONGnActual = 0 ;
HRESULThr = S_OK;
T * pelt = rgelt;
while (SUCCEEDED(hr) && m_iter != m_pcollection -> end() && nActual < celt)
{
//hr=Copy::copy(pelt,&*m_iter);
hr=Copy::copy(pelt,*m_iter);//CHEUNGMINE
if (FAILED(hr))
{
while (rgelt < pelt)
Copy::destroy(rgelt ++ );
nActual = 0 ;
}
else
{
pelt ++ ;
m_iter ++ ;
nActual ++ ;
}
}
if (SUCCEEDED(hr))
{
if (pceltFetched)
* pceltFetched = nActual;
if (nActual < celt)
hr = S_FALSE;
}
return hr;
}
STDMETHODIMPIEnumOnSTLImpl < Base,piid,T,Copy,CollType > ::Next(ULONGcelt,T * rgelt,
ULONG * pceltFetched)
{
if (rgelt == NULL || (celt != 1 && pceltFetched == NULL))
return E_POINTER;
if (pceltFetched != NULL)
* pceltFetched = 0 ;
if (m_pcollection == NULL)
return E_FAIL;
ULONGnActual = 0 ;
HRESULThr = S_OK;
T * pelt = rgelt;
while (SUCCEEDED(hr) && m_iter != m_pcollection -> end() && nActual < celt)
{
//hr=Copy::copy(pelt,&*m_iter);
hr=Copy::copy(pelt,*m_iter);//CHEUNGMINE
if (FAILED(hr))
{
while (rgelt < pelt)
Copy::destroy(rgelt ++ );
nActual = 0 ;
}
else
{
pelt ++ ;
m_iter ++ ;
nActual ++ ;
}
}
if (SUCCEEDED(hr))
{
if (pceltFetched)
* pceltFetched = nActual;
if (nActual < celt)
hr = S_FALSE;
}
return hr;
}
如果还有任何问题,请参考我的示例代码。