Search in sources :

Example 11 with Dispatch

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

use of com.sun.jna.platform.win32.COM.Dispatch 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)

Example 13 with Dispatch

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

the class EnumMoniker method iterator.

@Override
public Iterator<IDispatch> iterator() {
    return new Iterator<IDispatch>() {

        @Override
        public boolean hasNext() {
            return null != EnumMoniker.this.rawNext;
        }

        @Override
        public IDispatch next() {
            assert COMUtils.comIsInitialized() : "COM not initialized";
            final Moniker moniker = EnumMoniker.this.rawNext;
            final PointerByReference ppunkObject = new PointerByReference();
            WinNT.HRESULT hr = EnumMoniker.this.rawRot.GetObject(moniker.getPointer(), ppunkObject);
            COMUtils.checkRC(hr);
            // To assist debug, can use the following code
            // PointerByReference ppbc = new
            // PointerByReference();
            // Ole32.INSTANCE.CreateBindCtx(new DWORD(), ppbc);
            //
            // BSTRByReference ppszDisplayName = new
            // BSTRByReference();
            // hr = moniker.GetDisplayName(ppbc.getValue(),
            // moniker.getPointer(), ppszDisplayName);
            // COMUtils.checkRC(hr);
            // String name = ppszDisplayName.getString();
            // Ole32.INSTANCE.CoTaskMemFree(ppszDisplayName.getPointer().getPointer(0));
            // TODO: Can we assume that the object is an
            // IDispatch ?
            // Unknown unk = new
            // Unknown(ppunkObject.getValue());
            Dispatch dispatch = new Dispatch(ppunkObject.getValue());
            EnumMoniker.this.cacheNext();
            IDispatch d = EnumMoniker.this.factory.createProxy(IDispatch.class, dispatch);
            //must release a COM Ref, GetObject returns a pointer with +1
            int n = dispatch.Release();
            return d;
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException("remove");
        }
    };
}
Also used : IEnumMoniker(com.sun.jna.platform.win32.COM.IEnumMoniker) Moniker(com.sun.jna.platform.win32.COM.Moniker) PointerByReference(com.sun.jna.ptr.PointerByReference) Iterator(java.util.Iterator) WinNT(com.sun.jna.platform.win32.WinNT) Dispatch(com.sun.jna.platform.win32.COM.Dispatch)

Example 14 with Dispatch

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

the class Factory method createProxy.

@Override
public <T> T createProxy(Class<T> comInterface, IDispatch dispatch) {
    T result = super.createProxy(comInterface, dispatch);
    ProxyObject2 po2 = new ProxyObject2(result);
    Object proxy = Proxy.newProxyInstance(comInterface.getClassLoader(), new Class<?>[] { comInterface }, po2);
    return (T) proxy;
}
Also used : WinNT(com.sun.jna.platform.win32.WinNT) ComObject(com.sun.jna.platform.win32.COM.util.annotation.ComObject)

Example 15 with Dispatch

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

the class ComEventCallbacksObjectFactory_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)

Aggregations

HRESULT (com.sun.jna.platform.win32.WinNT.HRESULT)17 PointerByReference (com.sun.jna.ptr.PointerByReference)14 REFIID (com.sun.jna.platform.win32.Guid.REFIID)12 Dispatch (com.sun.jna.platform.win32.COM.Dispatch)7 COMException (com.sun.jna.platform.win32.COM.COMException)6 WinNT (com.sun.jna.platform.win32.WinNT)6 ConnectionPoint (com.sun.jna.platform.win32.COM.ConnectionPoint)5 IDispatch (com.sun.jna.platform.win32.COM.IDispatch)5 IID (com.sun.jna.platform.win32.Guid.IID)5 WString (com.sun.jna.WString)4 ComObject (com.sun.jna.platform.win32.COM.util.annotation.ComObject)4 DISPIDByReference (com.sun.jna.platform.win32.OaIdl.DISPIDByReference)4 VARIANT (com.sun.jna.platform.win32.Variant.VARIANT)4 WinDef (com.sun.jna.platform.win32.WinDef)3 IntByReference (com.sun.jna.ptr.IntByReference)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 ExecutionException (java.util.concurrent.ExecutionException)3 TimeoutException (java.util.concurrent.TimeoutException)3 ComInterface (com.sun.jna.platform.win32.COM.util.annotation.ComInterface)2 GUID (com.sun.jna.platform.win32.Guid.GUID)2