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);
}
}
}
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();
}
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);
}
Aggregations