QueryInterface

For a coclass that implements multiple interfaces you can access the non-default interfaces by using IUnknown.QueryInterface. There are two forms of QueryInterface. One that uses the [out] parameter convention. This form determines the interface that you want by looking at the type of array you pass in.

public void testQueryInterface() {
    COM.OleInitialize();
    try {
        IDual d = new Dual();
        try {
            IExtraDual[] out_ed = { null };
            d.QueryInterface(out_ed);
            try {
                out_ed[0].setPropBSTR("BSTR");
                assertEquals("BSTR", out_ed[0].getPropBSTR());
            } finally {
                out_ed[0].Release();
            }
        } finally {
            d.Release();
        }
    } finally {
        COM.OleUninitialize();
    }
}

The second form of QueryInterface returns the new interface. You must pass in the class of the interface you want and you must cast the return value to the correct interface.

public void testQueryInterface2() {
    COM.OleInitialize();
    try {
        IDual d = new Dual();
        try {
            IExtraDual ed;
            ed = (IExtraDual)d.QueryInterface(IExtraDual.class);
            try {
                ed.setPropBSTR("BSTR");
                assertEquals("BSTR", ed.getPropBSTR());
            } finally {
                ed.Release();
            }
        } finally {
            d.Release();
        }
    } finally {
        COM.OleUninitialize();
    }
}

$Id: query_interface.html 3769 2007-06-08 19:06:43Z hastings $