Search in sources :

Example 1 with IComEventCallbackCookie

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

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

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

the class MSOfficeExcelDemo method testExcel.

public static void testExcel() throws IOException {
    File demoDocument = null;
    ComIApplication msExcel = null;
    Factory factory = new Factory();
    try {
        System.out.println("Files in temp dir: " + Helper.tempDir.getAbsolutePath());
        ComExcel_Application excelObject = factory.createObject(ComExcel_Application.class);
        msExcel = excelObject.queryInterface(ComIApplication.class);
        System.out.println("MSExcel version: " + msExcel.getVersion());
        msExcel.setVisible(true);
        Helper.sleep(5);
        demoDocument = Helper.createNotExistingFile("jnatest", ".xls");
        Helper.extractClasspathFileToReal("/com/sun/jna/platform/win32/COM/util/office/resources/jnatest.xls", demoDocument);
        ComIWorkbook workbook = msExcel.getWorkbooks().Open(demoDocument.getAbsolutePath());
        msExcel.getActiveSheet().getRange("A1").setValue("Hello from JNA!");
        // wait 1sec. before closing
        Helper.sleep(1);
        // Save document into temp and close
        File output = new File(Helper.tempDir, "jnatest.xls");
        output.delete();
        workbook.SaveAs(output.getAbsolutePath());
        msExcel.getActiveWorkbook().Close(false);
        //			// msExcel.newExcelBook();
        msExcel.getWorkbooks().Open(output.getAbsolutePath());
        msExcel.getActiveSheet().getRange("A2").setValue("Hello again from JNA!");
        class Listener extends AbstractComEventCallbackListener implements ComIAppEvents {

            volatile boolean SheetSelectionChange_called;

            @Override
            public void errorReceivingCallbackEvent(String message, Exception exception) {
            }

            @Override
            public void SheetSelectionChange(ComIWorksheet sheet, ComIRange target) {
                SheetSelectionChange_called = true;
            }
        }
        ;
        Listener listener = new Listener();
        IComEventCallbackCookie cookie = msExcel.advise(ComIAppEvents.class, listener);
        Helper.sleep(1);
        msExcel.getActiveSheet().getRange("A5").Activate();
        Helper.sleep(1);
        msExcel.unadvise(ComIAppEvents.class, cookie);
        System.out.println("Listener was fired: " + listener.SheetSelectionChange_called);
        // close and discard changes
        msExcel.getActiveWorkbook().Close(false);
    } finally {
        // Make sure the excel instance is shut down
        if (null != msExcel) {
            msExcel.Quit();
        }
        // Release all objects acquired by the factory
        factory.disposeAll();
        if (demoDocument != null && demoDocument.exists()) {
            demoDocument.delete();
        }
    }
}
Also used : ComIRange(com.sun.jna.platform.win32.COM.util.office.excel.ComIRange) AbstractComEventCallbackListener(com.sun.jna.platform.win32.COM.util.AbstractComEventCallbackListener) ComIWorkbook(com.sun.jna.platform.win32.COM.util.office.excel.ComIWorkbook) AbstractComEventCallbackListener(com.sun.jna.platform.win32.COM.util.AbstractComEventCallbackListener) Factory(com.sun.jna.platform.win32.COM.util.Factory) IOException(java.io.IOException) IComEventCallbackCookie(com.sun.jna.platform.win32.COM.util.IComEventCallbackCookie) ComIWorksheet(com.sun.jna.platform.win32.COM.util.office.excel.ComIWorksheet) ComExcel_Application(com.sun.jna.platform.win32.COM.util.office.excel.ComExcel_Application) File(java.io.File) ComIApplication(com.sun.jna.platform.win32.COM.util.office.excel.ComIApplication) ComIAppEvents(com.sun.jna.platform.win32.COM.util.office.excel.ComIAppEvents)

Aggregations

COMException (com.sun.jna.platform.win32.COM.COMException)2 ConnectionPoint (com.sun.jna.platform.win32.COM.ConnectionPoint)2 ComInterface (com.sun.jna.platform.win32.COM.util.annotation.ComInterface)2 IID (com.sun.jna.platform.win32.Guid.IID)2 REFIID (com.sun.jna.platform.win32.Guid.REFIID)2 HRESULT (com.sun.jna.platform.win32.WinNT.HRESULT)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 ExecutionException (java.util.concurrent.ExecutionException)2 TimeoutException (java.util.concurrent.TimeoutException)2 IDispatchCallback (com.sun.jna.platform.win32.COM.IDispatchCallback)1 AbstractComEventCallbackListener (com.sun.jna.platform.win32.COM.util.AbstractComEventCallbackListener)1 Factory (com.sun.jna.platform.win32.COM.util.Factory)1 IComEventCallbackCookie (com.sun.jna.platform.win32.COM.util.IComEventCallbackCookie)1 ComExcel_Application (com.sun.jna.platform.win32.COM.util.office.excel.ComExcel_Application)1 ComIAppEvents (com.sun.jna.platform.win32.COM.util.office.excel.ComIAppEvents)1 ComIApplication (com.sun.jna.platform.win32.COM.util.office.excel.ComIApplication)1 ComIRange (com.sun.jna.platform.win32.COM.util.office.excel.ComIRange)1 ComIWorkbook (com.sun.jna.platform.win32.COM.util.office.excel.ComIWorkbook)1 ComIWorksheet (com.sun.jna.platform.win32.COM.util.office.excel.ComIWorksheet)1 DWORDByReference (com.sun.jna.platform.win32.WinDef.DWORDByReference)1