use of com.sun.jna.platform.win32.WTypes.BSTR 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.WTypes.BSTR 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.WTypes.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.WTypes.BSTR in project jna by java-native-access.
the class TypeInfoUtil method getNames.
/**
* Gets the names.
*
* @param memid
* the memid
* @param maxNames
* the max names
* @return the names
*/
public String[] getNames(MEMBERID memid, int maxNames) {
BSTR[] rgBstrNames = new BSTR[maxNames];
UINTByReference pcNames = new UINTByReference();
HRESULT hr = this.typeInfo.GetNames(memid, rgBstrNames, new UINT(maxNames), pcNames);
COMUtils.checkRC(hr);
int cNames = pcNames.getValue().intValue();
String[] result = new String[cNames];
for (int i = 0; i < result.length; i++) {
result[i] = rgBstrNames[i].getValue();
OLEAUTO.SysFreeString(rgBstrNames[i]);
}
return result;
}
use of com.sun.jna.platform.win32.WTypes.BSTR in project jna by java-native-access.
the class ITypeInfoTest method testGetNames.
@Test
public void testGetNames() {
ITypeInfo[] typeInfos = getTypeInfos();
MEMBERID memid = new MEMBERID(1);
BSTR[] rgBstrNames = new BSTR[1];
UINT cMaxNames = new UINT(1);
UINTByReference pcNames = new UINTByReference();
for (ITypeInfo typeInfo : typeInfos) {
HRESULT hr = typeInfo.GetNames(memid, rgBstrNames, cMaxNames, pcNames);
if (COMUtils.SUCCEEDED(hr)) {
//System.out.println("pcNames: " + pcNames.getValue().intValue());
return;
}
}
throw new RuntimeException("Didn't find name for member in any of the type infos");
}
Aggregations