1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
|
void MyCOMObject::CreateThis()
{
if (m_pOuter)
m_pOuter->AddRef();
}
void MyCOMObject::DeleteThis()
{
IUnknown* pOuter m_pOuter;
delete this;
if (pOuter)
pOuter->Release();
}
ULONG MyCOMObject::AddRef(void)
{
ULONG ret = (ULONG) InterlockedIncrement((LONG*)&m_uRefCount);
if (ret == 1)
CreateThis(); // this doesn't need to be thread-safe, because this is the first reference on the object !!!
}
LONG MyCOMObject::Release(void)
{
ULONG ret = (ULONG) InterlockedDecrement((LONG*)&m_uRefCount);
if (ret == 0)
DeleteThis(); // same as above, this is the last reference, no need to protect
}
HRESULT MyCOMObject::QueryInterface(REFIID riid, LPVOID* ppvOut)
{
if (riid == IID_IUnknown) {
*ppvOut = this;
AddRef();
return S_OK;
} else
return E_NOINTERFACE;
} |
Partager