Search in sources :

Example 81 with Kernel32

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

the class Kernel32Test method testFindFirstFile.

public void testFindFirstFile() throws IOException {
    Path tmpDir = Files.createTempDirectory("testFindFirstFile");
    File tmpFile = new File(Files.createTempFile(tmpDir, "testFindFirstFile", ".jna").toString());
    Memory p = new Memory(WIN32_FIND_DATA.sizeOf());
    HANDLE hFile = Kernel32.INSTANCE.FindFirstFile(tmpDir.toAbsolutePath().toString() + "\\*", p);
    assertFalse(WinBase.INVALID_HANDLE_VALUE.equals(hFile));
    try {
        // Get data and confirm the 1st name is . for the directory itself.
        WIN32_FIND_DATA fd = new WIN32_FIND_DATA(p);
        String actualFileName = new String(fd.getFileName());
        assertTrue(actualFileName.contentEquals("."));
        // Get data and confirm the 2nd name is .. for the directory's parent
        assertTrue(Kernel32.INSTANCE.FindNextFile(hFile, p));
        fd = new WIN32_FIND_DATA(p);
        actualFileName = new String(fd.getFileName());
        assertTrue(actualFileName.contentEquals(".."));
        // Get data and confirm the 3rd name is the tmp file name
        assertTrue(Kernel32.INSTANCE.FindNextFile(hFile, p));
        fd = new WIN32_FIND_DATA(p);
        actualFileName = new String(fd.getFileName());
        assertTrue(actualFileName.contentEquals(tmpFile.getName()));
        // No more files in directory
        assertFalse(Kernel32.INSTANCE.FindNextFile(hFile, p));
        assertEquals(WinNT.ERROR_NO_MORE_FILES, Kernel32.INSTANCE.GetLastError());
    } finally {
        Kernel32.INSTANCE.FindClose(hFile);
        tmpFile.delete();
        Files.delete(tmpDir);
    }
}
Also used : Path(java.nio.file.Path) Memory(com.sun.jna.Memory) File(java.io.File) HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE) WIN32_FIND_DATA(com.sun.jna.platform.win32.WinBase.WIN32_FIND_DATA)

Example 82 with Kernel32

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

the class Kernel32Test method testSystemTimeToFileTimeAndFileTimeToSystemTime.

/**
     * Test both SystemTimeToFileTime and FileTimeToSystemTime
     * @throws IOException
     */
public final void testSystemTimeToFileTimeAndFileTimeToSystemTime() throws IOException {
    WinBase.SYSTEMTIME systemTime = new WinBase.SYSTEMTIME();
    Kernel32.INSTANCE.GetSystemTime(systemTime);
    WinBase.FILETIME fileTime = new WinBase.FILETIME();
    if (false == Kernel32.INSTANCE.SystemTimeToFileTime(systemTime, fileTime)) {
        fail("SystemTimeToFileTime failed with " + Kernel32.INSTANCE.GetLastError());
    }
    WinBase.SYSTEMTIME newSystemTime = new WinBase.SYSTEMTIME();
    if (false == Kernel32.INSTANCE.FileTimeToSystemTime(fileTime, newSystemTime)) {
        fail("FileTimeToSystemTime failed with " + Kernel32.INSTANCE.GetLastError());
    }
    assertEquals(systemTime.wYear, newSystemTime.wYear);
    assertEquals(systemTime.wDay, newSystemTime.wDay);
    assertEquals(systemTime.wMonth, newSystemTime.wMonth);
    assertEquals(systemTime.wHour, newSystemTime.wHour);
    assertEquals(systemTime.wMinute, newSystemTime.wMinute);
    assertEquals(systemTime.wSecond, newSystemTime.wSecond);
    assertEquals(systemTime.wMilliseconds, newSystemTime.wMilliseconds);
}
Also used : SYSTEMTIME(com.sun.jna.platform.win32.WinBase.SYSTEMTIME) FILETIME(com.sun.jna.platform.win32.WinBase.FILETIME) SYSTEMTIME(com.sun.jna.platform.win32.WinBase.SYSTEMTIME) FILETIME(com.sun.jna.platform.win32.WinBase.FILETIME)

Example 83 with Kernel32

use of com.sun.jna.platform.win32.Kernel32 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 84 with Kernel32

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

the class Kernel32Test method testFindFirstFileExFindExInfoStandard.

public void testFindFirstFileExFindExInfoStandard() throws IOException {
    Path tmpDir = Files.createTempDirectory("testFindFirstFileExFindExInfoStandard");
    File tmpFile = new File(Files.createTempFile(tmpDir, "testFindFirstFileExFindExInfoStandard", ".jna").toString());
    Memory p = new Memory(WIN32_FIND_DATA.sizeOf());
    HANDLE hFile = Kernel32.INSTANCE.FindFirstFileEx(tmpDir.toAbsolutePath().toString() + "\\*", WinBase.FindExInfoStandard, p, WinBase.FindExSearchNameMatch, null, new DWORD(0));
    assertFalse(WinBase.INVALID_HANDLE_VALUE.equals(hFile));
    try {
        // Get data and confirm the 1st name is . for the directory itself.
        WIN32_FIND_DATA fd = new WIN32_FIND_DATA(p);
        String actualFileName = new String(fd.getFileName());
        assertTrue(actualFileName.contentEquals("."));
        // Get data and confirm the 2nd name is .. for the directory's parent
        assertTrue(Kernel32.INSTANCE.FindNextFile(hFile, p));
        fd = new WIN32_FIND_DATA(p);
        actualFileName = new String(fd.getFileName());
        assertTrue(actualFileName.contentEquals(".."));
        // Get data and confirm the 3rd name is the tmp file name
        assertTrue(Kernel32.INSTANCE.FindNextFile(hFile, p));
        fd = new WIN32_FIND_DATA(p);
        actualFileName = new String(fd.getFileName());
        assertTrue(actualFileName.contentEquals(tmpFile.getName()));
        // No more files in directory
        assertFalse(Kernel32.INSTANCE.FindNextFile(hFile, p));
        assertEquals(WinNT.ERROR_NO_MORE_FILES, Kernel32.INSTANCE.GetLastError());
    } finally {
        Kernel32.INSTANCE.FindClose(hFile);
        tmpFile.delete();
        Files.delete(tmpDir);
    }
}
Also used : Path(java.nio.file.Path) Memory(com.sun.jna.Memory) DWORD(com.sun.jna.platform.win32.WinDef.DWORD) File(java.io.File) HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE) WIN32_FIND_DATA(com.sun.jna.platform.win32.WinBase.WIN32_FIND_DATA)

Example 85 with Kernel32

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

the class Kernel32Test method testEnumResourceNames.

public void testEnumResourceNames() {
    // "14" is the type name of the My Computer icon in explorer.exe
    Pointer pointer = new Memory(Native.WCHAR_SIZE * 3);
    pointer.setWideString(0, "14");
    WinBase.EnumResNameProc ernp = new WinBase.EnumResNameProc() {

        @Override
        public boolean invoke(HMODULE module, Pointer type, Pointer name, Pointer lParam) {
            return true;
        }
    };
    // null HMODULE means use this process / its EXE
    // there are no type "14" resources in it.
    boolean result = Kernel32.INSTANCE.EnumResourceNames(null, pointer, ernp, null);
    assertFalse("EnumResourceNames should have failed.", result);
    assertEquals("GetLastError should be set to 1813", WinError.ERROR_RESOURCE_TYPE_NOT_FOUND, Kernel32.INSTANCE.GetLastError());
}
Also used : Memory(com.sun.jna.Memory) Pointer(com.sun.jna.Pointer) HMODULE(com.sun.jna.platform.win32.WinDef.HMODULE)

Aggregations

HANDLE (com.sun.jna.platform.win32.WinNT.HANDLE)74 IntByReference (com.sun.jna.ptr.IntByReference)47 File (java.io.File)33 DWORD (com.sun.jna.platform.win32.WinDef.DWORD)30 SC_HANDLE (com.sun.jna.platform.win32.Winsvc.SC_HANDLE)27 HANDLEByReference (com.sun.jna.platform.win32.WinNT.HANDLEByReference)26 Memory (com.sun.jna.Memory)25 PointerByReference (com.sun.jna.ptr.PointerByReference)14 Pointer (com.sun.jna.Pointer)12 EVT_HANDLE (com.sun.jna.platform.win32.Winevt.EVT_HANDLE)7 Test (org.junit.Test)7 SYSTEMTIME (com.sun.jna.platform.win32.WinBase.SYSTEMTIME)6 ArrayList (java.util.ArrayList)6 FILETIME (com.sun.jna.platform.win32.WinBase.FILETIME)5 HMODULE (com.sun.jna.platform.win32.WinDef.HMODULE)5 PSID (com.sun.jna.platform.win32.WinNT.PSID)5 TOKEN_PRIVILEGES (com.sun.jna.platform.win32.WinNT.TOKEN_PRIVILEGES)5 WString (com.sun.jna.WString)4 DWORDByReference (com.sun.jna.platform.win32.WinDef.DWORDByReference)4 ShortByReference (com.sun.jna.ptr.ShortByReference)4