Search in sources :

Example 16 with LONG

use of com.sun.jna.platform.win32.WinDef.LONG in project jna by java-native-access.

the class KeyHook method main.

public static void main(String[] args) {
    final User32 lib = User32.INSTANCE;
    HMODULE hMod = Kernel32.INSTANCE.GetModuleHandle(null);
    keyboardHook = new LowLevelKeyboardProc() {

        @Override
        public LRESULT callback(int nCode, WPARAM wParam, KBDLLHOOKSTRUCT info) {
            if (nCode >= 0) {
                switch(wParam.intValue()) {
                    case WinUser.WM_KEYUP:
                    case WinUser.WM_KEYDOWN:
                    case WinUser.WM_SYSKEYUP:
                    case WinUser.WM_SYSKEYDOWN:
                        System.err.println("in callback, key=" + info.vkCode);
                        if (info.vkCode == 81) {
                            quit = true;
                        }
                }
            }
            Pointer ptr = info.getPointer();
            long peer = Pointer.nativeValue(ptr);
            return lib.CallNextHookEx(hhk, nCode, wParam, new LPARAM(peer));
        }
    };
    hhk = lib.SetWindowsHookEx(WinUser.WH_KEYBOARD_LL, keyboardHook, hMod, 0);
    System.out.println("Keyboard hook installed, type anywhere, 'q' to quit");
    new Thread() {

        @Override
        public void run() {
            while (!quit) {
                try {
                    Thread.sleep(10);
                } catch (Exception e) {
                }
            }
            System.err.println("unhook and exit");
            lib.UnhookWindowsHookEx(hhk);
            System.exit(0);
        }
    }.start();
    // This bit never returns from GetMessage
    int result;
    MSG msg = new MSG();
    while ((result = lib.GetMessage(msg, null, 0, 0)) != 0) {
        if (result == -1) {
            System.err.println("error in get message");
            break;
        } else {
            System.err.println("got message");
            lib.TranslateMessage(msg);
            lib.DispatchMessage(msg);
        }
    }
    lib.UnhookWindowsHookEx(hhk);
}
Also used : MSG(com.sun.jna.platform.win32.WinUser.MSG) KBDLLHOOKSTRUCT(com.sun.jna.platform.win32.WinUser.KBDLLHOOKSTRUCT) Pointer(com.sun.jna.Pointer) HMODULE(com.sun.jna.platform.win32.WinDef.HMODULE) WPARAM(com.sun.jna.platform.win32.WinDef.WPARAM) LowLevelKeyboardProc(com.sun.jna.platform.win32.WinUser.LowLevelKeyboardProc) LRESULT(com.sun.jna.platform.win32.WinDef.LRESULT) LPARAM(com.sun.jna.platform.win32.WinDef.LPARAM) User32(com.sun.jna.platform.win32.User32)

Example 17 with LONG

use of com.sun.jna.platform.win32.WinDef.LONG in project jna by java-native-access.

the class VariantTest method testVariantConstructors.

public void testVariantConstructors() {
    VARIANT variant;
    // skipped: BSTRByReference constructor
    // skipped: empty constructor
    // skipped: pointer constructor
    // skipped: IDispatch constructor
    String testString = "TeST$ö";
    BSTR bstr = OleAuto.INSTANCE.SysAllocString(testString);
    variant = new VARIANT(bstr);
    assertThat(variant.getValue(), instanceOf(BSTR.class));
    assertThat(((BSTR) variant.getValue()).getValue(), equalTo(testString));
    assertThat(variant.stringValue(), equalTo(testString));
    variant = new VARIANT(testString);
    assertThat(variant.getValue(), instanceOf(BSTR.class));
    assertThat(((BSTR) variant.getValue()).getValue(), equalTo(testString));
    assertThat(variant.stringValue(), equalTo(testString));
    OleAuto.INSTANCE.SysFreeString(bstr);
    OleAuto.INSTANCE.SysFreeString((BSTR) variant.getValue());
    BOOL boolTrue = new WinDef.BOOL(true);
    variant = new VARIANT(Variant.VARIANT_TRUE);
    assertThat(variant.getValue(), instanceOf(VARIANT_BOOL.class));
    assertThat(((VARIANT_BOOL) variant.getValue()).shortValue(), equalTo((short) 0xFFFF));
    assertThat(variant.booleanValue(), equalTo(true));
    variant = new VARIANT(boolTrue);
    assertThat(variant.getValue(), instanceOf(VARIANT_BOOL.class));
    assertThat(((VARIANT_BOOL) variant.getValue()).shortValue(), equalTo((short) 0xFFFF));
    assertThat(variant.booleanValue(), equalTo(true));
    int testInt = 4223;
    LONG testIntWin = new LONG(testInt);
    variant = new VARIANT(testIntWin);
    assertThat(variant.getValue(), instanceOf(LONG.class));
    assertThat(((LONG) variant.getValue()).intValue(), equalTo(testInt));
    assertThat(variant.intValue(), equalTo(testInt));
    variant = new VARIANT(testInt);
    assertThat(variant.getValue(), instanceOf(LONG.class));
    assertThat(((LONG) variant.getValue()).intValue(), equalTo(testInt));
    assertThat(variant.intValue(), equalTo(testInt));
    short testShort = 23;
    SHORT testShortWin = new SHORT(testShort);
    variant = new VARIANT(testShortWin);
    assertThat(variant.getValue(), instanceOf(SHORT.class));
    assertThat(((SHORT) variant.getValue()).shortValue(), equalTo(testShort));
    assertThat(variant.shortValue(), equalTo(testShort));
    variant = new VARIANT(testShort);
    assertThat(variant.getValue(), instanceOf(SHORT.class));
    assertThat(((SHORT) variant.getValue()).shortValue(), equalTo(testShort));
    assertThat(variant.shortValue(), equalTo(testShort));
    long testLong = 4223L + Integer.MAX_VALUE;
    variant = new VARIANT(testLong);
    assertThat(variant.getValue(), instanceOf(LONGLONG.class));
    assertThat(((LONGLONG) variant.getValue()).longValue(), equalTo(testLong));
    assertThat(variant.longValue(), equalTo(testLong));
    Date testDate = new Date(2042 - 1900, 2, 3, 23, 0, 0);
    variant = new VARIANT(testDate);
    assertThat(variant.getValue(), instanceOf(DATE.class));
    assertThat(variant.dateValue(), equalTo(testDate));
    byte testByte = 42;
    BYTE testByteWin = new BYTE(testByte);
    CHAR testByteWin2 = new CHAR(testByte);
    variant = new VARIANT(testByte);
    assertThat(variant.getValue(), instanceOf(BYTE.class));
    assertThat(((BYTE) variant.getValue()).byteValue(), equalTo(testByte));
    assertThat(variant.byteValue(), equalTo(testByte));
    variant = new VARIANT(testByteWin);
    assertThat(variant.getValue(), instanceOf(BYTE.class));
    assertThat(((BYTE) variant.getValue()).byteValue(), equalTo(testByte));
    assertThat(variant.byteValue(), equalTo(testByte));
    variant = new VARIANT(testByteWin2);
    assertThat(variant.getValue(), instanceOf(CHAR.class));
    assertThat(((CHAR) variant.getValue()).byteValue(), equalTo(testByte));
    assertThat(variant.byteValue(), equalTo(testByte));
    variant = new VARIANT(testByteWin2);
    assertThat(variant.getValue(), instanceOf(CHAR.class));
    assertThat(((CHAR) variant.getValue()).byteValue(), equalTo(testByte));
    assertThat(variant.byteValue(), equalTo(testByte));
    double testDouble = 42.23;
    variant = new VARIANT(testDouble);
    assertThat(variant.getValue(), instanceOf(Double.class));
    // If this fails introduce comparison with range
    assertThat(variant.doubleValue(), equalTo(testDouble));
    float testFloat = 42.23f;
    variant = new VARIANT(testFloat);
    assertThat(variant.getValue(), instanceOf(Float.class));
    // If this fails introduce comparison with range
    assertThat(variant.floatValue(), equalTo(testFloat));
    char testChar = 42 + Short.MAX_VALUE;
    variant = new VARIANT(testChar);
    assertThat(variant.getValue(), instanceOf(USHORT.class));
    assertThat(((USHORT) variant.getValue()).intValue(), equalTo((int) testChar));
    assertThat(variant.intValue(), equalTo((int) testChar));
}
Also used : 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) Date(java.util.Date) SHORT(com.sun.jna.platform.win32.WinDef.SHORT) USHORT(com.sun.jna.platform.win32.WinDef.USHORT) BSTR(com.sun.jna.platform.win32.WTypes.BSTR) DATE(com.sun.jna.platform.win32.OaIdl.DATE) LONGLONG(com.sun.jna.platform.win32.WinDef.LONGLONG) VARIANT_BOOL(com.sun.jna.platform.win32.OaIdl.VARIANT_BOOL) BOOL(com.sun.jna.platform.win32.WinDef.BOOL) BYTE(com.sun.jna.platform.win32.WinDef.BYTE) USHORT(com.sun.jna.platform.win32.WinDef.USHORT) LONG(com.sun.jna.platform.win32.WinDef.LONG) LONGLONG(com.sun.jna.platform.win32.WinDef.LONGLONG)

Example 18 with LONG

use of com.sun.jna.platform.win32.WinDef.LONG in project jna by java-native-access.

the class IFileSystem3 method testConvertIntTypes.

@Test
public void testConvertIntTypes() {
    LONG testLONG = new LONG(42);
    VARIANT resultLONG = Convert.toVariant(testLONG);
    assertEquals(42, resultLONG.longValue());
    assertEquals(Integer.class, Convert.toJavaObject(resultLONG, Object.class, fact, false, false).getClass());
    assertEquals(42, Convert.toJavaObject(resultLONG, int.class, fact, false, false));
    assertEquals(42, Convert.toJavaObject(resultLONG, Integer.class, fact, false, false));
    SHORT testSHORT = new SHORT(42);
    VARIANT resultSHORT = Convert.toVariant(testSHORT);
    assertEquals(42, resultSHORT.longValue());
    assertEquals(Short.class, Convert.toJavaObject(resultSHORT, Object.class, fact, false, false).getClass());
    assertEquals((short) 42, Convert.toJavaObject(resultSHORT, short.class, fact, false, false));
    assertEquals((short) 42, Convert.toJavaObject(resultSHORT, Short.class, fact, false, false));
    BYTE testBYTE = new BYTE(42);
    VARIANT resultBYTE = Convert.toVariant(testBYTE);
    Byte testByteObj = 42;
    VARIANT resultByteObj = Convert.toVariant(testByteObj);
    byte testByte = 42;
    VARIANT resultByte = Convert.toVariant(testByte);
    assertEquals(42, resultBYTE.longValue());
    assertEquals(42, resultByteObj.longValue());
    assertEquals(42, resultByte.longValue());
    assertEquals(Byte.class, Convert.toJavaObject(resultBYTE, Object.class, fact, false, false).getClass());
    assertEquals(Byte.class, Convert.toJavaObject(resultByteObj, Object.class, fact, false, false).getClass());
    assertEquals(Byte.class, Convert.toJavaObject(resultByte, Object.class, fact, false, false).getClass());
    assertEquals((byte) 42, Convert.toJavaObject(resultBYTE, byte.class, fact, false, false));
    assertEquals((byte) 42, Convert.toJavaObject(resultByteObj, byte.class, fact, false, false));
    assertEquals((byte) 42, Convert.toJavaObject(resultByte, byte.class, fact, false, false));
    assertEquals((byte) 42, Convert.toJavaObject(resultBYTE, Byte.class, fact, false, false));
    assertEquals((byte) 42, Convert.toJavaObject(resultByteObj, Byte.class, fact, false, false));
    assertEquals((byte) 42, Convert.toJavaObject(resultByte, Byte.class, fact, false, false));
    Character testCharObj = 42;
    VARIANT resultCharObj = Convert.toVariant(testCharObj);
    char testChar = 42;
    VARIANT resultChar = Convert.toVariant(testChar);
    assertEquals(42, resultCharObj.longValue());
    assertEquals(42, resultChar.longValue());
    assertEquals(testCharObj, (Character) Convert.toJavaObject(resultCharObj, Object.class, fact, false, false));
    assertEquals(testCharObj, (Character) Convert.toJavaObject(resultChar, Object.class, fact, false, false));
    assertEquals(testCharObj, (Character) Convert.toJavaObject(resultCharObj, char.class, fact, false, false));
    assertEquals(testCharObj, (Character) Convert.toJavaObject(resultChar, char.class, fact, false, false));
    assertEquals(testCharObj, (Character) Convert.toJavaObject(resultCharObj, Character.class, fact, false, false));
    assertEquals(testCharObj, (Character) Convert.toJavaObject(resultChar, Character.class, fact, false, false));
    CHAR testCHAR = new CHAR(42);
    VARIANT resultCHAR = Convert.toVariant(testCHAR);
    assertEquals(42, resultCHAR.longValue());
    assertEquals((byte) 42, Convert.toJavaObject(resultCHAR, Object.class, fact, false, false));
    assertEquals((byte) 42, Convert.toJavaObject(resultCHAR, byte.class, fact, false, false));
    assertEquals((byte) 42, Convert.toJavaObject(resultCHAR, Byte.class, fact, false, false));
    Short testShortObj = 42;
    VARIANT resultShortObj = Convert.toVariant(testShortObj);
    short testShort = 42;
    VARIANT resultShort = Convert.toVariant(testShort);
    assertEquals(42, resultShortObj.longValue());
    assertEquals(42, resultShort.longValue());
    assertEquals((short) 42, Convert.toJavaObject(resultShortObj, Object.class, fact, false, false));
    assertEquals((short) 42, Convert.toJavaObject(resultShort, Object.class, fact, false, false));
    assertEquals((short) 42, Convert.toJavaObject(resultShortObj, short.class, fact, false, false));
    assertEquals((short) 42, Convert.toJavaObject(resultShort, short.class, fact, false, false));
    assertEquals((short) 42, Convert.toJavaObject(resultShortObj, Short.class, fact, false, false));
    assertEquals((short) 42, Convert.toJavaObject(resultShort, Short.class, fact, false, false));
    Integer testIntegerObj = 42;
    VARIANT resultIntegerObj = Convert.toVariant(testIntegerObj);
    int testInteger = 42;
    VARIANT resultInteger = Convert.toVariant(testInteger);
    assertEquals(42, resultIntegerObj.longValue());
    assertEquals(42, resultInteger.longValue());
    assertEquals((int) 42, Convert.toJavaObject(resultIntegerObj, Object.class, fact, false, false));
    assertEquals((int) 42, Convert.toJavaObject(resultInteger, Object.class, fact, false, false));
    assertEquals((int) 42, Convert.toJavaObject(resultIntegerObj, int.class, fact, false, false));
    assertEquals((int) 42, Convert.toJavaObject(resultInteger, int.class, fact, false, false));
    assertEquals((int) 42, Convert.toJavaObject(resultIntegerObj, Integer.class, fact, false, false));
    assertEquals((int) 42, Convert.toJavaObject(resultInteger, Integer.class, fact, false, false));
    Long testLongObj = 42L;
    VARIANT resultLongObj = Convert.toVariant(testLongObj);
    long testLong = 42;
    VARIANT resultLong = Convert.toVariant(testLong);
    assertEquals(42, resultLongObj.longValue());
    assertEquals(42, resultLong.longValue());
    assertEquals((long) 42, Convert.toJavaObject(resultLongObj, Object.class, fact, false, false));
    assertEquals((long) 42, Convert.toJavaObject(resultLong, Object.class, fact, false, false));
    assertEquals((long) 42, Convert.toJavaObject(resultLongObj, long.class, fact, false, false));
    assertEquals((long) 42, Convert.toJavaObject(resultLong, long.class, fact, false, false));
    assertEquals((long) 42, Convert.toJavaObject(resultLongObj, Long.class, fact, false, false));
    assertEquals((long) 42, Convert.toJavaObject(resultLong, Long.class, fact, false, false));
}
Also used : CHAR(com.sun.jna.platform.win32.WinDef.CHAR) VARIANT(com.sun.jna.platform.win32.Variant.VARIANT) SHORT(com.sun.jna.platform.win32.WinDef.SHORT) BYTE(com.sun.jna.platform.win32.WinDef.BYTE) ComObject(com.sun.jna.platform.win32.COM.util.annotation.ComObject) LONG(com.sun.jna.platform.win32.WinDef.LONG) Test(org.junit.Test)

Example 19 with LONG

use of com.sun.jna.platform.win32.WinDef.LONG 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
}
Also used : SAFEARRAY(com.sun.jna.platform.win32.OaIdl.SAFEARRAY) Pointer(com.sun.jna.Pointer) IConnectionPoint(com.sun.jna.platform.win32.COM.util.IConnectionPoint) Date(java.util.Date) 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) ComObject(com.sun.jna.platform.win32.COM.util.annotation.ComObject) Test(org.junit.Test)

Example 20 with LONG

use of com.sun.jna.platform.win32.WinDef.LONG in project jna by java-native-access.

the class SAFEARRAYTest method testPerformance.

@Ignore("Only for live testing")
@Test
public void testPerformance() {
    ObjectFactory fact = new ObjectFactory();
    // Open a record set with a sample search (basicly get the first five
    // entries from the search index
    Connection conn = fact.createObject(Connection.class);
    conn.Open("Provider=Search.CollatorDSO;Extended Properties='Application=Windows';", "", "", -1);
    Recordset recordset = fact.createObject(Recordset.class);
    recordset.Open("SELECT TOP 500 System.ItemPathDisplay, System.ItemName, System.ItemUrl, System.DateCreated FROM SYSTEMINDEX ORDER BY System.ItemUrl", conn, CursorTypeEnum.adOpenUnspecified, LockTypeEnum.adLockUnspecified, -1);
    SAFEARRAY wrap = recordset.GetRows();
    assertThat(wrap.getDimensionCount(), is(2));
    long timeDirect = 0;
    long timeGetElement = 0;
    long timePointer = 0;
    long timeHelper = 0;
    long start, end;
    for (int i = 0; i < 4 * 10; i++) {
        if (i % 4 == 0) {
            start = System.currentTimeMillis();
            toArrayPtrToElement(wrap);
            end = System.currentTimeMillis();
            timePointer += (end - start);
        } else if (i % 4 == 1) {
            start = System.currentTimeMillis();
            toArrayGetElement(wrap);
            end = System.currentTimeMillis();
            timeGetElement += (end - start);
        } else if (i % 4 == 2) {
            start = System.currentTimeMillis();
            toArrayDirect(wrap);
            end = System.currentTimeMillis();
            timeDirect += (end - start);
        } else if (i % 4 == 3) {
            start = System.currentTimeMillis();
            OaIdlUtil.toPrimitiveArray(wrap, false);
            end = System.currentTimeMillis();
            timeHelper += (end - start);
        }
    }
    System.out.println("Direct: " + timeDirect + " ms");
    System.out.println("GetElement: " + timeGetElement + " ms");
    System.out.println("Pointer: " + timePointer + " ms");
    System.out.println("Helper: " + timeHelper + " ms");
    recordset.Close();
    conn.Close();
    fact.disposeAll();
}
Also used : ObjectFactory(com.sun.jna.platform.win32.COM.util.ObjectFactory) SAFEARRAY(com.sun.jna.platform.win32.OaIdl.SAFEARRAY) IConnectionPoint(com.sun.jna.platform.win32.COM.util.IConnectionPoint) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

VARIANT (com.sun.jna.platform.win32.Variant.VARIANT)6 Pointer (com.sun.jna.Pointer)5 Date (java.util.Date)5 LONG (com.sun.jna.platform.win32.WinDef.LONG)4 Test (org.junit.Test)4 ComObject (com.sun.jna.platform.win32.COM.util.annotation.ComObject)3 DATE (com.sun.jna.platform.win32.OaIdl.DATE)3 SAFEARRAY (com.sun.jna.platform.win32.OaIdl.SAFEARRAY)3 BSTR (com.sun.jna.platform.win32.WTypes.BSTR)3 BYTE (com.sun.jna.platform.win32.WinDef.BYTE)3 CHAR (com.sun.jna.platform.win32.WinDef.CHAR)3 SHORT (com.sun.jna.platform.win32.WinDef.SHORT)3 IntByReference (com.sun.jna.ptr.IntByReference)3 Memory (com.sun.jna.Memory)2 COMException (com.sun.jna.platform.win32.COM.COMException)2 IConnectionPoint (com.sun.jna.platform.win32.COM.util.IConnectionPoint)2 VARIANT_BOOL (com.sun.jna.platform.win32.OaIdl.VARIANT_BOOL)2 VT_BSTR (com.sun.jna.platform.win32.Variant.VT_BSTR)2 VT_DATE (com.sun.jna.platform.win32.Variant.VT_DATE)2 FILETIME (com.sun.jna.platform.win32.WinBase.FILETIME)2