use of com.sun.jna.platform.win32.WTypes.VARTYPE 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.WTypes.VARTYPE in project jna by java-native-access.
the class TlbAbstractMethod method getType.
protected String getType(TYPEDESC typeDesc) {
VARTYPE vt = typeDesc.vt;
String type = "not_defined";
if (vt.intValue() == Variant.VT_PTR) {
TYPEDESC lptdesc = typeDesc._typedesc.getLptdesc();
type = this.getType(lptdesc);
} else if (vt.intValue() == Variant.VT_SAFEARRAY || vt.intValue() == Variant.VT_CARRAY) {
TYPEDESC tdescElem = typeDesc._typedesc.getLpadesc().tdescElem;
type = this.getType(tdescElem);
} else if (vt.intValue() == Variant.VT_USERDEFINED) {
HREFTYPE hreftype = typeDesc._typedesc.hreftype;
type = this.getUserdefinedType(hreftype);
} else {
type = this.getVarType(vt);
}
return type;
}
use of com.sun.jna.platform.win32.WTypes.VARTYPE 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.WTypes.VARTYPE 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.WTypes.VARTYPE 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;
}
Aggregations