Search in sources :

Example 31 with Variant

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

the class MSWord method SaveAs.

public void SaveAs(String FileName, LONG FileFormat) throws COMException {
    VARIANT vtFileName = new VARIANT(FileName);
    VARIANT vtFileFormat = new VARIANT(FileFormat);
    this.invokeNoReply("SaveAs", this.getActiveDocument(), vtFileName, vtFileFormat);
}
Also used : VARIANT(com.sun.jna.platform.win32.Variant.VARIANT)

Example 32 with Variant

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

the class Excelautomation_KB_219151_Mod method main.

public static void main(String[] args) throws IOException {
    // Initialize COM Subsystem
    Ole32.INSTANCE.CoInitializeEx(Pointer.NULL, Ole32.COINIT_MULTITHREADED);
    // Initialize Factory for COM object creation
    Factory fact = new Factory();
    // Set LCID for calls to english locale. Without this formulas need
    // to be specified in the users locale.
    fact.setLCID(new LCID(0x0409));
    try {
        // Start excel application
        ComExcel_Application excel = fact.createObject(ComExcel_Application.class);
        ComIApplication excelApp = excel.queryInterface(ComIApplication.class);
        // Set visiblite of application
        excelApp.setVisible(true);
        Helper.sleep(5);
        // Get a new workbook.
        ComIWorkbook wb = excelApp.getWorkbooks().Add();
        ComIWorksheet sheet = wb.getActiveSheet();
        // Add table headers going cell by cell.
        sheet.getCells().getItem(1, 1).setValue("First Name");
        sheet.getCells().getItem(1, 2).setValue("Last Name");
        sheet.getCells().getItem(1, 3).setValue("Full Name");
        sheet.getCells().getItem(1, 4).setValue("Salary");
        // Create an array to set multiple values at once.
        SAFEARRAY saNames = safeVariantArrayFromJava(new String[][] { { "John", "Smith" }, { "Tom", "Brown" }, { "Sue", "Thomas" }, { "Jane", "Jones" }, { "Adam", "Johnson" } });
        // Fill A2:B6 with an array of values (First and Last Names).
        VARIANT valueHolder = new VARIANT();
        valueHolder.setValue(Variant.VT_ARRAY | Variant.VT_VARIANT, saNames);
        sheet.getRange("A2", "B6").setValue(valueHolder);
        saNames.destroy();
        // Fill C2:C6 with a relative formula (=A2 & " " & B2).
        sheet.getRange("C2", "C6").setFormula("= A2 & \" \" & B2");
        // Fill D2:D6 with a formula(=RAND()*100000) and apply format.
        sheet.getRange("D2", "D6").setFormula("=RAND()*100000");
        sheet.getRange("D2", "D6").setNumberFormat("$0.00");
        // AutoFit columns A:D.
        sheet.getRange("A1", "D2").getEntireColumn().AutoFit();
        displayQuaterlySales(sheet);
        File tempFile = Helper.createNotExistingFile("exceloutput", ".xlsx");
        System.out.println("Writing output to: " + tempFile.getAbsolutePath());
        wb.SaveAs(tempFile.getAbsolutePath());
        excelApp.setUserControl(true);
    } finally {
        fact.disposeAll();
        Ole32.INSTANCE.CoUninitialize();
    }
    System.exit(0);
}
Also used : LCID(com.sun.jna.platform.win32.WinDef.LCID) SAFEARRAY(com.sun.jna.platform.win32.OaIdl.SAFEARRAY) ComIWorkbook(com.sun.jna.platform.win32.COM.util.office.excel.ComIWorkbook) Factory(com.sun.jna.platform.win32.COM.util.Factory) ComExcel_Application(com.sun.jna.platform.win32.COM.util.office.excel.ComExcel_Application) VARIANT(com.sun.jna.platform.win32.Variant.VARIANT) File(java.io.File) ComIApplication(com.sun.jna.platform.win32.COM.util.office.excel.ComIApplication) ComIWorksheet(com.sun.jna.platform.win32.COM.util.office.excel.ComIWorksheet)

Example 33 with Variant

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

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

the class COMUtils method comIsInitialized.

/**
     * Check if COM was initialized correctly. The initialization status is not changed!
     *
     * <p>This is a debug function, not for normal usage!</p>
     * 
     * @return whether COM has been initialized
     */
public static boolean comIsInitialized() {
    WinNT.HRESULT hr = Ole32.INSTANCE.CoInitializeEx(Pointer.NULL, Ole32.COINIT_MULTITHREADED);
    if (hr.equals(W32Errors.S_OK)) {
        // User failed - uninitialize again and return false
        Ole32.INSTANCE.CoUninitialize();
        return false;
    } else if (hr.equals(W32Errors.S_FALSE)) {
        // OK Variant 1 - User initialized COM with same threading module as
        // in this check. According to MSDN CoUninitialize needs to be called
        // in this case.
        Ole32.INSTANCE.CoUninitialize();
        return true;
    } else if (hr.intValue() == W32Errors.RPC_E_CHANGED_MODE) {
        return true;
    }
    // If another result than the checked ones above happens handling is
    // delegated to the "normal" COM exception handling and a COMException
    // will be raised.
    COMUtils.checkRC(hr);
    // The return will not be met, as COMUtils#checkRC will raise an exception
    return false;
}
Also used : WinNT(com.sun.jna.platform.win32.WinNT) HRESULT(com.sun.jna.platform.win32.WinNT.HRESULT)

Example 35 with Variant

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

the class Convert method toJavaObject.

public static Object toJavaObject(VARIANT value, Class<?> targetClass, ObjectFactory factory, boolean addReference, boolean freeValue) {
    if (null == value || value.getVarType().intValue() == VT_EMPTY || value.getVarType().intValue() == VT_NULL) {
        return null;
    }
    if (targetClass != null && (!targetClass.isAssignableFrom(Object.class))) {
        if (targetClass.isAssignableFrom(value.getClass())) {
            return value;
        }
        Object vobj = value.getValue();
        if (vobj != null && (targetClass.isAssignableFrom(vobj.getClass()))) {
            return vobj;
        }
    }
    VARIANT inputValue = value;
    if (value.getVarType().intValue() == (VT_BYREF | VT_VARIANT)) {
        value = (VARIANT) value.getValue();
    }
    // handling
    if (targetClass == null || (targetClass.isAssignableFrom(Object.class))) {
        targetClass = null;
        int varType = value.getVarType().intValue();
        switch(value.getVarType().intValue()) {
            case VT_UI1:
            case VT_I1:
            case VT_BYREF | VT_UI1:
            case VT_BYREF | VT_I1:
                targetClass = Byte.class;
                break;
            case VT_I2:
            case VT_BYREF | VT_I2:
                targetClass = Short.class;
                break;
            case VT_UI2:
            case VT_BYREF | VT_UI2:
                targetClass = Character.class;
                break;
            case VT_INT:
            case VT_UINT:
            case VT_UI4:
            case VT_I4:
            case VT_BYREF | VT_I4:
            case VT_BYREF | VT_UI4:
            case VT_BYREF | VT_INT:
            case VT_BYREF | VT_UINT:
                targetClass = Integer.class;
                break;
            case VT_UI8:
            case VT_I8:
            case VT_BYREF | VT_I8:
            case VT_BYREF | VT_UI8:
                targetClass = Long.class;
                break;
            case VT_R4:
            case VT_BYREF | VT_R4:
                targetClass = Float.class;
                break;
            case VT_R8:
            case VT_BYREF | VT_R8:
                targetClass = Double.class;
                break;
            case VT_BOOL:
            case VT_BYREF | VT_BOOL:
                targetClass = Boolean.class;
                break;
            case VT_ERROR:
            case VT_BYREF | VT_ERROR:
                targetClass = WinDef.SCODE.class;
                break;
            case VT_CY:
            case VT_BYREF | VT_CY:
                targetClass = OaIdl.CURRENCY.class;
                break;
            case VT_DATE:
            case VT_BYREF | VT_DATE:
                targetClass = Date.class;
                break;
            case VT_BSTR:
            case VT_BYREF | VT_BSTR:
                targetClass = String.class;
                break;
            case VT_UNKNOWN:
            case VT_BYREF | VT_UNKNOWN:
                targetClass = com.sun.jna.platform.win32.COM.IUnknown.class;
                break;
            case VT_DISPATCH:
            case VT_BYREF | VT_DISPATCH:
                targetClass = IDispatch.class;
                break;
            case VT_BYREF | VT_VARIANT:
                targetClass = Variant.class;
                break;
            case VT_BYREF:
                targetClass = PVOID.class;
                break;
            case VT_BYREF | VT_DECIMAL:
                targetClass = OaIdl.DECIMAL.class;
                break;
            case VT_RECORD:
            default:
                if ((varType & VT_ARRAY) > 0) {
                    targetClass = OaIdl.SAFEARRAY.class;
                }
        }
    }
    Object result;
    if (Byte.class.equals(targetClass) || byte.class.equals(targetClass)) {
        result = value.byteValue();
    } else if (Short.class.equals(targetClass) || short.class.equals(targetClass)) {
        result = value.shortValue();
    } else if (Character.class.equals(targetClass) || char.class.equals(targetClass)) {
        result = (char) value.intValue();
    } else if (Integer.class.equals(targetClass) || int.class.equals(targetClass)) {
        result = value.intValue();
    } else if (Long.class.equals(targetClass) || long.class.equals(targetClass) || IComEnum.class.isAssignableFrom(targetClass)) {
        result = value.longValue();
    } else if (Float.class.equals(targetClass) || float.class.equals(targetClass)) {
        result = value.floatValue();
    } else if (Double.class.equals(targetClass) || double.class.equals(targetClass)) {
        result = value.doubleValue();
    } else if (Boolean.class.equals(targetClass) || boolean.class.equals(targetClass)) {
        result = value.booleanValue();
    } else if (Date.class.equals(targetClass)) {
        result = value.dateValue();
    } else if (String.class.equals(targetClass)) {
        result = value.stringValue();
    } else if (value.getValue() instanceof com.sun.jna.platform.win32.COM.IDispatch) {
        com.sun.jna.platform.win32.COM.IDispatch d = (com.sun.jna.platform.win32.COM.IDispatch) value.getValue();
        Object proxy = factory.createProxy(targetClass, d);
        // call
        if (!addReference) {
            int n = d.Release();
        }
        result = proxy;
    } else {
        /*
                    WinDef.SCODE.class.equals(targetClass) 
                        || OaIdl.CURRENCY.class.equals(targetClass)
                        || OaIdl.DECIMAL.class.equals(targetClass)
                        || OaIdl.SAFEARRAY.class.equals(targetClass)
                        || com.sun.jna.platform.win32.COM.IUnknown.class.equals(targetClass)
                        || Variant.class.equals(targetClass)
                        || PVOID.class.equals(targetClass
                    */
        result = value.getValue();
    }
    if (IComEnum.class.isAssignableFrom(targetClass)) {
        result = targetClass.cast(Convert.toComEnum((Class<? extends IComEnum>) targetClass, result));
    }
    if (freeValue) {
        free(inputValue, result);
    }
    return result;
}
Also used : VARIANT(com.sun.jna.platform.win32.Variant.VARIANT) VT_VARIANT(com.sun.jna.platform.win32.Variant.VT_VARIANT) WinDef(com.sun.jna.platform.win32.WinDef) OaIdl(com.sun.jna.platform.win32.OaIdl)

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