Difference between IUnknown equality via QI or Inheritance
<p>I am interested in comparing two <code>IDirect3DDevice9</code> COM pointers, which inherit <code>IUnknown</code>, for equality. Based on a <a href="https://stackoverflow.com/questions/23403777/check-com-pointers-for-equality">similar topic</a>, I understand you can <code>QI</code> <code>IUnknown</code> for both pointers, and compare the result for equality. However, I am curious if we can accomplish the same thing via an <code>IsEqual</code> method that directly takes two <code>IUnknown</code> pointers, let inheritance determine the relevant <code>IUnknown</code> pointers, and use that for the equality check instead. Based on my experiment, that seems to work and doesn't require the <code>QI</code> and <code>release</code> operation (or maybe that's done implicitly). If there's any caveats that warrant this suggestion invalid, please let me know.</p>

<pre><code>BOOL IsEqual(IUnknown *pA, IUnknown *pB)
{
    return (pA == pB);
}

BOOL IsEqual (IDirect3DDevice9 *pDevice1, IDirect3DDevice9 *pDevice2)
{
    IUnknown *u1, *u2;

    pDevice1-&gt;QueryInterface(IID_IUnknown, &amp;u1);
    pDevice2-&gt;QueryInterface(IID_IUnknown, &amp;u2);

    BOOL areSame = u1 == u2;
    u1-&gt;Release();
    u2-&gt;Release();

    return areSame;
}
</code></pre>