Search in sources :

Example 21 with COMException

use of com.sun.jna.platform.win32.COM.COMException in project jna by java-native-access.

the class ProxyObject method queryInterface.

@Override
public <T> T queryInterface(Class<T> comInterface) throws COMException {
    assert COMUtils.comIsInitialized() : "COM not initialized";
    try {
        ComInterface comInterfaceAnnotation = comInterface.getAnnotation(ComInterface.class);
        if (null == comInterfaceAnnotation) {
            throw new COMException("queryInterface: Interface must define a value for iid via the ComInterface annotation");
        }
        final IID iid = this.getIID(comInterfaceAnnotation);
        final PointerByReference ppvObject = new PointerByReference();
        HRESULT hr = ProxyObject.this.getRawDispatch().QueryInterface(new REFIID(iid), ppvObject);
        if (WinNT.S_OK.equals(hr)) {
            Dispatch dispatch = new Dispatch(ppvObject.getValue());
            T t = this.factory.createProxy(comInterface, dispatch);
            // QueryInterface returns a COM object pointer with a +1
            // reference, we must drop one,
            // Note: createProxy adds one;
            int n = dispatch.Release();
            return t;
        } else {
            String formatMessageFromHR = Kernel32Util.formatMessage(hr);
            throw new COMException("queryInterface: " + formatMessageFromHR);
        }
    } catch (Exception e) {
        throw new COMException("Error occured when trying to query for interface " + comInterface.getName(), e);
    }
}
Also used : COMException(com.sun.jna.platform.win32.COM.COMException) HRESULT(com.sun.jna.platform.win32.WinNT.HRESULT) HRESULT(com.sun.jna.platform.win32.WinNT.HRESULT) WinNT(com.sun.jna.platform.win32.WinNT) VARIANT(com.sun.jna.platform.win32.Variant.VARIANT) IID(com.sun.jna.platform.win32.Guid.IID) REFIID(com.sun.jna.platform.win32.Guid.REFIID) PointerByReference(com.sun.jna.ptr.PointerByReference) Dispatch(com.sun.jna.platform.win32.COM.Dispatch) IDispatch(com.sun.jna.platform.win32.COM.IDispatch) REFIID(com.sun.jna.platform.win32.Guid.REFIID) WString(com.sun.jna.WString) ComInterface(com.sun.jna.platform.win32.COM.util.annotation.ComInterface) ConnectionPoint(com.sun.jna.platform.win32.COM.ConnectionPoint) TimeoutException(java.util.concurrent.TimeoutException) COMException(com.sun.jna.platform.win32.COM.COMException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

COMException (com.sun.jna.platform.win32.COM.COMException)13 HRESULT (com.sun.jna.platform.win32.WinNT.HRESULT)13 REFIID (com.sun.jna.platform.win32.Guid.REFIID)10 VARIANT (com.sun.jna.platform.win32.Variant.VARIANT)8 PointerByReference (com.sun.jna.ptr.PointerByReference)7 WString (com.sun.jna.WString)5 ConnectionPoint (com.sun.jna.platform.win32.COM.ConnectionPoint)5 IID (com.sun.jna.platform.win32.Guid.IID)5 WinNT (com.sun.jna.platform.win32.WinNT)5 Dispatch (com.sun.jna.platform.win32.COM.Dispatch)4 IDispatch (com.sun.jna.platform.win32.COM.IDispatch)4 ComInterface (com.sun.jna.platform.win32.COM.util.annotation.ComInterface)4 DISPIDByReference (com.sun.jna.platform.win32.OaIdl.DISPIDByReference)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 ExecutionException (java.util.concurrent.ExecutionException)4 TimeoutException (java.util.concurrent.TimeoutException)4 ComObject (com.sun.jna.platform.win32.COM.util.annotation.ComObject)3 GUID (com.sun.jna.platform.win32.Guid.GUID)2 EXCEPINFO (com.sun.jna.platform.win32.OaIdl.EXCEPINFO)2 DISPPARAMS (com.sun.jna.platform.win32.OleAuto.DISPPARAMS)2