組件對象模型與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>
STDMETHODIMP IEnumOnSTLImpl<Base, piid, T, Copy, CollType>::Next(ULONG celt, 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;
ULONG nActual = 0;
HRESULT hr = 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;
}
如果還有任何問題,請參考我的示例代碼。
http://download.csdn.net/source/260939