Search in sources :

Example 36 with DWORD

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

the class Kernel32DiskManagementFunctionsTest method testGetDiskFreeSpace.

private void testGetDiskFreeSpace(String lpRootPathName) {
    DWORDByReference lpSectorsPerCluster = new DWORDByReference();
    DWORDByReference lpBytesPerSector = new DWORDByReference();
    DWORDByReference lpNumberOfFreeClusters = new DWORDByReference();
    DWORDByReference lpTotalNumberOfClusters = new DWORDByReference();
    assertCallSucceeded("GetDiskFreeSpace(" + lpRootPathName + ")", Kernel32.INSTANCE.GetDiskFreeSpace(lpRootPathName, lpSectorsPerCluster, lpBytesPerSector, lpNumberOfFreeClusters, lpTotalNumberOfClusters));
    //        System.out.append(getCurrentTestName()).append('[').append(lpRootPathName).println(']');
    //        System.out.append('\t').append("SectorsPerCluster: ").println(lpSectorsPerCluster.getValue());
    //        System.out.append('\t').append("BytesPerSector: ").println(lpBytesPerSector.getValue());
    //        System.out.append('\t').append("NumberOfFreeClusters: ").println(lpNumberOfFreeClusters.getValue());
    //        System.out.append('\t').append("TotalNumberOfClusters: ").println(lpTotalNumberOfClusters.getValue());
    DWORD freeSize = lpNumberOfFreeClusters.getValue();
    assertTrue("No free clusters for " + lpRootPathName, freeSize.longValue() > 0L);
    DWORD totalSize = lpTotalNumberOfClusters.getValue();
    assertTrue("Free clusters (" + freeSize + ") not below total (" + totalSize + ") for " + lpRootPathName, freeSize.longValue() < totalSize.longValue());
}
Also used : DWORDByReference(com.sun.jna.platform.win32.WinDef.DWORDByReference) DWORD(com.sun.jna.platform.win32.WinDef.DWORD)

Example 37 with DWORD

use of com.sun.jna.platform.win32.WinDef.DWORD 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 38 with DWORD

use of com.sun.jna.platform.win32.WinDef.DWORD 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 39 with DWORD

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

the class Kernel32Test method testSetFileInformationByHandleFileBasicInfo.

public void testSetFileInformationByHandleFileBasicInfo() throws IOException, InterruptedException {
    File tmp = File.createTempFile("testSetFileInformationByHandleFileBasicInfo", "jna");
    tmp.deleteOnExit();
    HANDLE hFile = Kernel32.INSTANCE.CreateFile(tmp.getAbsolutePath(), WinNT.GENERIC_READ | WinNT.GENERIC_WRITE, WinNT.FILE_SHARE_READ | WinNT.FILE_SHARE_WRITE, new WinBase.SECURITY_ATTRIBUTES(), WinNT.OPEN_EXISTING, WinNT.FILE_ATTRIBUTE_NORMAL, null);
    assertFalse(WinBase.INVALID_HANDLE_VALUE.equals(hFile));
    try {
        Memory p = new Memory(FILE_BASIC_INFO.sizeOf());
        if (false == Kernel32.INSTANCE.GetFileInformationByHandleEx(hFile, WinBase.FileBasicInfo, p, new DWORD(p.size())))
            fail("GetFileInformationByHandleEx failed with " + Kernel32.INSTANCE.GetLastError());
        FILE_BASIC_INFO fbi = new FILE_BASIC_INFO(p);
        // Add TEMP attribute
        fbi.FileAttributes = fbi.FileAttributes | WinNT.FILE_ATTRIBUTE_TEMPORARY;
        fbi.ChangeTime = new WinNT.LARGE_INTEGER(0);
        fbi.CreationTime = new WinNT.LARGE_INTEGER(0);
        fbi.LastAccessTime = new WinNT.LARGE_INTEGER(0);
        fbi.LastWriteTime = new WinNT.LARGE_INTEGER(0);
        fbi.write();
        if (false == Kernel32.INSTANCE.SetFileInformationByHandle(hFile, WinBase.FileBasicInfo, fbi.getPointer(), new DWORD(FILE_BASIC_INFO.sizeOf())))
            fail("GetFileInformationByHandleEx failed with " + Kernel32.INSTANCE.GetLastError());
        if (false == Kernel32.INSTANCE.GetFileInformationByHandleEx(hFile, WinBase.FileBasicInfo, p, new DWORD(p.size())))
            fail("GetFileInformationByHandleEx failed with " + Kernel32.INSTANCE.GetLastError());
        fbi = new FILE_BASIC_INFO(p);
        assertTrue((fbi.FileAttributes & WinNT.FILE_ATTRIBUTE_TEMPORARY) != 0);
    } finally {
        Kernel32.INSTANCE.CloseHandle(hFile);
    }
}
Also used : 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) FILE_BASIC_INFO(com.sun.jna.platform.win32.WinBase.FILE_BASIC_INFO)

Example 40 with DWORD

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

the class Kernel32Test method testSetFileInformationByHandleFileDispositionInfo.

public void testSetFileInformationByHandleFileDispositionInfo() throws IOException, InterruptedException {
    File tmp = File.createTempFile("testSetFileInformationByHandleFileDispositionInfo", "jna");
    HANDLE hFile = Kernel32.INSTANCE.CreateFile(tmp.getAbsolutePath(), WinNT.GENERIC_WRITE | WinNT.DELETE, WinNT.FILE_SHARE_WRITE, new WinBase.SECURITY_ATTRIBUTES(), WinNT.OPEN_EXISTING, WinNT.FILE_ATTRIBUTE_NORMAL, null);
    assertFalse(WinBase.INVALID_HANDLE_VALUE.equals(hFile));
    try {
        FILE_DISPOSITION_INFO fdi = new FILE_DISPOSITION_INFO(true);
        if (false == Kernel32.INSTANCE.SetFileInformationByHandle(hFile, WinBase.FileDispositionInfo, fdi.getPointer(), new DWORD(FILE_DISPOSITION_INFO.sizeOf())))
            fail("SetFileInformationByHandle failed with " + Kernel32.INSTANCE.GetLastError());
    } finally {
        Kernel32.INSTANCE.CloseHandle(hFile);
    }
    assertFalse(Files.exists(Paths.get(tmp.getAbsolutePath())));
}
Also used : FILE_DISPOSITION_INFO(com.sun.jna.platform.win32.WinBase.FILE_DISPOSITION_INFO) DWORD(com.sun.jna.platform.win32.WinDef.DWORD) File(java.io.File) HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE)

Aggregations

DWORD (com.sun.jna.platform.win32.WinDef.DWORD)56 PointerByReference (com.sun.jna.ptr.PointerByReference)23 HANDLE (com.sun.jna.platform.win32.WinNT.HANDLE)17 File (java.io.File)17 HRESULT (com.sun.jna.platform.win32.WinNT.HRESULT)16 Test (org.junit.Test)15 DWORDByReference (com.sun.jna.platform.win32.WinDef.DWORDByReference)11 Memory (com.sun.jna.Memory)9 ULONG (com.sun.jna.platform.win32.WinDef.ULONG)9 ULONGByReference (com.sun.jna.platform.win32.WinDef.ULONGByReference)7 HANDLEByReference (com.sun.jna.platform.win32.WinNT.HANDLEByReference)7 GENERIC_MAPPING (com.sun.jna.platform.win32.WinNT.GENERIC_MAPPING)6 UINT_PTR (com.sun.jna.platform.win32.WinDef.UINT_PTR)5 TOKEN_PRIVILEGES (com.sun.jna.platform.win32.WinNT.TOKEN_PRIVILEGES)5 IntByReference (com.sun.jna.ptr.IntByReference)5 APPBARDATA (com.sun.jna.platform.win32.ShellAPI.APPBARDATA)4 WinNT (com.sun.jna.platform.win32.WinNT)4 Pointer (com.sun.jna.Pointer)3 SC_HANDLE (com.sun.jna.platform.win32.Winsvc.SC_HANDLE)3 BufferedWriter (java.io.BufferedWriter)3