Search in sources :

Example 11 with REFIID

use of com.sun.jna.platform.win32.Guid.REFIID in project jna by java-native-access.

the class ProxyObject method getUnknownId.

private long getUnknownId() {
    assert COMUtils.comIsInitialized() : "COM not initialized";
    if (-1 == this.unknownId) {
        try {
            final PointerByReference ppvObject = new PointerByReference();
            Thread current = Thread.currentThread();
            String tn = current.getName();
            IID iid = com.sun.jna.platform.win32.COM.IUnknown.IID_IUNKNOWN;
            HRESULT hr = ProxyObject.this.getRawDispatch().QueryInterface(new REFIID(iid), ppvObject);
            if (WinNT.S_OK.equals(hr)) {
                Dispatch dispatch = new Dispatch(ppvObject.getValue());
                this.unknownId = Pointer.nativeValue(dispatch.getPointer());
                // QueryInterface returns a COM object pointer with a +1
                // reference, we must drop one,
                // Note: createProxy adds one;
                int n = dispatch.Release();
            } else {
                String formatMessageFromHR = Kernel32Util.formatMessage(hr);
                throw new COMException("getUnknownId: " + formatMessageFromHR);
            }
        } catch (Exception e) {
            throw new COMException("Error occured when trying get Unknown Id ", e);
        }
    }
    return this.unknownId;
}
Also used : HRESULT(com.sun.jna.platform.win32.WinNT.HRESULT) COMException(com.sun.jna.platform.win32.COM.COMException) 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) WString(com.sun.jna.WString) REFIID(com.sun.jna.platform.win32.Guid.REFIID) 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 12 with REFIID

use of com.sun.jna.platform.win32.Guid.REFIID in project jna by java-native-access.

the class CallbackProxy method invokeOnThread.

void invokeOnThread(final DISPID dispIdMember, final REFIID riid, LCID lcid, WORD wFlags, final DISPPARAMS.ByReference pDispParams) {
    VARIANT[] arguments = pDispParams.getArgs();
    final Method eventMethod = CallbackProxy.this.dsipIdMap.get(dispIdMember);
    if (eventMethod == null) {
        CallbackProxy.this.comEventCallbackListener.errorReceivingCallbackEvent("No method found with dispId = " + dispIdMember, null);
        return;
    }
    /**
             * DISPPARAMs provides two different ways to pass arguments.
             *
             * Arguments can be passed as a linear list with all arguments
             * specified to a certain position (positional) or the position of
             * an argument can be passed via the rgdispidNamedArgs array
             * (named).
             *
             * pDispParams.rgvarg (length in pDispParams.cArgs) contains all
             * arguments (named + position based)
             *
             * pDispParams.rgdispidNamedArgs (length in pDispParams.cNamedArgs)
             * contains the named parameters as DISPIDs - the DISPIDs are the
             * target index in the method signature (zero based).
             *
             * Each entry in pDispParams.rgvarg is either position based or name
             * based and the position bases arguments are passed in reverse
             * order, so getting this:
             *
             * rgvarg = ["arg1", "arg2", "arg3", "arg4", "arg5"]
             * rgdispidNamedArgs = [3, 4]
             *
             * Would lead to this paramater array in the handler:
             *
             * ["arg5", "arg4", "arg3", "arg1", "arg2"]
             *
             * See also:
             * https://msdn.microsoft.com/de-de/library/windows/desktop/ms221653%28v=vs.85%29.aspx
             */
    // Arguments are converted to the JAVA side and IDispatch Interfaces
    // are wrapped into an ProxyObject if so requested.
    //
    // Out-Parameter need to be specified as VARIANT, VARIANT args are
    // not converted, so COM memory allocation rules apply.
    DISPID[] positionMap = pDispParams.getRgdispidNamedArgs();
    final Class<?>[] paramTypes = eventMethod.getParameterTypes();
    final Object[] params = new Object[paramTypes.length];
    // Handle position based parameters first
    for (int i = 0; i < params.length && (arguments.length - positionMap.length - i) > 0; i++) {
        Class targetClass = paramTypes[i];
        Variant.VARIANT varg = arguments[arguments.length - i - 1];
        params[i] = Convert.toJavaObject(varg, targetClass, factory, true, false);
    }
    for (int i = 0; i < positionMap.length; i++) {
        int targetPosition = positionMap[i].intValue();
        if (targetPosition >= params.length) {
            // If less parameters are mapped then supplied, ignore
            continue;
        }
        Class targetClass = paramTypes[targetPosition];
        Variant.VARIANT varg = arguments[i];
        params[targetPosition] = Convert.toJavaObject(varg, targetClass, factory, true, false);
    }
    // exception occurs while doing the call into the target method
    for (int i = 0; i < params.length; i++) {
        if (params[i] == null && paramTypes[i].isPrimitive()) {
            if (paramTypes[i].equals(boolean.class)) {
                params[i] = DEFAULT_BOOLEAN;
            } else if (paramTypes[i].equals(byte.class)) {
                params[i] = DEFAULT_BYTE;
            } else if (paramTypes[i].equals(short.class)) {
                params[i] = DEFAULT_SHORT;
            } else if (paramTypes[i].equals(int.class)) {
                params[i] = DEFAULT_INT;
            } else if (paramTypes[i].equals(long.class)) {
                params[i] = DEFAULT_LONG;
            } else if (paramTypes[i].equals(float.class)) {
                params[i] = DEFAULT_FLOAT;
            } else if (paramTypes[i].equals(double.class)) {
                params[i] = DEFAULT_DOUBLE;
            } else {
                throw new IllegalArgumentException("Class type " + paramTypes[i].getName() + " not mapped to primitive default value.");
            }
        }
    }
    try {
        eventMethod.invoke(comEventCallbackListener, params);
    } catch (Exception e) {
        List<String> decodedClassNames = new ArrayList<String>(params.length);
        for (Object o : params) {
            if (o == null) {
                decodedClassNames.add("NULL");
            } else {
                decodedClassNames.add(o.getClass().getName());
            }
        }
        CallbackProxy.this.comEventCallbackListener.errorReceivingCallbackEvent("Exception invoking method " + eventMethod + " supplied: " + decodedClassNames.toString(), e);
    }
}
Also used : DISPID(com.sun.jna.platform.win32.OaIdl.DISPID) Method(java.lang.reflect.Method) WString(com.sun.jna.WString) VARIANT(com.sun.jna.platform.win32.Variant.VARIANT) COMException(com.sun.jna.platform.win32.COM.COMException) VARIANT(com.sun.jna.platform.win32.Variant.VARIANT) Variant(com.sun.jna.platform.win32.Variant) ArrayList(java.util.ArrayList) List(java.util.List)

Example 13 with REFIID

use of com.sun.jna.platform.win32.Guid.REFIID 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)

Example 14 with REFIID

use of com.sun.jna.platform.win32.Guid.REFIID in project jna by java-native-access.

the class ComEventCallbacksObjectFactory_Test method testComEventCallback.

@Test
public void testComEventCallback() {
    DWebBrowserEvents2_Listener listener = new DWebBrowserEvents2_Listener();
    CallbackProxy proxy = new CallbackProxy(factory, DWebBrowserEvents2.class, listener);
    REFIID refiid = new REFIID(new IID(DWebBrowserEvents2.IID));
    // precondition: the structures for the listenedToRiid and
    // refiid have to be different (else the PointerType#equals would
    // be enough
    assertFalse(proxy.listenedToRiid.getPointer().equals(refiid.getPointer()));
    // Neverthe less, the QueryInterface method has to return the
    // correct pointer (the IID is relevant, not its wrapper
    PointerByReference interfacePointer = new PointerByReference();
    // Check the "business" interface
    HRESULT hr = proxy.QueryInterface(refiid, interfacePointer);
    assertTrue(COMUtils.SUCCEEDED(hr));
    assertEquals(interfacePointer.getValue(), proxy.getPointer());
    // IUnknown must be implemented
    hr = proxy.QueryInterface(new REFIID(IID_IUNKNOWN), interfacePointer);
    assertTrue(COMUtils.SUCCEEDED(hr));
    assertEquals(interfacePointer.getValue(), proxy.getPointer());
    // Currently only Dispatch based callbacks are supported,
    // so this interface must be present to
    hr = proxy.QueryInterface(new REFIID(IID_IDISPATCH), interfacePointer);
    assertTrue(COMUtils.SUCCEEDED(hr));
    assertEquals(interfacePointer.getValue(), proxy.getPointer());
    // Negative check -- this has to fail, the IID should not be
    // assigned
    hr = proxy.QueryInterface(new REFIID(new IID("{00000000-0000-0000-C000-000000000000}")), interfacePointer);
    assertTrue(COMUtils.FAILED(hr));
}
Also used : HRESULT(com.sun.jna.platform.win32.WinNT.HRESULT) IID(com.sun.jna.platform.win32.Guid.IID) REFIID(com.sun.jna.platform.win32.Guid.REFIID) PointerByReference(com.sun.jna.ptr.PointerByReference) REFIID(com.sun.jna.platform.win32.Guid.REFIID) Test(org.junit.Test)

Example 15 with REFIID

use of com.sun.jna.platform.win32.Guid.REFIID in project jna by java-native-access.

the class COMBindingBaseObject method oleMethod.

protected HRESULT oleMethod(int nType, VARIANT.ByReference pvResult, IDispatch pDisp, String name, VARIANT[] pArgs) throws COMException {
    if (pDisp == null)
        throw new COMException("pDisp (IDispatch) parameter is null!");
    // variable declaration
    WString[] ptName = new WString[] { new WString(name) };
    DISPIDByReference pdispID = new DISPIDByReference();
    // Get DISPID for name passed...
    HRESULT hr = pDisp.GetIDsOfNames(new REFIID(Guid.IID_NULL), ptName, 1, LOCALE_USER_DEFAULT, pdispID);
    COMUtils.checkRC(hr);
    return this.oleMethod(nType, pvResult, pDisp, pdispID.getValue(), pArgs);
}
Also used : WString(com.sun.jna.WString) HRESULT(com.sun.jna.platform.win32.WinNT.HRESULT) DISPIDByReference(com.sun.jna.platform.win32.OaIdl.DISPIDByReference) REFIID(com.sun.jna.platform.win32.Guid.REFIID)

Aggregations

REFIID (com.sun.jna.platform.win32.Guid.REFIID)20 HRESULT (com.sun.jna.platform.win32.WinNT.HRESULT)19 PointerByReference (com.sun.jna.ptr.PointerByReference)16 WString (com.sun.jna.WString)9 IID (com.sun.jna.platform.win32.Guid.IID)7 Test (org.junit.Test)7 COMException (com.sun.jna.platform.win32.COM.COMException)6 DISPIDByReference (com.sun.jna.platform.win32.OaIdl.DISPIDByReference)6 VARIANT (com.sun.jna.platform.win32.Variant.VARIANT)6 ConnectionPoint (com.sun.jna.platform.win32.COM.ConnectionPoint)4 Dispatch (com.sun.jna.platform.win32.COM.Dispatch)4 IntByReference (com.sun.jna.ptr.IntByReference)4 IDispatch (com.sun.jna.platform.win32.COM.IDispatch)3 EXCEPINFO (com.sun.jna.platform.win32.OaIdl.EXCEPINFO)3 DISPPARAMS (com.sun.jna.platform.win32.OleAuto.DISPPARAMS)3 WinDef (com.sun.jna.platform.win32.WinDef)3 ComInterface (com.sun.jna.platform.win32.COM.util.annotation.ComInterface)2 DISPID (com.sun.jna.platform.win32.OaIdl.DISPID)2 Variant (com.sun.jna.platform.win32.Variant)2 DWORDByReference (com.sun.jna.platform.win32.WinDef.DWORDByReference)2