Search in sources :

Example 1 with DISPPARAMS

use of com.sun.jna.platform.win32.OleAuto.DISPPARAMS in project jna by java-native-access.

the class COMBindingBaseObject method oleMethod.

protected HRESULT oleMethod(int nType, VARIANT.ByReference pvResult, IDispatch pDisp, DISPID dispId, VARIANT[] pArgs) throws COMException {
    if (pDisp == null)
        throw new COMException("pDisp (IDispatch) parameter is null!");
    // variable declaration
    int _argsLen = 0;
    VARIANT[] _args = null;
    DISPPARAMS.ByReference dp = new DISPPARAMS.ByReference();
    EXCEPINFO.ByReference pExcepInfo = new EXCEPINFO.ByReference();
    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 });
    }
    // Build DISPPARAMS
    if (_argsLen > 0) {
        dp.setArgs(_args);
        // write 'DISPPARAMS' structure to memory
        dp.write();
    }
    // 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;
    }
    // Make the call!
    HRESULT hr = pDisp.Invoke(dispId, new REFIID(Guid.IID_NULL), LOCALE_SYSTEM_DEFAULT, new WinDef.WORD(finalNType), dp, pvResult, pExcepInfo, puArgErr);
    COMUtils.checkRC(hr, pExcepInfo, puArgErr);
    return hr;
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) 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) PointerByReference(com.sun.jna.ptr.PointerByReference) DISPIDByReference(com.sun.jna.platform.win32.OaIdl.DISPIDByReference) IntByReference(com.sun.jna.ptr.IntByReference) WinDef(com.sun.jna.platform.win32.WinDef) DISPPARAMS(com.sun.jna.platform.win32.OleAuto.DISPPARAMS)

Example 2 with DISPPARAMS

use of com.sun.jna.platform.win32.OleAuto.DISPPARAMS 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 3 with DISPPARAMS

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

Aggregations

VARIANT (com.sun.jna.platform.win32.Variant.VARIANT)3 COMException (com.sun.jna.platform.win32.COM.COMException)2 REFIID (com.sun.jna.platform.win32.Guid.REFIID)2 DISPIDByReference (com.sun.jna.platform.win32.OaIdl.DISPIDByReference)2 EXCEPINFO (com.sun.jna.platform.win32.OaIdl.EXCEPINFO)2 DISPPARAMS (com.sun.jna.platform.win32.OleAuto.DISPPARAMS)2 WinDef (com.sun.jna.platform.win32.WinDef)2 HRESULT (com.sun.jna.platform.win32.WinNT.HRESULT)2 IntByReference (com.sun.jna.ptr.IntByReference)2 PointerByReference (com.sun.jna.ptr.PointerByReference)2 WString (com.sun.jna.WString)1 ConnectionPoint (com.sun.jna.platform.win32.COM.ConnectionPoint)1 DISPID (com.sun.jna.platform.win32.OaIdl.DISPID)1 Variant (com.sun.jna.platform.win32.Variant)1 DWORDByReference (com.sun.jna.platform.win32.WinDef.DWORDByReference)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1