Search in sources :

Example 6 with DISPID

use of com.sun.jna.platform.win32.OaIdl.DISPID 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 7 with DISPID

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

Example 8 with DISPID

use of com.sun.jna.platform.win32.OaIdl.DISPID in project jna by java-native-access.

the class ProxyObject method oleMethod.

/*
	 * @see com.sun.jna.platform.win32.COM.COMBindingBaseObject#oleMethod
	 */
protected HRESULT oleMethod(final int nType, final VARIANT.ByReference pvResult, final IDispatch pDisp, final DISPID dispId, VARIANT[] pArgs) throws COMException {
    assert COMUtils.comIsInitialized() : "COM not initialized";
    if (pDisp == null)
        throw new COMException("pDisp (IDispatch) parameter is null!");
    // variable declaration
    int _argsLen = 0;
    VARIANT[] _args = null;
    final DISPPARAMS.ByReference dp = new DISPPARAMS.ByReference();
    final EXCEPINFO.ByReference pExcepInfo = new EXCEPINFO.ByReference();
    final IntByReference puArgErr = new IntByReference();
    // make parameter reverse ordering as expected by COM runtime
    if ((pArgs != null) && (pArgs.length > 0)) {
        _argsLen = pArgs.length;
        _args = new VARIANT[_argsLen];
        int revCount = _argsLen;
        for (int i = 0; i < _argsLen; i++) {
            _args[i] = pArgs[--revCount];
        }
    }
    // Handle special-case for property-puts!
    if (nType == OleAuto.DISPATCH_PROPERTYPUT) {
        dp.setRgdispidNamedArgs(new DISPID[] { OaIdl.DISPID_PROPERTYPUT });
    }
    // Apply "fix" according to
    // https://www.delphitools.info/2013/04/30/gaining-visual-basic-ole-super-powers/
    // https://msdn.microsoft.com/en-us/library/windows/desktop/ms221486(v=vs.85).aspx
    //
    // Summary: there are methods in the word typelibrary that require both
    // PROPERTYGET _and_ METHOD to be set. With only one of these set the call
    // fails.
    //
    // The article from delphitools argues, that automation compatible libraries
    // need to be compatible with VisualBasic which does not distingish methods
    // and property getters and will set both flags always.
    //
    // The MSDN article advises this behaviour: "[...] Some languages cannot 
    // distinguish between retrieving a property and calling a method. In this 
    //case, you should set the flags DISPATCH_PROPERTYGET and DISPATCH_METHOD.
    // [...]"))
    //
    // This was found when trying to bind InchesToPoints from the _Application 
    // dispatch interface of the MS Word 15 type library
    //
    // The signature according the ITypeLib Viewer (OLE/COM Object Viewer):
    // [id(0x00000172), helpcontext(0x09700172)]
    // single InchesToPoints([in] single Inches);
    final int finalNType;
    if (nType == OleAuto.DISPATCH_METHOD || nType == OleAuto.DISPATCH_PROPERTYGET) {
        finalNType = OleAuto.DISPATCH_METHOD | OleAuto.DISPATCH_PROPERTYGET;
    } else {
        finalNType = nType;
    }
    // Build DISPPARAMS
    if (_argsLen > 0) {
        dp.setArgs(_args);
        // write 'DISPPARAMS' structure to memory
        dp.write();
    }
    HRESULT hr = pDisp.Invoke(dispId, new REFIID(Guid.IID_NULL), factory.getLCID(), new WinDef.WORD(finalNType), dp, pvResult, pExcepInfo, puArgErr);
    COMUtils.checkRC(hr, pExcepInfo, puArgErr);
    return hr;
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) COMException(com.sun.jna.platform.win32.COM.COMException) HRESULT(com.sun.jna.platform.win32.WinNT.HRESULT) EXCEPINFO(com.sun.jna.platform.win32.OaIdl.EXCEPINFO) REFIID(com.sun.jna.platform.win32.Guid.REFIID) VARIANT(com.sun.jna.platform.win32.Variant.VARIANT) DWORDByReference(com.sun.jna.platform.win32.WinDef.DWORDByReference) DISPIDByReference(com.sun.jna.platform.win32.OaIdl.DISPIDByReference) PointerByReference(com.sun.jna.ptr.PointerByReference) IntByReference(com.sun.jna.ptr.IntByReference) ConnectionPoint(com.sun.jna.platform.win32.COM.ConnectionPoint) WinDef(com.sun.jna.platform.win32.WinDef) DISPPARAMS(com.sun.jna.platform.win32.OleAuto.DISPPARAMS)

Example 9 with DISPID

use of com.sun.jna.platform.win32.OaIdl.DISPID in project jna by java-native-access.

the class ProxyObject method getProperty.

@Override
public <T> T getProperty(Class<T> returnType, DISPID dispID, Object... args) {
    VARIANT[] vargs;
    if (null == args) {
        vargs = new VARIANT[0];
    } else {
        vargs = new VARIANT[args.length];
    }
    for (int i = 0; i < vargs.length; ++i) {
        vargs[i] = Convert.toVariant(args[i]);
    }
    Variant.VARIANT.ByReference result = new Variant.VARIANT.ByReference();
    WinNT.HRESULT hr = this.oleMethod(OleAuto.DISPATCH_PROPERTYGET, result, this.getRawDispatch(), dispID, vargs);
    for (int i = 0; i < vargs.length; i++) {
        // Free value allocated by Convert#toVariant
        Convert.free(vargs[i], args[i]);
    }
    COMUtils.checkRC(hr);
    return (T) Convert.toJavaObject(result, returnType, factory, false, true);
}
Also used : Variant(com.sun.jna.platform.win32.Variant) HRESULT(com.sun.jna.platform.win32.WinNT.HRESULT) WinNT(com.sun.jna.platform.win32.WinNT) VARIANT(com.sun.jna.platform.win32.Variant.VARIANT) WinNT(com.sun.jna.platform.win32.WinNT) HRESULT(com.sun.jna.platform.win32.WinNT.HRESULT) VARIANT(com.sun.jna.platform.win32.Variant.VARIANT) DWORDByReference(com.sun.jna.platform.win32.WinDef.DWORDByReference) DISPIDByReference(com.sun.jna.platform.win32.OaIdl.DISPIDByReference) PointerByReference(com.sun.jna.ptr.PointerByReference) IntByReference(com.sun.jna.ptr.IntByReference) ConnectionPoint(com.sun.jna.platform.win32.COM.ConnectionPoint)

Example 10 with DISPID

use of com.sun.jna.platform.win32.OaIdl.DISPID in project jna by java-native-access.

the class ProxyObject method invoke.

// --------------------- InvocationHandler -----------------------------
@Override
public Object invoke(final Object proxy, final java.lang.reflect.Method method, final Object[] args) throws Throwable {
    boolean declaredAsInterface = (method.getAnnotation(ComMethod.class) != null) || (method.getAnnotation(ComProperty.class) != null);
    if ((!declaredAsInterface) && (method.getDeclaringClass().equals(Object.class) || method.getDeclaringClass().equals(IRawDispatchHandle.class) || method.getDeclaringClass().equals(com.sun.jna.platform.win32.COM.util.IUnknown.class) || method.getDeclaringClass().equals(com.sun.jna.platform.win32.COM.util.IDispatch.class) || method.getDeclaringClass().equals(IConnectionPoint.class))) {
        try {
            return method.invoke(this, args);
        } catch (InvocationTargetException ex) {
            throw ex.getCause();
        }
    }
    Class<?> returnType = method.getReturnType();
    boolean isVoid = Void.TYPE.equals(returnType);
    ComProperty prop = method.getAnnotation(ComProperty.class);
    if (null != prop) {
        int dispId = prop.dispId();
        if (isVoid) {
            if (dispId != -1) {
                this.setProperty(new DISPID(dispId), args[0]);
                return null;
            } else {
                String propName = this.getMutatorName(method, prop);
                this.setProperty(propName, args[0]);
                return null;
            }
        } else {
            if (dispId != -1) {
                return this.getProperty(returnType, new DISPID(dispId), args);
            } else {
                String propName = this.getAccessorName(method, prop);
                return this.getProperty(returnType, propName, args);
            }
        }
    }
    ComMethod meth = method.getAnnotation(ComMethod.class);
    if (null != meth) {
        Object[] fullLengthArgs = unfoldWhenVarargs(method, args);
        int dispId = meth.dispId();
        if (dispId != -1) {
            return this.invokeMethod(returnType, new DISPID(dispId), fullLengthArgs);
        } else {
            String methName = this.getMethodName(method, meth);
            return this.invokeMethod(returnType, methName, fullLengthArgs);
        }
    }
    return null;
}
Also used : ComProperty(com.sun.jna.platform.win32.COM.util.annotation.ComProperty) DISPID(com.sun.jna.platform.win32.OaIdl.DISPID) WString(com.sun.jna.WString) InvocationTargetException(java.lang.reflect.InvocationTargetException) ConnectionPoint(com.sun.jna.platform.win32.COM.ConnectionPoint) ComMethod(com.sun.jna.platform.win32.COM.util.annotation.ComMethod)

Aggregations

HRESULT (com.sun.jna.platform.win32.WinNT.HRESULT)9 VARIANT (com.sun.jna.platform.win32.Variant.VARIANT)8 DISPIDByReference (com.sun.jna.platform.win32.OaIdl.DISPIDByReference)7 REFIID (com.sun.jna.platform.win32.Guid.REFIID)6 DISPID (com.sun.jna.platform.win32.OaIdl.DISPID)6 IntByReference (com.sun.jna.ptr.IntByReference)6 PointerByReference (com.sun.jna.ptr.PointerByReference)6 WString (com.sun.jna.WString)5 ConnectionPoint (com.sun.jna.platform.win32.COM.ConnectionPoint)4 Variant (com.sun.jna.platform.win32.Variant)4 DWORDByReference (com.sun.jna.platform.win32.WinDef.DWORDByReference)4 COMException (com.sun.jna.platform.win32.COM.COMException)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 WinNT (com.sun.jna.platform.win32.WinNT)3 Method (java.lang.reflect.Method)2 Test (org.junit.Test)2 Dispatch (com.sun.jna.platform.win32.COM.Dispatch)1 ComEventCallback (com.sun.jna.platform.win32.COM.util.annotation.ComEventCallback)1