Search in sources :

Example 1 with WIN32_FIND_DATA

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

the class Kernel32Test method testFindFirstFileExFindExInfoBasic.

public void testFindFirstFileExFindExInfoBasic() throws IOException {
    Path tmpDir = Files.createTempDirectory("testFindFirstFileExFindExInfoBasic");
    File tmpFile = new File(Files.createTempFile(tmpDir, "testFindFirstFileExFindExInfoBasic", ".jna").toString());
    Memory p = new Memory(WIN32_FIND_DATA.sizeOf());
    // Add the file name to the search to get just that one entry
    HANDLE hFile = Kernel32.INSTANCE.FindFirstFileEx(tmpDir.toAbsolutePath().toString() + "\\" + tmpFile.getName(), WinBase.FindExInfoBasic, 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 file itself
        WIN32_FIND_DATA fd = new WIN32_FIND_DATA(p);
        String actualFileName = new String(fd.getFileName());
        actualFileName = new String(fd.getFileName());
        assertTrue(actualFileName.contentEquals(tmpFile.getName()));
        // FindExInfoBasic does not return the short name, so confirm that its empty
        String alternateFileName = fd.getAlternateFileName();
        assertTrue(alternateFileName.isEmpty());
        // 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 2 with WIN32_FIND_DATA

use of com.sun.jna.platform.win32.WinBase.WIN32_FIND_DATA 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 3 with WIN32_FIND_DATA

use of com.sun.jna.platform.win32.WinBase.WIN32_FIND_DATA 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)

Aggregations

Memory (com.sun.jna.Memory)3 WIN32_FIND_DATA (com.sun.jna.platform.win32.WinBase.WIN32_FIND_DATA)3 HANDLE (com.sun.jna.platform.win32.WinNT.HANDLE)3 File (java.io.File)3 Path (java.nio.file.Path)3 DWORD (com.sun.jna.platform.win32.WinDef.DWORD)2