Search in sources :

Example 1 with IID

use of com.sun.jna.platform.win32.Guid.IID in project jna by java-native-access.

the class ComEventCallbacksFactory_Test method testComEventCallback.

@Test
public void testComEventCallback() {
    DWebBrowserEvents2_Listener listener = new DWebBrowserEvents2_Listener();
    CallbackProxy proxy = new CallbackProxy(factory, DWebBrowserEvents2.class, listener);
    REFIID refiid = new REFIID(new IID(DWebBrowserEvents2.IID));
    // precondition: the structures for the listenedToRiid and
    // refiid have to be different (else the PointerType#equals would
    // be enough
    assertFalse(proxy.listenedToRiid.getPointer().equals(refiid.getPointer()));
    // Neverthe less, the QueryInterface method has to return the
    // correct pointer (the IID is relevant, not its wrapper
    PointerByReference interfacePointer = new PointerByReference();
    // Check the "business" interface
    HRESULT hr = proxy.QueryInterface(refiid, interfacePointer);
    assertTrue(COMUtils.SUCCEEDED(hr));
    assertEquals(interfacePointer.getValue(), proxy.getPointer());
    // IUnknown must be implemented
    hr = proxy.QueryInterface(new REFIID(IID_IUNKNOWN), interfacePointer);
    assertTrue(COMUtils.SUCCEEDED(hr));
    assertEquals(interfacePointer.getValue(), proxy.getPointer());
    // Currently only Dispatch based callbacks are supported,
    // so this interface must be present to
    hr = proxy.QueryInterface(new REFIID(IID_IDISPATCH), interfacePointer);
    assertTrue(COMUtils.SUCCEEDED(hr));
    assertEquals(interfacePointer.getValue(), proxy.getPointer());
    // Negative check -- this has to fail, the IID should not be
    // assigned
    hr = proxy.QueryInterface(new REFIID(new IID("{00000000-0000-0000-C000-000000000000}")), interfacePointer);
    assertTrue(COMUtils.FAILED(hr));
}
Also used : HRESULT(com.sun.jna.platform.win32.WinNT.HRESULT) IID(com.sun.jna.platform.win32.Guid.IID) REFIID(com.sun.jna.platform.win32.Guid.REFIID) PointerByReference(com.sun.jna.ptr.PointerByReference) REFIID(com.sun.jna.platform.win32.Guid.REFIID) Test(org.junit.Test)

Example 2 with IID

use of com.sun.jna.platform.win32.Guid.IID in project jna by java-native-access.

the class ProxyObject method advise.

public IComEventCallbackCookie advise(Class<?> comEventCallbackInterface, final IComEventCallbackListener comEventCallbackListener) {
    assert COMUtils.comIsInitialized() : "COM not initialized";
    try {
        ComInterface comInterfaceAnnotation = comEventCallbackInterface.getAnnotation(ComInterface.class);
        if (null == comInterfaceAnnotation) {
            throw new COMException("advise: Interface must define a value for either iid via the ComInterface annotation");
        }
        final IID iid = this.getIID(comInterfaceAnnotation);
        final ConnectionPoint rawCp = this.fetchRawConnectionPoint(iid);
        // create the dispatch listener
        final IDispatchCallback rawListener = factory.createDispatchCallback(comEventCallbackInterface, comEventCallbackListener);
        // store it the comEventCallback argument, so it is not garbage
        // collected.
        comEventCallbackListener.setDispatchCallbackListener(rawListener);
        // set the dispatch listener to listen to events from the connection
        // point
        final DWORDByReference pdwCookie = new DWORDByReference();
        HRESULT hr = rawCp.Advise(rawListener, pdwCookie);
        // release before check in case check
        int n = rawCp.Release();
        // throws exception
        COMUtils.checkRC(hr);
        // return the cookie so that a call to stop listening can be made
        return new ComEventCallbackCookie(pdwCookie.getValue());
    } catch (Exception e) {
        throw new COMException("Error occured in advise when trying to connect the listener " + comEventCallbackListener, e);
    }
}
Also used : COMException(com.sun.jna.platform.win32.COM.COMException) HRESULT(com.sun.jna.platform.win32.WinNT.HRESULT) IID(com.sun.jna.platform.win32.Guid.IID) REFIID(com.sun.jna.platform.win32.Guid.REFIID) IDispatchCallback(com.sun.jna.platform.win32.COM.IDispatchCallback) DWORDByReference(com.sun.jna.platform.win32.WinDef.DWORDByReference) ComInterface(com.sun.jna.platform.win32.COM.util.annotation.ComInterface) ConnectionPoint(com.sun.jna.platform.win32.COM.ConnectionPoint) 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)

Example 3 with IID

use of com.sun.jna.platform.win32.Guid.IID in project jna by java-native-access.

the class ProxyObject method unadvise.

public void unadvise(Class<?> comEventCallbackInterface, final IComEventCallbackCookie cookie) {
    assert COMUtils.comIsInitialized() : "COM not initialized";
    try {
        ComInterface comInterfaceAnnotation = comEventCallbackInterface.getAnnotation(ComInterface.class);
        if (null == comInterfaceAnnotation) {
            throw new COMException("unadvise: Interface must define a value for iid via the ComInterface annotation");
        }
        IID iid = this.getIID(comInterfaceAnnotation);
        final ConnectionPoint rawCp = this.fetchRawConnectionPoint(iid);
        HRESULT hr = rawCp.Unadvise(((ComEventCallbackCookie) cookie).getValue());
        rawCp.Release();
        COMUtils.checkRC(hr);
    } catch (Exception e) {
        throw new COMException("Error occured in unadvise when trying to disconnect the listener from " + this, e);
    }
}
Also used : COMException(com.sun.jna.platform.win32.COM.COMException) HRESULT(com.sun.jna.platform.win32.WinNT.HRESULT) IID(com.sun.jna.platform.win32.Guid.IID) REFIID(com.sun.jna.platform.win32.Guid.REFIID) 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)

Example 4 with IID

use of com.sun.jna.platform.win32.Guid.IID in project jna by java-native-access.

the class ProxyObject method fetchRawConnectionPoint.

// ---------------------- IConnectionPoint ----------------------
private ConnectionPoint fetchRawConnectionPoint(IID iid) throws InterruptedException, ExecutionException, TimeoutException {
    assert COMUtils.comIsInitialized() : "COM not initialized";
    // query for ConnectionPointContainer
    IConnectionPointContainer cpc = this.queryInterface(IConnectionPointContainer.class);
    Dispatch rawCpcDispatch = (Dispatch) cpc.getRawDispatch();
    final ConnectionPointContainer rawCpc = new ConnectionPointContainer(rawCpcDispatch.getPointer());
    // find connection point for comEventCallback interface
    final REFIID adviseRiid = new REFIID(iid.getPointer());
    final PointerByReference ppCp = new PointerByReference();
    HRESULT hr = rawCpc.FindConnectionPoint(adviseRiid, ppCp);
    COMUtils.checkRC(hr);
    final ConnectionPoint rawCp = new ConnectionPoint(ppCp.getValue());
    return rawCp;
}
Also used : ConnectionPointContainer(com.sun.jna.platform.win32.COM.ConnectionPointContainer) HRESULT(com.sun.jna.platform.win32.WinNT.HRESULT) 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) ConnectionPoint(com.sun.jna.platform.win32.COM.ConnectionPoint)

Example 5 with IID

use of com.sun.jna.platform.win32.Guid.IID in project jna by java-native-access.

the class ProxyObject method getUnknownId.

private long getUnknownId() {
    assert COMUtils.comIsInitialized() : "COM not initialized";
    if (-1 == this.unknownId) {
        try {
            final PointerByReference ppvObject = new PointerByReference();
            Thread current = Thread.currentThread();
            String tn = current.getName();
            IID iid = com.sun.jna.platform.win32.COM.IUnknown.IID_IUNKNOWN;
            HRESULT hr = ProxyObject.this.getRawDispatch().QueryInterface(new REFIID(iid), ppvObject);
            if (WinNT.S_OK.equals(hr)) {
                Dispatch dispatch = new Dispatch(ppvObject.getValue());
                this.unknownId = Pointer.nativeValue(dispatch.getPointer());
                // QueryInterface returns a COM object pointer with a +1
                // reference, we must drop one,
                // Note: createProxy adds one;
                int n = dispatch.Release();
            } else {
                String formatMessageFromHR = Kernel32Util.formatMessage(hr);
                throw new COMException("getUnknownId: " + formatMessageFromHR);
            }
        } catch (Exception e) {
            throw new COMException("Error occured when trying get Unknown Id ", e);
        }
    }
    return this.unknownId;
}
Also used : HRESULT(com.sun.jna.platform.win32.WinNT.HRESULT) COMException(com.sun.jna.platform.win32.COM.COMException) 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) WString(com.sun.jna.WString) REFIID(com.sun.jna.platform.win32.Guid.REFIID) 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

REFIID (com.sun.jna.platform.win32.Guid.REFIID)10 IID (com.sun.jna.platform.win32.Guid.IID)9 HRESULT (com.sun.jna.platform.win32.WinNT.HRESULT)9 PointerByReference (com.sun.jna.ptr.PointerByReference)7 COMException (com.sun.jna.platform.win32.COM.COMException)5 ConnectionPoint (com.sun.jna.platform.win32.COM.ConnectionPoint)5 WString (com.sun.jna.WString)4 ComInterface (com.sun.jna.platform.win32.COM.util.annotation.ComInterface)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 ExecutionException (java.util.concurrent.ExecutionException)4 TimeoutException (java.util.concurrent.TimeoutException)4 Test (org.junit.Test)4 Dispatch (com.sun.jna.platform.win32.COM.Dispatch)3 IDispatch (com.sun.jna.platform.win32.COM.IDispatch)3 VARIANT (com.sun.jna.platform.win32.Variant.VARIANT)2 DWORDByReference (com.sun.jna.platform.win32.WinDef.DWORDByReference)2 ConnectionPointContainer (com.sun.jna.platform.win32.COM.ConnectionPointContainer)1 IDispatchCallback (com.sun.jna.platform.win32.COM.IDispatchCallback)1 DISPIDByReference (com.sun.jna.platform.win32.OaIdl.DISPIDByReference)1 EXCEPINFO (com.sun.jna.platform.win32.OaIdl.EXCEPINFO)1