Search in sources :

Example 1 with ConnectionPoint

use of com.sun.jna.platform.win32.COM.ConnectionPoint 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 2 with ConnectionPoint

use of com.sun.jna.platform.win32.COM.ConnectionPoint 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 3 with ConnectionPoint

use of com.sun.jna.platform.win32.COM.ConnectionPoint 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 4 with ConnectionPoint

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

the class ComEventCallbacks_Test method testComEventCallback.

@Test
public void testComEventCallback() throws InterruptedException {
    VARIANT.ByReference pVarResult = new VARIANT.ByReference();
    IntByReference puArgErr = new IntByReference();
    EXCEPINFO.ByReference pExcepInfo = new EXCEPINFO.ByReference();
    HRESULT hr;
    DISPPARAMS.ByReference pDispParams;
    pDispParams = new DISPPARAMS.ByReference();
    pDispParams.setArgs(new VARIANT[] { new VARIANT(true) });
    pDispParams.setRgdispidNamedArgs(new DISPID[] { OaIdl.DISPID_PROPERTYPUT });
    // Visible-Prioperty
    hr = ieDispatch.Invoke(dispIdVisible.getValue(), niid, lcid, propertyPutFlags, pDispParams, null, null, null);
    COMUtils.checkRC(hr);
    // query for ConnectionPointContainer
    Unknown unk = new Unknown(ieApp.getValue());
    PointerByReference ppCpc = new PointerByReference();
    hr = unk.QueryInterface(new REFIID(IID_IConnectionPointContainer), ppCpc);
    COMUtils.checkRC(hr);
    ConnectionPointContainer cpc = new ConnectionPointContainer(ppCpc.getValue());
    // find connection point for DWebBrowserEvents2
    REFIID riid = new REFIID(IID_DWebBrowserEvents2);
    PointerByReference ppCp = new PointerByReference();
    hr = cpc.FindConnectionPoint(riid, ppCp);
    COMUtils.checkRC(hr);
    final ConnectionPoint cp = new ConnectionPoint(ppCp.getValue());
    IID cp_iid = new IID();
    hr = cp.GetConnectionInterface(cp_iid);
    COMUtils.checkRC(hr);
    final DWebBrowserEvents2_Listener listener = new DWebBrowserEvents2_Listener();
    final DWORDByReference pdwCookie = new DWORDByReference();
    HRESULT hr1 = cp.Advise(listener, pdwCookie);
    COMUtils.checkRC(hr1);
    // Advise make several callbacks into the object passed in - at this
    // point QueryInterface must have be called multiple times
    Assert.assertTrue(listener.QueryInterface_called);
    // Call Navigate with URL https://github.com/java-native-access/jna
    String navigateURL = "https://github.com/java-native-access/jna";
    String blockedURL = "http://www.google.de";
    VARIANT[] arguments = new VARIANT[] { new VARIANT(navigateURL) };
    pDispParams = new DISPPARAMS.ByReference();
    pDispParams.setArgs(arguments);
    hr = ieDispatch.Invoke(dispIdNavigate.getValue(), niid, lcid, methodFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
    COMUtils.checkRC(hr, pExcepInfo, puArgErr);
    for (int i = 0; i < 10; i++) {
        if (listener.navigateComplete2Called) {
            break;
        }
        Thread.sleep(1000);
    }
    // At this point the call to Navigate before should be complete
    Assert.assertTrue(listener.navigateComplete2Called);
    // Navidate complete should have brought us to github
    Assert.assertEquals(navigateURL, listener.navigateComplete2String);
    listener.navigateComplete2Called = false;
    listener.navigateComplete2String = null;
    listener.blockNavigation = true;
    arguments = new VARIANT[] { new VARIANT(blockedURL) };
    pDispParams = new DISPPARAMS.ByReference();
    pDispParams.setArgs(arguments);
    hr = ieDispatch.Invoke(dispIdNavigate.getValue(), niid, lcid, methodFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
    COMUtils.checkRC(hr, pExcepInfo, puArgErr);
    // wait 10 seconds to ensure navigation won't happen
    for (int i = 0; i < 10; i++) {
        if (listener.navigateComplete2Called) {
            break;
        }
        Thread.sleep(1000);
    }
    // Naviation will be blocked - so NavigateComplete can't be called
    Assert.assertFalse("NavigateComplete Handler was called although it should be blocked", listener.navigateComplete2Called);
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) HRESULT(com.sun.jna.platform.win32.WinNT.HRESULT) IID(com.sun.jna.platform.win32.Guid.IID) REFIID(com.sun.jna.platform.win32.Guid.REFIID) DWORDByReference(com.sun.jna.platform.win32.WinDef.DWORDByReference) EXCEPINFO(com.sun.jna.platform.win32.OaIdl.EXCEPINFO) REFIID(com.sun.jna.platform.win32.Guid.REFIID) WString(com.sun.jna.WString) DWORDByReference(com.sun.jna.platform.win32.WinDef.DWORDByReference) DISPIDByReference(com.sun.jna.platform.win32.OaIdl.DISPIDByReference) VARIANT_BOOLByReference(com.sun.jna.platform.win32.OaIdl.VARIANT_BOOLByReference) UINTByReference(com.sun.jna.platform.win32.WinDef.UINTByReference) PointerByReference(com.sun.jna.ptr.PointerByReference) IntByReference(com.sun.jna.ptr.IntByReference) VARIANT(com.sun.jna.platform.win32.Variant.VARIANT) PointerByReference(com.sun.jna.ptr.PointerByReference) DISPPARAMS(com.sun.jna.platform.win32.OleAuto.DISPPARAMS) Test(org.junit.Test)

Example 5 with ConnectionPoint

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

the class ComEventCallbacks_Test method GetConnectionInterface.

@Test
public void GetConnectionInterface() {
    // query for ConnectionPointContainer
    Unknown unk = new Unknown(this.ieApp.getValue());
    PointerByReference ppCpc = new PointerByReference();
    HRESULT hr = unk.QueryInterface(new REFIID(IID_IConnectionPointContainer), ppCpc);
    COMUtils.checkRC(hr);
    ConnectionPointContainer cpc = new ConnectionPointContainer(ppCpc.getValue());
    // find connection point for DWebBrowserEvents2
    REFIID riid = new REFIID(IID_DWebBrowserEvents2);
    PointerByReference ppCp = new PointerByReference();
    hr = cpc.FindConnectionPoint(riid, ppCp);
    COMUtils.checkRC(hr);
    ConnectionPoint cp = new ConnectionPoint(ppCp.getValue());
    IID cp_iid = new IID();
    hr = cp.GetConnectionInterface(cp_iid);
    COMUtils.checkRC(hr);
    Assert.assertEquals(IID_DWebBrowserEvents2, cp_iid);
}
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)

Aggregations

REFIID (com.sun.jna.platform.win32.Guid.REFIID)5 HRESULT (com.sun.jna.platform.win32.WinNT.HRESULT)5 IID (com.sun.jna.platform.win32.Guid.IID)4 ConnectionPoint (com.sun.jna.platform.win32.COM.ConnectionPoint)3 PointerByReference (com.sun.jna.ptr.PointerByReference)3 COMException (com.sun.jna.platform.win32.COM.COMException)2 ComInterface (com.sun.jna.platform.win32.COM.util.annotation.ComInterface)2 DWORDByReference (com.sun.jna.platform.win32.WinDef.DWORDByReference)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 ExecutionException (java.util.concurrent.ExecutionException)2 TimeoutException (java.util.concurrent.TimeoutException)2 Test (org.junit.Test)2 WString (com.sun.jna.WString)1 ConnectionPointContainer (com.sun.jna.platform.win32.COM.ConnectionPointContainer)1 Dispatch (com.sun.jna.platform.win32.COM.Dispatch)1 IDispatch (com.sun.jna.platform.win32.COM.IDispatch)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 VARIANT_BOOLByReference (com.sun.jna.platform.win32.OaIdl.VARIANT_BOOLByReference)1