use of com.sun.jna.platform.win32.Variant.VT_BSTR 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.Variant.VT_BSTR in project jna by java-native-access.
the class OaIdlUtil method toPrimitiveArray.
private static void toPrimitiveArray(Object dataArray, Object targetArray, int[] minIdx, int[] maxIdx, int[] elements, int[] cumElements, int varType, int[] currentIdx) {
int dimIdx = currentIdx.length;
int[] subIdx = new int[currentIdx.length + 1];
System.arraycopy(currentIdx, 0, subIdx, 0, dimIdx);
for (int i = minIdx[dimIdx]; i <= maxIdx[dimIdx]; i++) {
subIdx[dimIdx] = i;
if (dimIdx == (minIdx.length - 1)) {
int offset = 0;
for (int j = 0; j < dimIdx; j++) {
offset += cumElements[j] * currentIdx[j];
}
offset += subIdx[dimIdx] - minIdx[dimIdx];
int targetPos = subIdx[dimIdx] - minIdx[dimIdx];
switch(varType) {
case VT_BOOL:
Array.set(targetArray, targetPos, Array.getShort(dataArray, offset) != 0);
break;
case VT_UI1:
case VT_I1:
Array.set(targetArray, targetPos, Array.getByte(dataArray, offset));
break;
case VT_UI2:
case VT_I2:
Array.set(targetArray, targetPos, Array.getShort(dataArray, offset));
break;
case VT_UI4:
case VT_UINT:
case VT_I4:
case VT_INT:
Array.set(targetArray, targetPos, Array.getInt(dataArray, offset));
break;
case VT_ERROR:
Array.set(targetArray, targetPos, new SCODE(Array.getInt(dataArray, offset)));
break;
case VT_R4:
Array.set(targetArray, targetPos, Array.getFloat(dataArray, offset));
break;
case VT_R8:
Array.set(targetArray, targetPos, Array.getDouble(dataArray, offset));
break;
case VT_DATE:
Array.set(targetArray, targetPos, new DATE(Array.getDouble(dataArray, offset)).getAsJavaDate());
break;
case VT_BSTR:
Array.set(targetArray, targetPos, new BSTR((Pointer) Array.get(dataArray, offset)).getValue());
break;
case VT_VARIANT:
VARIANT holder = (VARIANT) Array.get(dataArray, offset);
switch(holder.getVarType().intValue()) {
case VT_NULL:
case VT_EMPTY:
Array.set(targetArray, targetPos, null);
break;
case VT_BOOL:
Array.set(targetArray, targetPos, holder.booleanValue());
break;
case VT_UI1:
case VT_I1:
Array.set(targetArray, targetPos, holder.byteValue());
break;
case VT_UI2:
case VT_I2:
Array.set(targetArray, targetPos, holder.shortValue());
break;
case VT_UI4:
case VT_UINT:
case VT_I4:
case VT_INT:
Array.set(targetArray, targetPos, holder.intValue());
break;
case VT_ERROR:
Array.set(targetArray, targetPos, new SCODE(holder.intValue()));
break;
case VT_R4:
Array.set(targetArray, targetPos, holder.floatValue());
break;
case VT_R8:
Array.set(targetArray, targetPos, holder.doubleValue());
break;
case VT_DATE:
Array.set(targetArray, targetPos, holder.dateValue());
break;
case VT_BSTR:
Array.set(targetArray, targetPos, holder.stringValue());
break;
default:
throw new IllegalStateException("Type not supported: " + holder.getVarType().intValue());
}
break;
case VT_UNKNOWN:
case VT_DISPATCH:
case VT_CY:
case VT_DECIMAL:
case VT_RECORD:
default:
throw new IllegalStateException("Type not supported: " + varType);
}
} else {
toPrimitiveArray(dataArray, Array.get(targetArray, i), minIdx, maxIdx, elements, cumElements, varType, subIdx);
}
}
}
use of com.sun.jna.platform.win32.Variant.VT_BSTR 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;
}
use of com.sun.jna.platform.win32.Variant.VT_BSTR 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