Search in sources :

Example 6 with ComInterface

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

the class Factory method createProxy.

@Override
public <T> T createProxy(Class<T> comInterface, IDispatch dispatch) {
    T result = super.createProxy(comInterface, dispatch);
    ProxyObject2 po2 = new ProxyObject2(result);
    Object proxy = Proxy.newProxyInstance(comInterface.getClassLoader(), new Class<?>[] { comInterface }, po2);
    return (T) proxy;
}
Also used : WinNT(com.sun.jna.platform.win32.WinNT) ComObject(com.sun.jna.platform.win32.COM.util.annotation.ComObject)

Example 7 with ComInterface

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

the class ObjectFactory method createObject.

/**
	 * Creates a new COM object (CoCreateInstance) for the given progId and
	 * returns a ProxyObject for the given interface.
	 */
public <T> T createObject(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 = Ole32.INSTANCE.CoCreateInstance(guid, null, WTypes.CLSCTX_SERVER, IDispatch.IID_IDISPATCH, ptrDisp);
    COMUtils.checkRC(hr);
    Dispatch d = new Dispatch(ptrDisp.getValue());
    T t = this.createProxy(comInterface, d);
    //CoCreateInstance returns a pointer to COM object with a +1 reference count, so we must drop one
    //Note: the createProxy adds one
    int n = 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 8 with ComInterface

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

the class ObjectFactory method createProxy.

/**
	 * Creates a ProxyObject for the given interface and IDispatch pointer.
	 * 
	 */
public <T> T createProxy(Class<T> comInterface, IDispatch dispatch) {
    assert COMUtils.comIsInitialized() : "COM not initialized";
    ProxyObject jop = new ProxyObject(comInterface, dispatch, this);
    Object proxy = Proxy.newProxyInstance(comInterface.getClassLoader(), new Class<?>[] { comInterface }, jop);
    T result = comInterface.cast(proxy);
    return result;
}
Also used : HRESULT(com.sun.jna.platform.win32.WinNT.HRESULT) WinNT(com.sun.jna.platform.win32.WinNT) ComObject(com.sun.jna.platform.win32.COM.util.annotation.ComObject)

Example 9 with ComInterface

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

the class ProxyObject method queryInterface.

@Override
public <T> T queryInterface(Class<T> comInterface) throws COMException {
    assert COMUtils.comIsInitialized() : "COM not initialized";
    try {
        ComInterface comInterfaceAnnotation = comInterface.getAnnotation(ComInterface.class);
        if (null == comInterfaceAnnotation) {
            throw new COMException("queryInterface: Interface must define a value for iid via the ComInterface annotation");
        }
        final IID iid = this.getIID(comInterfaceAnnotation);
        final PointerByReference ppvObject = new PointerByReference();
        HRESULT hr = ProxyObject.this.getRawDispatch().QueryInterface(new REFIID(iid), ppvObject);
        if (WinNT.S_OK.equals(hr)) {
            Dispatch dispatch = new Dispatch(ppvObject.getValue());
            T t = this.factory.createProxy(comInterface, dispatch);
            // QueryInterface returns a COM object pointer with a +1
            // reference, we must drop one,
            // Note: createProxy adds one;
            int n = dispatch.Release();
            return t;
        } else {
            String formatMessageFromHR = Kernel32Util.formatMessage(hr);
            throw new COMException("queryInterface: " + formatMessageFromHR);
        }
    } catch (Exception e) {
        throw new COMException("Error occured when trying to query for interface " + comInterface.getName(), e);
    }
}
Also used : COMException(com.sun.jna.platform.win32.COM.COMException) HRESULT(com.sun.jna.platform.win32.WinNT.HRESULT) HRESULT(com.sun.jna.platform.win32.WinNT.HRESULT) WinNT(com.sun.jna.platform.win32.WinNT) VARIANT(com.sun.jna.platform.win32.Variant.VARIANT) IID(com.sun.jna.platform.win32.Guid.IID) REFIID(com.sun.jna.platform.win32.Guid.REFIID) PointerByReference(com.sun.jna.ptr.PointerByReference) Dispatch(com.sun.jna.platform.win32.COM.Dispatch) IDispatch(com.sun.jna.platform.win32.COM.IDispatch) REFIID(com.sun.jna.platform.win32.Guid.REFIID) WString(com.sun.jna.WString) 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)

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