Search in sources :

Example 11 with SAFEARRAY

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

the class SAFEARRAYTest method testSafeArrayPutGetElement.

@Test
public void testSafeArrayPutGetElement() throws Exception {
    int rowCount = 2;
    int colCount = 10;
    SAFEARRAY varArray = SAFEARRAY.createSafeArray(rowCount, colCount);
    assertThat(varArray.getDimensionCount(), is(2));
    assertThat(varArray.getUBound(0), equalTo(rowCount - 1));
    assertThat(varArray.getUBound(1), equalTo(colCount - 1));
    for (int rowIdx = 0; rowIdx < rowCount; rowIdx++) {
        for (int colIdx = 0; colIdx < colCount; colIdx++) {
            VARIANT variant = new VARIANT(rowIdx + "#" + colIdx);
            varArray.putElement(variant, rowIdx, colIdx);
        }
    }
    for (int rowIdx = 0; rowIdx < rowCount; rowIdx++) {
        for (int colIdx = 0; colIdx < colCount; colIdx++) {
            VARIANT element = (VARIANT) varArray.getElement(rowIdx, colIdx);
            assertEquals(rowIdx + "#" + colIdx, element.stringValue());
            OleAuto.INSTANCE.VariantClear(element);
        }
    }
}
Also used : SAFEARRAY(com.sun.jna.platform.win32.OaIdl.SAFEARRAY) VARIANT(com.sun.jna.platform.win32.Variant.VARIANT) IConnectionPoint(com.sun.jna.platform.win32.COM.util.IConnectionPoint) Test(org.junit.Test)

Example 12 with SAFEARRAY

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

the class SAFEARRAYTest method testPerformance.

@Ignore("Only for live testing")
@Test
public void testPerformance() {
    ObjectFactory fact = new ObjectFactory();
    // Open a record set with a sample search (basicly get the first five
    // entries from the search index
    Connection conn = fact.createObject(Connection.class);
    conn.Open("Provider=Search.CollatorDSO;Extended Properties='Application=Windows';", "", "", -1);
    Recordset recordset = fact.createObject(Recordset.class);
    recordset.Open("SELECT TOP 500 System.ItemPathDisplay, System.ItemName, System.ItemUrl, System.DateCreated FROM SYSTEMINDEX ORDER BY System.ItemUrl", conn, CursorTypeEnum.adOpenUnspecified, LockTypeEnum.adLockUnspecified, -1);
    SAFEARRAY wrap = recordset.GetRows();
    assertThat(wrap.getDimensionCount(), is(2));
    long timeDirect = 0;
    long timeGetElement = 0;
    long timePointer = 0;
    long timeHelper = 0;
    long start, end;
    for (int i = 0; i < 4 * 10; i++) {
        if (i % 4 == 0) {
            start = System.currentTimeMillis();
            toArrayPtrToElement(wrap);
            end = System.currentTimeMillis();
            timePointer += (end - start);
        } else if (i % 4 == 1) {
            start = System.currentTimeMillis();
            toArrayGetElement(wrap);
            end = System.currentTimeMillis();
            timeGetElement += (end - start);
        } else if (i % 4 == 2) {
            start = System.currentTimeMillis();
            toArrayDirect(wrap);
            end = System.currentTimeMillis();
            timeDirect += (end - start);
        } else if (i % 4 == 3) {
            start = System.currentTimeMillis();
            OaIdlUtil.toPrimitiveArray(wrap, false);
            end = System.currentTimeMillis();
            timeHelper += (end - start);
        }
    }
    System.out.println("Direct: " + timeDirect + " ms");
    System.out.println("GetElement: " + timeGetElement + " ms");
    System.out.println("Pointer: " + timePointer + " ms");
    System.out.println("Helper: " + timeHelper + " ms");
    recordset.Close();
    conn.Close();
    fact.disposeAll();
}
Also used : ObjectFactory(com.sun.jna.platform.win32.COM.util.ObjectFactory) SAFEARRAY(com.sun.jna.platform.win32.OaIdl.SAFEARRAY) IConnectionPoint(com.sun.jna.platform.win32.COM.util.IConnectionPoint) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 13 with SAFEARRAY

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

Aggregations

VARIANT (com.sun.jna.platform.win32.Variant.VARIANT)10 SAFEARRAY (com.sun.jna.platform.win32.OaIdl.SAFEARRAY)9 IConnectionPoint (com.sun.jna.platform.win32.COM.util.IConnectionPoint)6 ComObject (com.sun.jna.platform.win32.COM.util.annotation.ComObject)5 Test (org.junit.Test)5 Pointer (com.sun.jna.Pointer)4 BSTR (com.sun.jna.platform.win32.WTypes.BSTR)3 ObjectFactory (com.sun.jna.platform.win32.COM.util.ObjectFactory)2 DATE (com.sun.jna.platform.win32.OaIdl.DATE)2 VT_BSTR (com.sun.jna.platform.win32.Variant.VT_BSTR)2 VT_DATE (com.sun.jna.platform.win32.Variant.VT_DATE)2 VT_VARIANT (com.sun.jna.platform.win32.Variant.VT_VARIANT)2 Date (java.util.Date)2 COMException (com.sun.jna.platform.win32.COM.COMException)1 Factory (com.sun.jna.platform.win32.COM.util.Factory)1 ComExcel_Application (com.sun.jna.platform.win32.COM.util.office.excel.ComExcel_Application)1 ComIApplication (com.sun.jna.platform.win32.COM.util.office.excel.ComIApplication)1 ComIWorkbook (com.sun.jna.platform.win32.COM.util.office.excel.ComIWorkbook)1 ComIWorksheet (com.sun.jna.platform.win32.COM.util.office.excel.ComIWorksheet)1 OaIdl (com.sun.jna.platform.win32.OaIdl)1