Search in sources :

Example 1 with Dispatch

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

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

the class ComEventCallbacks_Test method before.

@Before
public void before() {
    AbstractWin32TestSupport.killProcessByName("iexplore.exe");
    try {
        Thread.sleep(5 * 1000);
    } catch (InterruptedException ex) {
    }
    HRESULT hr = Ole32.INSTANCE.CoInitializeEx(null, Ole32.COINIT_MULTITHREADED);
    COMUtils.checkRC(hr);
    // Create InternetExplorer object
    ieApp = new PointerByReference();
    hr = Ole32.INSTANCE.CoCreateInstance(CLSID_InternetExplorer, null, WTypes.CLSCTX_SERVER, IDispatch.IID_IDISPATCH, ieApp);
    COMUtils.checkRC(hr);
    ieDispatch = new Dispatch(ieApp.getValue());
    ieDispatch.AddRef();
    hr = ieDispatch.GetIDsOfNames(new REFIID(Guid.IID_NULL), new WString[] { new WString("Quit") }, 1, lcid, dispIdQuit);
    COMUtils.checkRC(hr);
    hr = ieDispatch.GetIDsOfNames(new REFIID(Guid.IID_NULL), new WString[] { new WString("Visible") }, 1, lcid, dispIdVisible);
    COMUtils.checkRC(hr);
    hr = ieDispatch.GetIDsOfNames(new REFIID(Guid.IID_NULL), new WString[] { new WString("Navigate") }, 1, lcid, dispIdNavigate);
    COMUtils.checkRC(hr);
}
Also used : WString(com.sun.jna.WString) HRESULT(com.sun.jna.platform.win32.WinNT.HRESULT) PointerByReference(com.sun.jna.ptr.PointerByReference) REFIID(com.sun.jna.platform.win32.Guid.REFIID) Before(org.junit.Before)

Example 3 with Dispatch

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

the class IDispatchTest method createIDispatch.

private Dispatch createIDispatch() {
    try {
        PointerByReference pDispatch = new PointerByReference();
        // Get CLSID for Shell.Application...
        CLSID.ByReference clsid = new CLSID.ByReference();
        HRESULT hr = Ole32.INSTANCE.CLSIDFromProgID("Shell.Application", clsid);
        if (W32Errors.FAILED(hr)) {
            Ole32.INSTANCE.CoUninitialize();
            COMUtils.checkRC(hr);
        }
        hr = Ole32.INSTANCE.CoCreateInstance(clsid, null, WTypes.CLSCTX_SERVER, IDispatch.IID_IDISPATCH, pDispatch);
        if (W32Errors.FAILED(hr)) {
            COMUtils.checkRC(hr);
        }
        return new Dispatch(pDispatch.getValue());
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
Also used : HRESULT(com.sun.jna.platform.win32.WinNT.HRESULT) CLSID(com.sun.jna.platform.win32.Guid.CLSID) PointerByReference(com.sun.jna.ptr.PointerByReference) UINTByReference(com.sun.jna.platform.win32.WinDef.UINTByReference) PointerByReference(com.sun.jna.ptr.PointerByReference) DISPIDByReference(com.sun.jna.platform.win32.OaIdl.DISPIDByReference)

Example 4 with Dispatch

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

the class IDispatchTest method testGetIDsOfNames.

public void testGetIDsOfNames() {
    Dispatch dispatch = this.createIDispatch();
    WString[] ptName = new WString[] { new WString("Application") };
    DISPIDByReference pdispID = new DISPIDByReference();
    HRESULT hr = dispatch.GetIDsOfNames(new REFIID(Guid.IID_NULL), ptName, 1, LOCALE_SYSTEM_DEFAULT, pdispID);
    COMUtils.checkRC(hr);
    assertEquals(0, hr.intValue());
}
Also used : WString(com.sun.jna.WString) HRESULT(com.sun.jna.platform.win32.WinNT.HRESULT) DISPIDByReference(com.sun.jna.platform.win32.OaIdl.DISPIDByReference) REFIID(com.sun.jna.platform.win32.Guid.REFIID)

Example 5 with Dispatch

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

the class COMBindingBaseObject method init.

private void init(boolean useActiveInstance, GUID clsid, int dwClsContext) throws COMException {
    HRESULT hr;
    if (useActiveInstance) {
        hr = OleAuto.INSTANCE.GetActiveObject(clsid, null, this.pUnknown);
        if (COMUtils.SUCCEEDED(hr)) {
            this.iUnknown = new Unknown(this.pUnknown.getValue());
            hr = iUnknown.QueryInterface(new REFIID(IDispatch.IID_IDISPATCH), this.pDispatch);
        } else {
            hr = Ole32.INSTANCE.CoCreateInstance(clsid, null, dwClsContext, IDispatch.IID_IDISPATCH, this.pDispatch);
        }
    } else {
        hr = Ole32.INSTANCE.CoCreateInstance(clsid, null, dwClsContext, IDispatch.IID_IDISPATCH, this.pDispatch);
    }
    COMUtils.checkRC(hr);
    this.iDispatch = new Dispatch(this.pDispatch.getValue());
}
Also used : HRESULT(com.sun.jna.platform.win32.WinNT.HRESULT) REFIID(com.sun.jna.platform.win32.Guid.REFIID)

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