Search in sources :

Example 1 with ComInterface

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;
}
Also used : COMException(com.sun.jna.platform.win32.COM.COMException) HRESULT(com.sun.jna.platform.win32.WinNT.HRESULT) WinNT(com.sun.jna.platform.win32.WinNT) PointerByReference(com.sun.jna.ptr.PointerByReference) ComObject(com.sun.jna.platform.win32.COM.util.annotation.ComObject) GUID(com.sun.jna.platform.win32.Guid.GUID) WinNT(com.sun.jna.platform.win32.WinNT) Dispatch(com.sun.jna.platform.win32.COM.Dispatch) IDispatch(com.sun.jna.platform.win32.COM.IDispatch) HRESULT(com.sun.jna.platform.win32.WinNT.HRESULT)

Example 2 with ComInterface

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);
    }
}
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 3 with ComInterface

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);
    }
}
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 4 with ComInterface

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;
}
Also used : WinNT(com.sun.jna.platform.win32.WinNT) COMException(com.sun.jna.platform.win32.COM.COMException) ArrayList(java.util.ArrayList)

Example 5 with ComInterface

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());
}
Also used : COMException(com.sun.jna.platform.win32.COM.COMException) IID(com.sun.jna.platform.win32.Guid.IID) REFIID(com.sun.jna.platform.win32.Guid.REFIID) WString(com.sun.jna.WString) REFIID(com.sun.jna.platform.win32.Guid.REFIID) ComInterface(com.sun.jna.platform.win32.COM.util.annotation.ComInterface)

Aggregations

COMException (com.sun.jna.platform.win32.COM.COMException)7 WinNT (com.sun.jna.platform.win32.WinNT)6 HRESULT (com.sun.jna.platform.win32.WinNT.HRESULT)6 ComInterface (com.sun.jna.platform.win32.COM.util.annotation.ComInterface)4 ComObject (com.sun.jna.platform.win32.COM.util.annotation.ComObject)4 IID (com.sun.jna.platform.win32.Guid.IID)4 REFIID (com.sun.jna.platform.win32.Guid.REFIID)4 ConnectionPoint (com.sun.jna.platform.win32.COM.ConnectionPoint)3 Dispatch (com.sun.jna.platform.win32.COM.Dispatch)3 IDispatch (com.sun.jna.platform.win32.COM.IDispatch)3 PointerByReference (com.sun.jna.ptr.PointerByReference)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 ExecutionException (java.util.concurrent.ExecutionException)3 TimeoutException (java.util.concurrent.TimeoutException)3 WString (com.sun.jna.WString)2 GUID (com.sun.jna.platform.win32.Guid.GUID)2 IDispatchCallback (com.sun.jna.platform.win32.COM.IDispatchCallback)1 VARIANT (com.sun.jna.platform.win32.Variant.VARIANT)1 DWORDByReference (com.sun.jna.platform.win32.WinDef.DWORDByReference)1 ArrayList (java.util.ArrayList)1