Search in sources :

Example 1 with BSTR

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;
}
Also used : BSTR(com.sun.jna.platform.win32.WTypes.BSTR) OaIdl(com.sun.jna.platform.win32.OaIdl) SAFEARRAY(com.sun.jna.platform.win32.OaIdl.SAFEARRAY) VARIANT(com.sun.jna.platform.win32.Variant.VARIANT)

Example 2 with BSTR

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;
    }
}
Also used : SAFEARRAY(com.sun.jna.platform.win32.OaIdl.SAFEARRAY) VARIANT_BOOL(com.sun.jna.platform.win32.OaIdl.VARIANT_BOOL) CHAR(com.sun.jna.platform.win32.WinDef.CHAR) VARIANT(com.sun.jna.platform.win32.Variant.VARIANT) VT_VARIANT(com.sun.jna.platform.win32.Variant.VT_VARIANT) VT_BSTR(com.sun.jna.platform.win32.Variant.VT_BSTR) BSTR(com.sun.jna.platform.win32.WTypes.BSTR) Proxy(java.lang.reflect.Proxy) BYTE(com.sun.jna.platform.win32.WinDef.BYTE) LONG(com.sun.jna.platform.win32.WinDef.LONG) InvocationHandler(java.lang.reflect.InvocationHandler) Date(java.util.Date) SHORT(com.sun.jna.platform.win32.WinDef.SHORT) DATE(com.sun.jna.platform.win32.OaIdl.DATE) VT_DATE(com.sun.jna.platform.win32.Variant.VT_DATE) VARIANT_BOOL(com.sun.jna.platform.win32.OaIdl.VARIANT_BOOL) BOOL(com.sun.jna.platform.win32.WinDef.BOOL) VT_BOOL(com.sun.jna.platform.win32.Variant.VT_BOOL)

Example 3 with BSTR

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);
        }
    }
}
Also used : VT_BSTR(com.sun.jna.platform.win32.Variant.VT_BSTR) BSTR(com.sun.jna.platform.win32.WTypes.BSTR) SCODE(com.sun.jna.platform.win32.WinDef.SCODE) DATE(com.sun.jna.platform.win32.OaIdl.DATE) VT_DATE(com.sun.jna.platform.win32.Variant.VT_DATE) VARIANT(com.sun.jna.platform.win32.Variant.VARIANT) VT_VARIANT(com.sun.jna.platform.win32.Variant.VT_VARIANT)

Example 4 with BSTR

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;
}
Also used : BSTR(com.sun.jna.platform.win32.WTypes.BSTR) UINTByReference(com.sun.jna.platform.win32.WinDef.UINTByReference) HRESULT(com.sun.jna.platform.win32.WinNT.HRESULT) UINT(com.sun.jna.platform.win32.WinDef.UINT)

Example 5 with BSTR

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");
}
Also used : BSTR(com.sun.jna.platform.win32.WTypes.BSTR) UINTByReference(com.sun.jna.platform.win32.WinDef.UINTByReference) HRESULT(com.sun.jna.platform.win32.WinNT.HRESULT) MEMBERID(com.sun.jna.platform.win32.OaIdl.MEMBERID) UINT(com.sun.jna.platform.win32.WinDef.UINT) Test(org.junit.Test)

Aggregations

BSTR (com.sun.jna.platform.win32.WTypes.BSTR)11 VARIANT (com.sun.jna.platform.win32.Variant.VARIANT)5 DATE (com.sun.jna.platform.win32.OaIdl.DATE)4 SAFEARRAY (com.sun.jna.platform.win32.OaIdl.SAFEARRAY)3 VT_BSTR (com.sun.jna.platform.win32.Variant.VT_BSTR)3 VT_DATE (com.sun.jna.platform.win32.Variant.VT_DATE)3 Date (java.util.Date)3 Test (org.junit.Test)3 ComObject (com.sun.jna.platform.win32.COM.util.annotation.ComObject)2 VARIANT_BOOL (com.sun.jna.platform.win32.OaIdl.VARIANT_BOOL)2 VT_VARIANT (com.sun.jna.platform.win32.Variant.VT_VARIANT)2 BOOL (com.sun.jna.platform.win32.WinDef.BOOL)2 BYTE (com.sun.jna.platform.win32.WinDef.BYTE)2 CHAR (com.sun.jna.platform.win32.WinDef.CHAR)2 LONG (com.sun.jna.platform.win32.WinDef.LONG)2 SCODE (com.sun.jna.platform.win32.WinDef.SCODE)2 SHORT (com.sun.jna.platform.win32.WinDef.SHORT)2 UINT (com.sun.jna.platform.win32.WinDef.UINT)2 UINTByReference (com.sun.jna.platform.win32.WinDef.UINTByReference)2 HRESULT (com.sun.jna.platform.win32.WinNT.HRESULT)2