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);
}
}
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);
}
}
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();
}
}
}
Aggregations