Search in sources :

Example 11 with LONG

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

the class Advapi32Util method registrySetLongValue.

/**
	 * Set a long value in registry.
	 *
	 * @param root
	 *            Root key.
	 * @param keyPath
	 *            Path to an existing registry key.
	 * @param name
	 *            Value name.
	 * @param value
	 *            Value to write to registry.
	 */
public static void registrySetLongValue(HKEY root, String keyPath, String name, long value) {
    HKEYByReference phkKey = new HKEYByReference();
    int rc = Advapi32.INSTANCE.RegOpenKeyEx(root, keyPath, 0, WinNT.KEY_READ | WinNT.KEY_WRITE, phkKey);
    if (rc != W32Errors.ERROR_SUCCESS) {
        throw new Win32Exception(rc);
    }
    try {
        registrySetLongValue(phkKey.getValue(), name, value);
    } finally {
        rc = Advapi32.INSTANCE.RegCloseKey(phkKey.getValue());
        if (rc != W32Errors.ERROR_SUCCESS) {
            throw new Win32Exception(rc);
        }
    }
}
Also used : HKEYByReference(com.sun.jna.platform.win32.WinReg.HKEYByReference)

Example 12 with LONG

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

the class Kernel32Test method testGetSystemTimes.

public void testGetSystemTimes() {
    Kernel32 kernel = Kernel32.INSTANCE;
    FILETIME lpIdleTime = new FILETIME();
    FILETIME lpKernelTime = new FILETIME();
    FILETIME lpUserTime = new FILETIME();
    boolean succ = kernel.GetSystemTimes(lpIdleTime, lpKernelTime, lpUserTime);
    assertTrue(succ);
    long idleTime = lpIdleTime.toDWordLong().longValue();
    long kernelTime = lpKernelTime.toDWordLong().longValue();
    long userTime = lpUserTime.toDWordLong().longValue();
    // All should be >= 0.  kernel includes idle.
    assertTrue(idleTime >= 0);
    assertTrue(kernelTime >= idleTime);
    assertTrue(userTime >= 0);
}
Also used : FILETIME(com.sun.jna.platform.win32.WinBase.FILETIME)

Example 13 with LONG

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

the class Kernel32Test method testGetProcessList.

public void testGetProcessList() throws IOException {
    HANDLE processEnumHandle = Kernel32.INSTANCE.CreateToolhelp32Snapshot(Tlhelp32.TH32CS_SNAPALL, new WinDef.DWORD(0));
    assertFalse(WinBase.INVALID_HANDLE_VALUE.equals(processEnumHandle));
    try {
        Tlhelp32.PROCESSENTRY32.ByReference processEntry = new Tlhelp32.PROCESSENTRY32.ByReference();
        assertTrue(Kernel32.INSTANCE.Process32First(processEnumHandle, processEntry));
        List<Long> processIdList = new ArrayList<Long>();
        processIdList.add(processEntry.th32ProcessID.longValue());
        while (Kernel32.INSTANCE.Process32Next(processEnumHandle, processEntry)) {
            processIdList.add(processEntry.th32ProcessID.longValue());
        }
        assertTrue(processIdList.size() > 4);
    } finally {
        Kernel32Util.closeHandle(processEnumHandle);
    }
}
Also used : ArrayList(java.util.ArrayList) DWORD(com.sun.jna.platform.win32.WinDef.DWORD) HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE) IntByReference(com.sun.jna.ptr.IntByReference) ShortByReference(com.sun.jna.ptr.ShortByReference) HANDLEByReference(com.sun.jna.platform.win32.WinNT.HANDLEByReference)

Example 14 with LONG

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

the class WevtapiTest method testEvtCreateBookmark.

public void testEvtCreateBookmark() throws Exception {
    EVT_HANDLE queryHandle = null;
    EVT_HANDLE contextHandle = null;
    File testEvtx = new File(getClass().getResource("/res/WevtapiTest.sample1.evtx").toURI());
    StringBuilder sb = new StringBuilder();
    try {
        queryHandle = Wevtapi.INSTANCE.EvtQuery(null, testEvtx.getPath(), null, Winevt.EVT_QUERY_FLAGS.EvtQueryFilePath);
        // test EvtCreateBookmark
        EVT_HANDLE hBookmark = Wevtapi.INSTANCE.EvtCreateBookmark("<BookmarkList><Bookmark Channel='" + testEvtx.getAbsolutePath() + "' RecordId='" + 11 + "' IsCurrent='true'/></BookmarkList>");
        if (hBookmark == null) {
            throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
        }
        // test EvtSeek
        if (!Wevtapi.INSTANCE.EvtSeek(queryHandle, 0L, hBookmark, 0, Winevt.EVT_SEEK_FLAGS.EvtSeekRelativeToBookmark)) {
            throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
        }
        String[] targets = { "Event/System/EventRecordID" };
        contextHandle = Wevtapi.INSTANCE.EvtCreateRenderContext(targets.length, targets, Winevt.EVT_RENDER_CONTEXT_FLAGS.EvtRenderContextValues);
        int eventArraySize = 10;
        int evtNextTimeout = 1000;
        int arrayIndex = 1;
        Memory buff;
        IntByReference propertyCount = new IntByReference();
        Winevt.EVT_VARIANT evtVariant = new Winevt.EVT_VARIANT();
        EVT_HANDLE[] eventArray = new EVT_HANDLE[eventArraySize];
        IntByReference returned = new IntByReference();
        while (Wevtapi.INSTANCE.EvtNext(queryHandle, eventArraySize, eventArray, evtNextTimeout, 0, returned)) {
            for (int i = 0; i < returned.getValue(); i++) {
                EVT_HANDLE evtHandle = eventArray[i];
                try {
                    buff = WevtapiUtil.EvtRender(contextHandle, eventArray[i], Winevt.EVT_RENDER_FLAGS.EvtRenderEventValues, propertyCount);
                    useMemory(evtVariant, buff, 0);
                    assertThat("EventRecordID", (Long) evtVariant.getValue(), is((long) arrayIndex * eventArraySize + i + 1));
                    sb.append(evtVariant.getValue());
                    // test EvtUpdateBookmark
                    if (!Wevtapi.INSTANCE.EvtUpdateBookmark(hBookmark, eventArray[i])) {
                        throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
                    }
                } finally {
                    if (eventArray[i] != null) {
                        Wevtapi.INSTANCE.EvtClose(eventArray[i]);
                    }
                }
            }
            arrayIndex++;
        }
        if (Kernel32.INSTANCE.GetLastError() != WinError.ERROR_SUCCESS && Kernel32.INSTANCE.GetLastError() != WinError.ERROR_NO_MORE_ITEMS) {
            throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
        }
        buff = WevtapiUtil.EvtRender(null, hBookmark, Winevt.EVT_RENDER_FLAGS.EvtRenderBookmark, propertyCount);
        assertThat(buff.getWideString(0), is("<BookmarkList>\r\n  <Bookmark Channel='" + testEvtx.getAbsolutePath() + "' RecordId='" + 20 + "' IsCurrent='true'/>\r\n</BookmarkList>"));
        assertThat(sb.length() > 0, is(true));
    } finally {
        if (queryHandle != null) {
            Wevtapi.INSTANCE.EvtClose(queryHandle);
        }
        if (contextHandle != null) {
            Wevtapi.INSTANCE.EvtClose(contextHandle);
        }
    }
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) Memory(com.sun.jna.Memory) EVT_HANDLE(com.sun.jna.platform.win32.Winevt.EVT_HANDLE) File(java.io.File)

Example 15 with LONG

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

the class WinNTTypesTest method testLargeIntegerUnionLongValue.

@Test
public void testLargeIntegerUnionLongValue() {
    for (long expected : new long[] { Long.MIN_VALUE, Integer.MIN_VALUE, Short.MIN_VALUE, Byte.MIN_VALUE, 0L, Long.MAX_VALUE, Integer.MAX_VALUE, Short.MAX_VALUE, Byte.MAX_VALUE }) {
        LARGE_INTEGER large = new LARGE_INTEGER(expected);
        assertEquals("Mismatched large value", expected, large.getValue());
        LARGE_INTEGER.LowHigh loHi = new LARGE_INTEGER.LowHigh(expected);
        assertEquals("Mismatched low part", loHi.LowPart, large.getLow());
        assertEquals("Mismatched high part", loHi.HighPart, large.getHigh());
    }
}
Also used : LARGE_INTEGER(com.sun.jna.platform.win32.WinNT.LARGE_INTEGER) 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