use of com.sun.jna.platform.win32.OaIdl.SAFEARRAY in project jna by java-native-access.
the class VariantTest method testVariantSafearrayWrapping.
public void testVariantSafearrayWrapping() {
SAFEARRAY safearray = OaIdl.SAFEARRAY.createSafeArray(new VARTYPE(Variant.VT_I1), 5);
try {
VARIANT variant = new VARIANT(safearray);
assertThat(variant.getVarType().intValue(), equalTo((int) (Variant.VT_I1 | Variant.VT_ARRAY)));
Object wrappedValue = variant.getValue();
assertThat(wrappedValue, instanceOf(SAFEARRAY.class));
assertThat(safearray.getUBound(0), is(4));
} finally {
safearray.destroy();
}
}
use of com.sun.jna.platform.win32.OaIdl.SAFEARRAY 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;
}
use of com.sun.jna.platform.win32.OaIdl.SAFEARRAY in project jna by java-native-access.
the class Convert method toVariant.
/**
* Convert a java value into a VARIANT suitable for passing in a COM
* invocation.
*
* <p><i>Implementation notes</i></p>
*
* <ul>
* <li>VARIANTs are not rewrapped, but passed through unmodified</li>
* <li>A string is wrapped into a BSTR, that is wrapped into the VARIANT.
* The string is allocated as native memory by the VARIANT constructor.
* The BSTR needs to be freed by {@link com.sun.jna.platform.win32.OleAuto#SysFreeString}.</li>
* </ul>
*
* @param value to be wrapped
* @return wrapped VARIANT
*/
public static VARIANT toVariant(Object value) {
if (value instanceof VARIANT) {
return (VARIANT) value;
} else if (value instanceof BSTR) {
return new VARIANT((BSTR) value);
} else if (value instanceof VARIANT_BOOL) {
return new VARIANT((VARIANT_BOOL) value);
} else if (value instanceof BOOL) {
return new VARIANT((BOOL) value);
} else if (value instanceof LONG) {
return new VARIANT((LONG) value);
} else if (value instanceof SHORT) {
return new VARIANT((SHORT) value);
} else if (value instanceof DATE) {
return new VARIANT((DATE) value);
} else if (value instanceof BYTE) {
return new VARIANT((BYTE) value);
} else if (value instanceof Byte) {
return new VARIANT((Byte) value);
} else if (value instanceof Character) {
return new VARIANT((Character) value);
} else if (value instanceof CHAR) {
return new VARIANT((CHAR) value);
} else if (value instanceof Short) {
return new VARIANT((Short) value);
} else if (value instanceof Integer) {
return new VARIANT((Integer) value);
} else if (value instanceof Long) {
return new VARIANT((Long) value);
} else if (value instanceof Float) {
return new VARIANT((Float) value);
} else if (value instanceof Double) {
return new VARIANT((Double) value);
} else if (value instanceof String) {
return new VARIANT((String) value);
} else if (value instanceof Boolean) {
return new VARIANT((Boolean) value);
} else if (value instanceof com.sun.jna.platform.win32.COM.IDispatch) {
return new VARIANT((com.sun.jna.platform.win32.COM.IDispatch) value);
} else if (value instanceof Date) {
return new VARIANT((Date) value);
} else if (value instanceof Proxy) {
InvocationHandler ih = Proxy.getInvocationHandler(value);
ProxyObject pobj = (ProxyObject) ih;
return new VARIANT(pobj.getRawDispatch());
} else if (value instanceof IComEnum) {
IComEnum enm = (IComEnum) value;
return new VARIANT(new WinDef.LONG(enm.getValue()));
} else if (value instanceof SAFEARRAY) {
return new VARIANT((SAFEARRAY) value);
} else {
return null;
}
}
use of com.sun.jna.platform.win32.OaIdl.SAFEARRAY in project jna by java-native-access.
the class OaIdlUtil method toPrimitiveArray.
/**
* Read SAFEARRAY into a java array. Not all VARTYPEs are supported!
*
* <p>
* Supported types:</p>
* <ul>
* <li>VT_BOOL</li>
* <li>VT_UI1</li>
* <li>VT_I1</li>
* <li>VT_UI2</li>
* <li>VT_I2</li>
* <li>VT_UI4</li>
* <li>VT_UINT</li>
* <li>VT_I4</li>
* <li>VT_INT</li>
* <li>VT_ERROR</li>
* <li>VT_R4</li>
* <li>VT_R8</li>
* <li>VT_DATE</li>
* <li>VT_BSTR</li>
* <li>VT_VARIANT (Onle the following VARTYPES):
* <ul>
* <li>VT_EMPTY (converted to NULL)</li>
* <li>VT_NULL</li>
* <li>VT_BOOL</li>
* <li>VT_UI1</li>
* <li>VT_I1</li>
* <li>VT_UI2</li>
* <li>VT_I2</li>
* <li>VT_UI4</li>
* <li>VT_UINT</li>
* <li>VT_I4</li>
* <li>VT_INT</li>
* <li>VT_ERROR</li>
* <li>VT_R4</li>
* <li>VT_R8</li>
* <li>VT_DATE</li>
* <li>VT_BSTR</li>
* </ul>
* </li>
* </ul>
*
* @param sa SAFEARRAY to convert
* @param destruct if true the supplied SAFEARRAY is destroyed, there must
* not be additional locks on the array!
* @return Java array corresponding to the given SAFEARRAY
*/
public static Object toPrimitiveArray(SAFEARRAY sa, boolean destruct) {
Pointer dataPointer = sa.accessData();
try {
int dimensions = sa.getDimensionCount();
int[] minIdx = new int[dimensions];
int[] maxIdx = new int[dimensions];
int[] elements = new int[dimensions];
int[] cumElements = new int[dimensions];
int varType = sa.getVarType().intValue();
for (int i = 0; i < dimensions; i++) {
minIdx[i] = sa.getLBound(i);
maxIdx[i] = sa.getUBound(i);
elements[i] = maxIdx[i] - minIdx[i] + 1;
}
for (int i = dimensions - 1; i >= 0; i--) {
if (i == (dimensions - 1)) {
cumElements[i] = 1;
} else {
cumElements[i] = cumElements[i + 1] * elements[i + 1];
}
}
if (dimensions == 0) {
throw new IllegalArgumentException("Supplied Array has no dimensions.");
}
int elementCount = cumElements[0] * elements[0];
Object sourceArray;
switch(varType) {
case VT_UI1:
case VT_I1:
sourceArray = dataPointer.getByteArray(0, elementCount);
break;
case VT_BOOL:
case VT_UI2:
case VT_I2:
sourceArray = dataPointer.getShortArray(0, elementCount);
break;
case VT_UI4:
case VT_UINT:
case VT_I4:
case VT_INT:
case VT_ERROR:
sourceArray = dataPointer.getIntArray(0, elementCount);
break;
case VT_R4:
sourceArray = dataPointer.getFloatArray(0, elementCount);
break;
case VT_R8:
case VT_DATE:
sourceArray = dataPointer.getDoubleArray(0, elementCount);
break;
case VT_BSTR:
sourceArray = dataPointer.getPointerArray(0, elementCount);
break;
case VT_VARIANT:
VARIANT variant = new VARIANT(dataPointer);
sourceArray = variant.toArray(elementCount);
break;
case VT_UNKNOWN:
case VT_DISPATCH:
case VT_CY:
case VT_DECIMAL:
case VT_RECORD:
default:
throw new IllegalStateException("Type not supported: " + varType);
}
Object targetArray = Array.newInstance(Object.class, elements);
toPrimitiveArray(sourceArray, targetArray, minIdx, maxIdx, elements, cumElements, varType, new int[0]);
return targetArray;
} finally {
sa.unaccessData();
if (destruct) {
sa.destroy();
}
}
}
use of com.sun.jna.platform.win32.OaIdl.SAFEARRAY in project jna by java-native-access.
the class SAFEARRAYTest method testDataTypes.
@Test
public void testDataTypes() {
int idx = 1;
Pointer dataPointer;
SAFEARRAY sa;
long elementSize;
Object[] objectResult;
sa = SAFEARRAY.createSafeArray(new WTypes.VARTYPE(VT_BOOL), 2);
elementSize = sa.getElemsize();
assertThat(elementSize, equalTo(2L));
dataPointer = sa.accessData();
sa.putElement(true, idx);
short[] shortResult = dataPointer.getShortArray(0, 2);
objectResult = (Object[]) toPrimitiveArray(sa, false);
assertThat((Boolean) sa.getElement(idx), equalTo(true));
assertThat(shortResult[idx], equalTo((short) 0xFFFF));
assertThat((Short) dataPointer.getShort(idx * elementSize), equalTo((short) 0xFFFF));
assertThat((Boolean) objectResult[idx], equalTo(true));
sa.unaccessData();
sa.destroy();
byte testByte = 67;
sa = SAFEARRAY.createSafeArray(new WTypes.VARTYPE(VT_UI1), 2);
elementSize = sa.getElemsize();
assertThat(elementSize, equalTo(1L));
dataPointer = sa.accessData();
sa.putElement(testByte, idx);
byte[] byteResult = dataPointer.getByteArray(0, 2);
objectResult = (Object[]) toPrimitiveArray(sa, false);
assertThat((Byte) sa.getElement(idx), equalTo(testByte));
assertThat(dataPointer.getByte(idx * elementSize), equalTo(testByte));
assertThat(byteResult[idx], equalTo(testByte));
assertThat((Byte) objectResult[idx], equalTo(testByte));
sa.unaccessData();
sa.destroy();
sa = SAFEARRAY.createSafeArray(new WTypes.VARTYPE(VT_I1), 2);
elementSize = sa.getElemsize();
assertThat(elementSize, equalTo(1L));
dataPointer = sa.accessData();
sa.putElement(testByte, idx);
byteResult = dataPointer.getByteArray(0, 2);
objectResult = (Object[]) toPrimitiveArray(sa, false);
assertThat((Byte) sa.getElement(idx), equalTo(testByte));
assertThat(dataPointer.getByte(idx * elementSize), equalTo(testByte));
assertThat(byteResult[idx], equalTo(testByte));
assertThat((Byte) objectResult[idx], equalTo(testByte));
sa.unaccessData();
sa.destroy();
short testShort = Short.MAX_VALUE - 1;
sa = SAFEARRAY.createSafeArray(new WTypes.VARTYPE(VT_UI2), 2);
elementSize = sa.getElemsize();
assertThat(elementSize, equalTo(2L));
dataPointer = sa.accessData();
sa.putElement(testShort, idx);
shortResult = dataPointer.getShortArray(0, 2);
objectResult = (Object[]) toPrimitiveArray(sa, false);
assertThat((Short) sa.getElement(idx), equalTo(testShort));
assertThat(dataPointer.getShort(idx * elementSize), equalTo(testShort));
assertThat(shortResult[idx], equalTo(testShort));
assertThat((Short) objectResult[idx], equalTo(testShort));
sa.unaccessData();
sa.destroy();
sa = SAFEARRAY.createSafeArray(new WTypes.VARTYPE(VT_I2), 2);
elementSize = sa.getElemsize();
assertThat(elementSize, equalTo(2L));
dataPointer = sa.accessData();
sa.putElement(testShort, idx);
shortResult = dataPointer.getShortArray(0, 2);
objectResult = (Object[]) toPrimitiveArray(sa, false);
assertThat((Short) sa.getElement(idx), equalTo(testShort));
assertThat(dataPointer.getShort(idx * elementSize), equalTo(testShort));
assertThat(shortResult[idx], equalTo(testShort));
assertThat((Short) objectResult[idx], equalTo(testShort));
sa.unaccessData();
sa.destroy();
int testInt = Integer.MAX_VALUE - 1;
sa = SAFEARRAY.createSafeArray(new WTypes.VARTYPE(VT_UI4), 2);
elementSize = sa.getElemsize();
assertThat(elementSize, equalTo(4L));
dataPointer = sa.accessData();
sa.putElement(testInt, idx);
int[] intResult = dataPointer.getIntArray(0, 2);
objectResult = (Object[]) toPrimitiveArray(sa, false);
assertThat((Integer) sa.getElement(idx), equalTo(testInt));
assertThat(dataPointer.getInt(idx * elementSize), equalTo(testInt));
assertThat(intResult[idx], equalTo(testInt));
assertThat((Integer) objectResult[idx], equalTo(testInt));
sa.unaccessData();
sa.destroy();
sa = SAFEARRAY.createSafeArray(new WTypes.VARTYPE(VT_I4), 2);
elementSize = sa.getElemsize();
assertThat(elementSize, equalTo(4L));
dataPointer = sa.accessData();
sa.putElement(testInt, idx);
intResult = dataPointer.getIntArray(0, 2);
objectResult = (Object[]) toPrimitiveArray(sa, false);
assertThat((Integer) sa.getElement(idx), equalTo(testInt));
assertThat(dataPointer.getInt(idx * elementSize), equalTo(testInt));
assertThat(intResult[idx], equalTo(testInt));
assertThat((Integer) objectResult[idx], equalTo(testInt));
sa.unaccessData();
sa.destroy();
sa = SAFEARRAY.createSafeArray(new WTypes.VARTYPE(VT_UINT), 2);
elementSize = sa.getElemsize();
assertThat(elementSize, equalTo(4L));
dataPointer = sa.accessData();
sa.putElement(testInt, idx);
intResult = dataPointer.getIntArray(0, 2);
objectResult = (Object[]) toPrimitiveArray(sa, false);
assertThat((Integer) sa.getElement(idx), equalTo(testInt));
assertThat(dataPointer.getInt(idx * elementSize), equalTo(testInt));
assertThat(intResult[idx], equalTo(testInt));
assertThat((Integer) objectResult[idx], equalTo(testInt));
sa.unaccessData();
sa.destroy();
sa = SAFEARRAY.createSafeArray(new WTypes.VARTYPE(VT_INT), 2);
elementSize = sa.getElemsize();
assertThat(elementSize, equalTo(4L));
dataPointer = sa.accessData();
sa.putElement(testInt, idx);
intResult = dataPointer.getIntArray(0, 2);
objectResult = (Object[]) toPrimitiveArray(sa, false);
assertThat((Integer) sa.getElement(idx), equalTo(testInt));
assertThat(dataPointer.getInt(idx * elementSize), equalTo(testInt));
assertThat(intResult[idx], equalTo(testInt));
assertThat((Integer) objectResult[idx], equalTo(testInt));
sa.unaccessData();
sa.destroy();
SCODE testSCODE = new SCODE(47);
sa = SAFEARRAY.createSafeArray(new WTypes.VARTYPE(VT_ERROR), 2);
elementSize = sa.getElemsize();
assertThat(elementSize, equalTo(4L));
dataPointer = sa.accessData();
sa.putElement(testSCODE, idx);
intResult = dataPointer.getIntArray(0, 2);
objectResult = (Object[]) toPrimitiveArray(sa, false);
assertThat((SCODE) sa.getElement(idx), equalTo(testSCODE));
assertThat(dataPointer.getInt(idx * elementSize), equalTo(47));
assertThat(intResult[idx], equalTo(47));
assertThat((SCODE) objectResult[idx], equalTo(testSCODE));
sa.unaccessData();
sa.destroy();
float testFloat = 42.23f;
sa = SAFEARRAY.createSafeArray(new WTypes.VARTYPE(VT_R4), 2);
elementSize = sa.getElemsize();
assertThat(elementSize, equalTo(4L));
dataPointer = sa.accessData();
sa.putElement(testFloat, idx);
float[] floatResult = dataPointer.getFloatArray(0, 2);
objectResult = (Object[]) toPrimitiveArray(sa, false);
assertThat((Float) sa.getElement(idx), equalTo(testFloat));
assertThat((Float) dataPointer.getFloat(idx * elementSize), equalTo(testFloat));
assertThat(floatResult[idx], equalTo(testFloat));
assertThat((Float) objectResult[idx], equalTo(testFloat));
sa.unaccessData();
sa.destroy();
double testDouble = 42.23d;
sa = SAFEARRAY.createSafeArray(new WTypes.VARTYPE(VT_R8), 2);
elementSize = sa.getElemsize();
assertThat(elementSize, equalTo(8L));
dataPointer = sa.accessData();
sa.putElement(testDouble, idx);
double[] doubleResult = dataPointer.getDoubleArray(0, 2);
objectResult = (Object[]) toPrimitiveArray(sa, false);
assertThat((Double) sa.getElement(idx), equalTo(testDouble));
assertThat((Double) dataPointer.getDouble(idx * elementSize), equalTo(testDouble));
assertThat(doubleResult[idx], equalTo(testDouble));
assertThat((Double) objectResult[idx], equalTo(testDouble));
sa.unaccessData();
sa.destroy();
Date testDate = new Date(1923, 1, 1, 5, 0, 0);
DATE testDATE = new DATE(testDate);
sa = SAFEARRAY.createSafeArray(new WTypes.VARTYPE(VT_DATE), 2);
elementSize = sa.getElemsize();
assertThat(elementSize, equalTo(8L));
dataPointer = sa.accessData();
sa.putElement(testDATE, idx);
doubleResult = dataPointer.getDoubleArray(0, 2);
objectResult = (Object[]) toPrimitiveArray(sa, false);
assertThat(((DATE) sa.getElement(idx)).date, equalTo(testDATE.date));
assertThat((Double) dataPointer.getDouble(idx * elementSize), equalTo(testDATE.date));
assertThat(((DATE) sa.getElement(idx)).getAsJavaDate(), equalTo(testDate));
assertThat(doubleResult[idx], equalTo(testDATE.date));
assertThat((Date) objectResult[idx], equalTo(testDate));
sa.unaccessData();
sa.destroy();
String testString = "äöüßAE!";
sa = SAFEARRAY.createSafeArray(new WTypes.VARTYPE(VT_BSTR), 2);
elementSize = sa.getElemsize();
dataPointer = sa.accessData();
sa.putElement(testString, idx);
Pointer[] pointerResult = dataPointer.getPointerArray(0, 2);
objectResult = (Object[]) toPrimitiveArray(sa, false);
assertThat(((String) sa.getElement(idx)), equalTo(testString));
assertThat(new BSTR(dataPointer.getPointer(idx * elementSize)).getValue(), equalTo(testString));
assertThat(new BSTR(pointerResult[idx]).getValue(), equalTo(testString));
assertThat((String) objectResult[idx], equalTo(testString));
sa.unaccessData();
sa.destroy();
// VT_VARIANT is tested in testADODB
// untested: VT_UNKNOWN
// untested: VT_DISPATCH
// untested: VT_CY
// untested: VT_DECIMAL
// unsupported: VT_RECORD
}
Aggregations