Search in sources :

Example 36 with Kernel32

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

the class Kernel32Test method testFileTimeFromLargeInteger.

/**
     * Test FILETIME's LARGE_INTEGER constructor
     * @throws IOException
     */
public final void testFileTimeFromLargeInteger() throws IOException {
    File tmp = File.createTempFile("testGetFileInformationByHandleEx", "jna");
    tmp.deleteOnExit();
    HANDLE hFile = Kernel32.INSTANCE.CreateFile(tmp.getAbsolutePath(), WinNT.GENERIC_WRITE, 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);
        FILETIME ft = new FILETIME(fbi.LastWriteTime);
        SYSTEMTIME stUTC = new SYSTEMTIME();
        SYSTEMTIME stLocal = new SYSTEMTIME();
        Kernel32.INSTANCE.FileTimeToSystemTime(ft, stUTC);
        // Covert to local
        Kernel32.INSTANCE.SystemTimeToTzSpecificLocalTime(null, stUTC, stLocal);
        FileTime calculatedCreateTime = FileTime.fromMillis(stLocal.toCalendar().getTimeInMillis());
        // Actual file's createTime
        FileTime createTime = Files.getLastModifiedTime(Paths.get(tmp.getAbsolutePath()));
        assertEquals(createTime.toMillis(), calculatedCreateTime.toMillis());
    } finally {
        Kernel32.INSTANCE.CloseHandle(hFile);
    }
}
Also used : FILETIME(com.sun.jna.platform.win32.WinBase.FILETIME) Memory(com.sun.jna.Memory) SYSTEMTIME(com.sun.jna.platform.win32.WinBase.SYSTEMTIME) DWORD(com.sun.jna.platform.win32.WinDef.DWORD) FileTime(java.nio.file.attribute.FileTime) 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 37 with Kernel32

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

the class Kernel32Test method testWriteProcessMemory.

public void testWriteProcessMemory() {
    Kernel32 kernel = Kernel32.INSTANCE;
    boolean successWrite = kernel.WriteProcessMemory(null, Pointer.NULL, Pointer.NULL, 1, null);
    assertFalse(successWrite);
    assertEquals(kernel.GetLastError(), WinError.ERROR_INVALID_HANDLE);
    ByteBuffer bufDest = ByteBuffer.allocateDirect(4);
    bufDest.put(new byte[] { 0, 1, 2, 3 });
    ByteBuffer bufSrc = ByteBuffer.allocateDirect(4);
    bufSrc.put(new byte[] { 5, 10, 15, 20 });
    Pointer ptrSrc = Native.getDirectBufferPointer(bufSrc);
    Pointer ptrDest = Native.getDirectBufferPointer(bufDest);
    HANDLE selfHandle = kernel.GetCurrentProcess();
    //Write only the first three
    kernel.WriteProcessMemory(selfHandle, ptrDest, ptrSrc, 3, null);
    assertEquals(bufDest.get(0), 5);
    assertEquals(bufDest.get(1), 10);
    assertEquals(bufDest.get(2), 15);
    assertEquals(bufDest.get(3), 3);
}
Also used : Pointer(com.sun.jna.Pointer) ByteBuffer(java.nio.ByteBuffer) HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE)

Example 38 with Kernel32

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

the class Kernel32Test method testOpenThread.

public void testOpenThread() {
    HANDLE h = Kernel32.INSTANCE.OpenThread(WinNT.THREAD_ALL_ACCESS, false, Kernel32.INSTANCE.GetCurrentThreadId());
    assertNotNull(h);
    assertFalse(h.equals(0));
    Kernel32Util.closeHandle(h);
}
Also used : HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE)

Example 39 with Kernel32

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

the class Kernel32VolumeManagementFunctionsTest method testEnumVolumes.

@Test
public void testEnumVolumes() {
    char[] lpszVolumeName = new char[WinDef.MAX_PATH + 1];
    HANDLE hFindVolume = assertValidHandle("FindFirstVolume", Kernel32.INSTANCE.FindFirstVolume(lpszVolumeName, lpszVolumeName.length));
    try {
        int foundPaths = 0;
        do {
            String volumeGUID = Native.toString(lpszVolumeName);
            testEnumVolumeMountMoints(volumeGUID);
            foundPaths += testGetVolumePathNamesForVolumeName(volumeGUID);
        } while (Kernel32.INSTANCE.FindNextVolume(hFindVolume, lpszVolumeName, lpszVolumeName.length));
        assertTrue("No paths were found", foundPaths > 0);
        int hr = Kernel32.INSTANCE.GetLastError();
        assertEquals("Bad volumes enum termination reason", WinError.ERROR_NO_MORE_FILES, hr);
    } finally {
        assertCallSucceeded("FindVolumeClose", Kernel32.INSTANCE.FindVolumeClose(hFindVolume));
    }
}
Also used : HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE) Test(org.junit.Test)

Example 40 with Kernel32

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

the class Kernel32Test method testDeviceIoControlFsctlCompression.

public void testDeviceIoControlFsctlCompression() throws IOException {
    File tmp = File.createTempFile("testDeviceIoControlFsctlCompression", "jna");
    tmp.deleteOnExit();
    HANDLE hFile = Kernel32.INSTANCE.CreateFile(tmp.getAbsolutePath(), WinNT.GENERIC_ALL, WinNT.FILE_SHARE_READ, new WinBase.SECURITY_ATTRIBUTES(), WinNT.OPEN_EXISTING, WinNT.FILE_ATTRIBUTE_NORMAL, null);
    assertFalse(WinBase.INVALID_HANDLE_VALUE.equals(hFile));
    try {
        ShortByReference lpBuffer = new ShortByReference();
        IntByReference lpBytes = new IntByReference();
        if (false == Kernel32.INSTANCE.DeviceIoControl(hFile, FSCTL_GET_COMPRESSION, null, 0, lpBuffer.getPointer(), USHORT.SIZE, lpBytes, null)) {
            fail("DeviceIoControl failed with " + Kernel32.INSTANCE.GetLastError());
        }
        assertEquals(WinNT.COMPRESSION_FORMAT_NONE, lpBuffer.getValue());
        assertEquals(USHORT.SIZE, lpBytes.getValue());
        lpBuffer = new ShortByReference((short) WinNT.COMPRESSION_FORMAT_LZNT1);
        if (false == Kernel32.INSTANCE.DeviceIoControl(hFile, FSCTL_SET_COMPRESSION, lpBuffer.getPointer(), USHORT.SIZE, null, 0, lpBytes, null)) {
            fail("DeviceIoControl failed with " + Kernel32.INSTANCE.GetLastError());
        }
        if (false == Kernel32.INSTANCE.DeviceIoControl(hFile, FSCTL_GET_COMPRESSION, null, 0, lpBuffer.getPointer(), USHORT.SIZE, lpBytes, null)) {
            fail("DeviceIoControl failed with " + Kernel32.INSTANCE.GetLastError());
        }
        assertEquals(WinNT.COMPRESSION_FORMAT_LZNT1, lpBuffer.getValue());
        assertEquals(USHORT.SIZE, lpBytes.getValue());
    } finally {
        Kernel32Util.closeHandle(hFile);
    }
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) ShortByReference(com.sun.jna.ptr.ShortByReference) File(java.io.File) HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE)

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