Step 2: Defining the IUnknown Methods

The inherited IUnknown methods of the sample CEchoTool class are implemented as follows:

STDMETHODIMP CEchoTool::QueryInterface(const IID &iid, void **ppv)
{
  if (iid == IID_IUnknown || iid == IID_IDirectMusicTool)
  {
    *ppv = static_cast<IDirectMusicTool*>(this);
  } else
  {
    *ppv = NULL;
    return E_NOINTERFACE;
  }
 
  reinterpret_cast<IUnknown*>(this)->AddRef();
  return S_OK;
}
 
STDMETHODIMP_(ULONG) CEchoTool::AddRef()
{
    return InterlockedIncrement(&m_cRef);
}
 
STDMETHODIMP_(ULONG) CEchoTool::Release()
{
    if( 0 == InterlockedDecrement(&m_cRef) )
    {
        delete this;
        return 0;
    }
    return m_cRef;
}