use of com.sun.jna.platform.win32.COM.util.annotation.ComInterface in project jna by java-native-access.
the class ObjectFactory method fetchObject.
/**
* Gets and existing COM object (GetActiveObject) for the given progId and
* returns a ProxyObject for the given interface.
*/
public <T> T fetchObject(Class<T> comInterface) {
assert COMUtils.comIsInitialized() : "COM not initialized";
ComObject comObectAnnotation = comInterface.getAnnotation(ComObject.class);
if (null == comObectAnnotation) {
throw new COMException("createObject: Interface must define a value for either clsId or progId via the ComInterface annotation");
}
final GUID guid = this.discoverClsId(comObectAnnotation);
final PointerByReference ptrDisp = new PointerByReference();
WinNT.HRESULT hr = OleAuto.INSTANCE.GetActiveObject(guid, null, ptrDisp);
COMUtils.checkRC(hr);
Dispatch d = new Dispatch(ptrDisp.getValue());
T t = this.createProxy(comInterface, d);
//GetActiveObject returns a pointer to COM object with a +1 reference count, so we must drop one
//Note: the createProxy adds one
d.Release();
return t;
}
use of com.sun.jna.platform.win32.COM.util.annotation.ComInterface 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.annotation.ComInterface 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.annotation.ComInterface in project jna by java-native-access.
the class RunningObjectTable method getActiveObjectsByInterface.
@Override
public <T> List<T> getActiveObjectsByInterface(Class<T> comInterface) {
assert COMUtils.comIsInitialized() : "COM not initialized";
List<T> result = new ArrayList<T>();
for (IDispatch obj : this.enumRunning()) {
try {
T dobj = obj.queryInterface(comInterface);
result.add(dobj);
} catch (COMException ex) {
}
}
return result;
}
use of com.sun.jna.platform.win32.COM.util.annotation.ComInterface in project jna by java-native-access.
the class CallbackProxy method createRIID.
REFIID createRIID(Class<?> comEventCallbackInterface) {
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");
}
String iidStr = comInterfaceAnnotation.iid();
if (null == iidStr || iidStr.isEmpty()) {
throw new COMException("ComInterface must define a value for iid");
}
return new REFIID(new IID(iidStr).getPointer());
}
Aggregations