Search in sources :

Example 11 with VARIANT

use of com.sun.jna.platform.win32.Variant.VARIANT 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 12 with VARIANT

use of com.sun.jna.platform.win32.Variant.VARIANT in project jna by java-native-access.

the class MSWord method insertText.

public void insertText(String text) throws COMException {
    Selection pSelection = new Selection(this.getAutomationProperty("Selection", this.getIDispatch()));
    this.invokeNoReply("TypeText", pSelection, new VARIANT(text));
}
Also used : VARIANT(com.sun.jna.platform.win32.Variant.VARIANT)

Example 13 with VARIANT

use of com.sun.jna.platform.win32.Variant.VARIANT in project jna by java-native-access.

the class MSWord method Save.

public void Save(boolean bNoPrompt, LONG originalFormat) throws COMException {
    VARIANT vtNoPrompt = new VARIANT(bNoPrompt);
    VARIANT vtOriginalFormat = new VARIANT(originalFormat);
    this.invokeNoReply("Save", this.getDocuments(), vtNoPrompt, vtOriginalFormat);
}
Also used : VARIANT(com.sun.jna.platform.win32.Variant.VARIANT)

Example 14 with VARIANT

use of com.sun.jna.platform.win32.Variant.VARIANT in project jna by java-native-access.

the class Excelautomation_KB_219151_Mod method safeVariantArrayFromJava.

private static SAFEARRAY safeVariantArrayFromJava(String[][] data) {
    // The data array is defined/stored row major, while excel expects the
    // data column major, so this method also transposes the matrix
    OaIdl.SAFEARRAY wrapped = OaIdl.SAFEARRAY.createSafeArray(data[0].length, data.length);
    // VARIANT is java allocated and will be freed by GC
    VARIANT var = new VARIANT();
    for (int i = 0; i < data.length; i++) {
        for (int j = 0; j < data[0].length; j++) {
            // BSTR is allocated by java and will be freed by GC
            var.setValue(Variant.VT_BSTR, new BSTR(data[i][j]));
            wrapped.putElement(var, j, i);
        }
    }
    return wrapped;
}
Also used : BSTR(com.sun.jna.platform.win32.WTypes.BSTR) OaIdl(com.sun.jna.platform.win32.OaIdl) SAFEARRAY(com.sun.jna.platform.win32.OaIdl.SAFEARRAY) VARIANT(com.sun.jna.platform.win32.Variant.VARIANT)

Example 15 with VARIANT

use of com.sun.jna.platform.win32.Variant.VARIANT in project jna by java-native-access.

the class IFileSystem3 method testConvertEnum.

@Test
public void testConvertEnum() {
    TestEnum testEnum = TestEnum.Val2;
    VARIANT resultEnum = Convert.toVariant(testEnum);
    assertEquals((int) testEnum.getValue(), resultEnum.intValue());
    assertEquals((int) testEnum.getValue(), Convert.toJavaObject(resultEnum, Object.class, fact, false, false));
    assertEquals(testEnum, Convert.toJavaObject(resultEnum, TestEnum.class, fact, false, false));
}
Also used : ComObject(com.sun.jna.platform.win32.COM.util.annotation.ComObject) VARIANT(com.sun.jna.platform.win32.Variant.VARIANT) Test(org.junit.Test)

Aggregations

VARIANT (com.sun.jna.platform.win32.Variant.VARIANT)37 HRESULT (com.sun.jna.platform.win32.WinNT.HRESULT)12 ComObject (com.sun.jna.platform.win32.COM.util.annotation.ComObject)10 Test (org.junit.Test)10 DISPIDByReference (com.sun.jna.platform.win32.OaIdl.DISPIDByReference)6 SAFEARRAY (com.sun.jna.platform.win32.OaIdl.SAFEARRAY)6 IntByReference (com.sun.jna.ptr.IntByReference)6 PointerByReference (com.sun.jna.ptr.PointerByReference)6 REFIID (com.sun.jna.platform.win32.Guid.REFIID)5 DATE (com.sun.jna.platform.win32.OaIdl.DATE)5 BSTR (com.sun.jna.platform.win32.WTypes.BSTR)5 SHORT (com.sun.jna.platform.win32.WinDef.SHORT)5 IConnectionPoint (com.sun.jna.platform.win32.COM.util.IConnectionPoint)4 Variant (com.sun.jna.platform.win32.Variant)4 VT_VARIANT (com.sun.jna.platform.win32.Variant.VT_VARIANT)4 WinDef (com.sun.jna.platform.win32.WinDef)4 Pointer (com.sun.jna.Pointer)3 WString (com.sun.jna.WString)3 COMException (com.sun.jna.platform.win32.COM.COMException)3 ConnectionPoint (com.sun.jna.platform.win32.COM.ConnectionPoint)3