Search in sources :

Example 1 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, 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 DISPID

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

the class HybdridCOMInvocationTest method testOfficeInvocationDemonstration.

public void testOfficeInvocationDemonstration() {
    // THIS IS NOT A TEST
    //
    // This reproduces the problem by using the dispatch directly.
    PointerByReference pDispatch = new PointerByReference();
    HRESULT hr = Ole32.INSTANCE.CoCreateInstance(CLSID_WORD, null, WTypes.CLSCTX_SERVER, IID_APPLICATION, pDispatch);
    if (!COMUtils.SUCCEEDED(hr)) {
        LOG.log(Level.INFO, "HybdridCOMInvocationTest test was not run, MS Word object could not be instantiated.");
        return;
    }
    Dispatch dp = new Dispatch(pDispatch.getValue());
    // DispID of InchesToPoints
    DISPID dispId = new OaIdl.DISPID(0x00000172);
    // Interface _Application of MS Word type library
    WinDef.LCID LOCALE_SYSTEM_DEFAULT = Kernel32.INSTANCE.GetSystemDefaultLCID();
    Variant.VARIANT.ByReference result = new Variant.VARIANT.ByReference();
    OaIdl.EXCEPINFO.ByReference pExcepInfo = new OaIdl.EXCEPINFO.ByReference();
    IntByReference puArgErr = new IntByReference();
    WORD wFlagsMethod = new WinDef.WORD(OleAuto.DISPATCH_METHOD);
    WORD wFlagsGet = new WinDef.WORD(OleAuto.DISPATCH_PROPERTYGET);
    WORD wFlagsCombined = new WinDef.WORD(OleAuto.DISPATCH_METHOD | OleAuto.DISPATCH_PROPERTYGET);
    OleAuto.DISPPARAMS.ByReference pDispParams = new OleAuto.DISPPARAMS.ByReference();
    VARIANT[] params = new VARIANT[] { new VARIANT(1f) };
    pDispParams.setArgs(params);
    // Call InchesToPoints as a method
    hr = dp.Invoke(dispId, new REFIID(Guid.IID_NULL), LOCALE_SYSTEM_DEFAULT, wFlagsMethod, pDispParams, result, pExcepInfo, puArgErr);
    assertTrue(COMUtils.FAILED(hr));
    // Call InchesToPoints as a property getter
    hr = dp.Invoke(dispId, new REFIID(Guid.IID_NULL), LOCALE_SYSTEM_DEFAULT, wFlagsGet, pDispParams, result, pExcepInfo, puArgErr);
    assertTrue(COMUtils.FAILED(hr));
    // Call InchesToPoints as a hybrid
    hr = dp.Invoke(dispId, new REFIID(Guid.IID_NULL), LOCALE_SYSTEM_DEFAULT, wFlagsCombined, pDispParams, result, pExcepInfo, puArgErr);
    assertTrue(COMUtils.SUCCEEDED(hr));
    assertEquals(72.0f, result.floatValue());
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) WORD(com.sun.jna.platform.win32.WinDef.WORD) HRESULT(com.sun.jna.platform.win32.WinNT.HRESULT) DISPID(com.sun.jna.platform.win32.OaIdl.DISPID) Dispatch(com.sun.jna.platform.win32.COM.Dispatch) REFIID(com.sun.jna.platform.win32.Guid.REFIID) PointerByReference(com.sun.jna.ptr.PointerByReference) IntByReference(com.sun.jna.ptr.IntByReference) VARIANT(com.sun.jna.platform.win32.Variant.VARIANT) Variant(com.sun.jna.platform.win32.Variant) OleAuto(com.sun.jna.platform.win32.OleAuto) PointerByReference(com.sun.jna.ptr.PointerByReference) WinDef(com.sun.jna.platform.win32.WinDef) OaIdl(com.sun.jna.platform.win32.OaIdl)

Example 3 with DISPID

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

the class ProxyObject method resolveDispId.

protected DISPID resolveDispId(final IDispatch pDisp, String name) {
    assert COMUtils.comIsInitialized() : "COM not initialized";
    if (pDisp == null)
        throw new COMException("pDisp (IDispatch) parameter is null!");
    // variable declaration
    final WString[] ptName = new WString[] { new WString(name) };
    final DISPIDByReference pdispID = new DISPIDByReference();
    // Get DISPID for name passed...
    HRESULT hr = pDisp.GetIDsOfNames(new REFIID(Guid.IID_NULL), ptName, 1, factory.getLCID(), pdispID);
    COMUtils.checkRC(hr);
    return pdispID.getValue();
}
Also used : WString(com.sun.jna.WString) COMException(com.sun.jna.platform.win32.COM.COMException) 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 4 with DISPID

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

the class ProxyObject method invokeMethod.

@Override
public <T> T invokeMethod(Class<T> returnType, DISPID dispID, Object... args) {
    assert COMUtils.comIsInitialized() : "COM not initialized";
    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_METHOD, 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 5 with DISPID

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

the class CallbackProxy method createDispIdMap.

Map<DISPID, Method> createDispIdMap(Class<?> comEventCallbackInterface) {
    Map<DISPID, Method> map = new HashMap<DISPID, Method>();
    for (Method meth : comEventCallbackInterface.getMethods()) {
        ComEventCallback annotation = meth.getAnnotation(ComEventCallback.class);
        if (null != annotation) {
            int dispId = annotation.dispid();
            if (-1 == dispId) {
                dispId = this.fetchDispIdFromName(annotation);
            }
            if (dispId == -1) {
                CallbackProxy.this.comEventCallbackListener.errorReceivingCallbackEvent("DISPID for " + meth.getName() + " not found", null);
            }
            map.put(new DISPID(dispId), meth);
        }
    }
    return map;
}
Also used : HashMap(java.util.HashMap) DISPID(com.sun.jna.platform.win32.OaIdl.DISPID) Method(java.lang.reflect.Method) ComEventCallback(com.sun.jna.platform.win32.COM.util.annotation.ComEventCallback)

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