use of com.sun.jna.platform.win32.Variant in project jna by java-native-access.
the class CallbackProxy method invokeOnThread.
void invokeOnThread(final DISPID dispIdMember, final REFIID riid, LCID lcid, WORD wFlags, final DISPPARAMS.ByReference pDispParams) {
VARIANT[] arguments = pDispParams.getArgs();
final Method eventMethod = CallbackProxy.this.dsipIdMap.get(dispIdMember);
if (eventMethod == null) {
CallbackProxy.this.comEventCallbackListener.errorReceivingCallbackEvent("No method found with dispId = " + dispIdMember, null);
return;
}
/**
* DISPPARAMs provides two different ways to pass arguments.
*
* Arguments can be passed as a linear list with all arguments
* specified to a certain position (positional) or the position of
* an argument can be passed via the rgdispidNamedArgs array
* (named).
*
* pDispParams.rgvarg (length in pDispParams.cArgs) contains all
* arguments (named + position based)
*
* pDispParams.rgdispidNamedArgs (length in pDispParams.cNamedArgs)
* contains the named parameters as DISPIDs - the DISPIDs are the
* target index in the method signature (zero based).
*
* Each entry in pDispParams.rgvarg is either position based or name
* based and the position bases arguments are passed in reverse
* order, so getting this:
*
* rgvarg = ["arg1", "arg2", "arg3", "arg4", "arg5"]
* rgdispidNamedArgs = [3, 4]
*
* Would lead to this paramater array in the handler:
*
* ["arg5", "arg4", "arg3", "arg1", "arg2"]
*
* See also:
* https://msdn.microsoft.com/de-de/library/windows/desktop/ms221653%28v=vs.85%29.aspx
*/
// Arguments are converted to the JAVA side and IDispatch Interfaces
// are wrapped into an ProxyObject if so requested.
//
// Out-Parameter need to be specified as VARIANT, VARIANT args are
// not converted, so COM memory allocation rules apply.
DISPID[] positionMap = pDispParams.getRgdispidNamedArgs();
final Class<?>[] paramTypes = eventMethod.getParameterTypes();
final Object[] params = new Object[paramTypes.length];
// Handle position based parameters first
for (int i = 0; i < params.length && (arguments.length - positionMap.length - i) > 0; i++) {
Class targetClass = paramTypes[i];
Variant.VARIANT varg = arguments[arguments.length - i - 1];
params[i] = Convert.toJavaObject(varg, targetClass, factory, true, false);
}
for (int i = 0; i < positionMap.length; i++) {
int targetPosition = positionMap[i].intValue();
if (targetPosition >= params.length) {
// If less parameters are mapped then supplied, ignore
continue;
}
Class targetClass = paramTypes[targetPosition];
Variant.VARIANT varg = arguments[i];
params[targetPosition] = Convert.toJavaObject(varg, targetClass, factory, true, false);
}
// exception occurs while doing the call into the target method
for (int i = 0; i < params.length; i++) {
if (params[i] == null && paramTypes[i].isPrimitive()) {
if (paramTypes[i].equals(boolean.class)) {
params[i] = DEFAULT_BOOLEAN;
} else if (paramTypes[i].equals(byte.class)) {
params[i] = DEFAULT_BYTE;
} else if (paramTypes[i].equals(short.class)) {
params[i] = DEFAULT_SHORT;
} else if (paramTypes[i].equals(int.class)) {
params[i] = DEFAULT_INT;
} else if (paramTypes[i].equals(long.class)) {
params[i] = DEFAULT_LONG;
} else if (paramTypes[i].equals(float.class)) {
params[i] = DEFAULT_FLOAT;
} else if (paramTypes[i].equals(double.class)) {
params[i] = DEFAULT_DOUBLE;
} else {
throw new IllegalArgumentException("Class type " + paramTypes[i].getName() + " not mapped to primitive default value.");
}
}
}
try {
eventMethod.invoke(comEventCallbackListener, params);
} catch (Exception e) {
List<String> decodedClassNames = new ArrayList<String>(params.length);
for (Object o : params) {
if (o == null) {
decodedClassNames.add("NULL");
} else {
decodedClassNames.add(o.getClass().getName());
}
}
CallbackProxy.this.comEventCallbackListener.errorReceivingCallbackEvent("Exception invoking method " + eventMethod + " supplied: " + decodedClassNames.toString(), e);
}
}
use of com.sun.jna.platform.win32.Variant 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.Variant 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 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 in project jna by java-native-access.
the class VariantTest method testVariantCopyBoolean.
public void testVariantCopyBoolean() {
VARIANT variantSource = new VARIANT(Variant.VARIANT_TRUE);
VARIANT variantDest = new VARIANT();
HRESULT hr = OleAuto.INSTANCE.VariantCopy(variantDest.getPointer(), variantSource);
assertTrue("hr: " + hr.intValue(), hr.intValue() == 0);
}
Aggregations