Search in sources :

Example 86 with Kernel32

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

the class Kernel32Test method testGetSetFileTime.

public void testGetSetFileTime() throws IOException {
    File tmp = File.createTempFile("testGetSetFileTime", "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 {
        WinBase.FILETIME.ByReference creationTime = new WinBase.FILETIME.ByReference();
        WinBase.FILETIME.ByReference accessTime = new WinBase.FILETIME.ByReference();
        WinBase.FILETIME.ByReference modifiedTime = new WinBase.FILETIME.ByReference();
        Kernel32.INSTANCE.GetFileTime(hFile, creationTime, accessTime, modifiedTime);
        assertEquals(creationTime.toDate().getYear(), new Date().getYear());
        assertEquals(accessTime.toDate().getYear(), new Date().getYear());
        assertEquals(modifiedTime.toDate().getYear(), new Date().getYear());
        Kernel32.INSTANCE.SetFileTime(hFile, null, null, new WinBase.FILETIME(new Date(2010, 1, 1)));
        assertEquals(2010, new Date(tmp.lastModified()).getYear());
    } finally {
        Kernel32Util.closeHandle(hFile);
    }
}
Also used : FILETIME(com.sun.jna.platform.win32.WinBase.FILETIME) FILETIME(com.sun.jna.platform.win32.WinBase.FILETIME) File(java.io.File) 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) Date(java.util.Date)

Example 87 with Kernel32

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

the class Kernel32Test method testWaitForMultipleObjects.

public void testWaitForMultipleObjects() {
    HANDLE[] handles = new HANDLE[2];
    try {
        for (int index = 0; index < handles.length; index++) {
            HANDLE h = Kernel32.INSTANCE.CreateEvent(null, false, false, null);
            assertNotNull("Failed to create event #" + index + ": " + Kernel32.INSTANCE.GetLastError(), h);
            handles[index] = h;
        }
        // handle runs into timeout since it is not triggered
        // WAIT_TIMEOUT = 0x00000102
        assertEquals(WinError.WAIT_TIMEOUT, Kernel32.INSTANCE.WaitForMultipleObjects(handles.length, handles, false, 1000));
    } finally {
        Kernel32Util.closeHandles(handles);
    }
    // invalid Handle
    handles[0] = WinBase.INVALID_HANDLE_VALUE;
    handles[1] = Kernel32.INSTANCE.CreateEvent(null, false, false, null);
    assertNotNull("Failed to create valid event: " + Kernel32.INSTANCE.GetLastError(), handles[1]);
    try {
        // returns WAIT_FAILED since handle is invalid
        assertEquals(WinBase.WAIT_FAILED, Kernel32.INSTANCE.WaitForMultipleObjects(handles.length, handles, false, 5000));
    } finally {
        Kernel32Util.closeHandle(handles[1]);
    }
}
Also used : HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE)

Example 88 with Kernel32

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

the class Kernel32Test method testGetCommTimeouts.

public void testGetCommTimeouts() {
    WinBase.COMMTIMEOUTS lpCommTimeouts = new WinBase.COMMTIMEOUTS();
    // Here we test a com port that definitely does not exist!
    HANDLE handleSerialPort = Kernel32.INSTANCE.CreateFile("\\\\.\\comDummy", WinNT.GENERIC_READ | WinNT.GENERIC_WRITE, 0, null, WinNT.OPEN_EXISTING, WinNT.FILE_ATTRIBUTE_NORMAL, null);
    int lastError = Kernel32.INSTANCE.GetLastError();
    assertEquals(lastError, WinNT.ERROR_FILE_NOT_FOUND);
    // try to read the com port timeouts using the invalid handle
    assertFalse(Kernel32.INSTANCE.GetCommTimeouts(handleSerialPort, lpCommTimeouts));
    // Check if we can open a connection to com port1
    // If yes, we try to read the com state
    // If no com port exists we have to skip this test
    handleSerialPort = Kernel32.INSTANCE.CreateFile("\\\\.\\com1", WinNT.GENERIC_READ | WinNT.GENERIC_WRITE, 0, null, WinNT.OPEN_EXISTING, WinNT.FILE_ATTRIBUTE_NORMAL, null);
    lastError = Kernel32.INSTANCE.GetLastError();
    if (WinNT.NO_ERROR == lastError) {
        assertFalse(WinNT.INVALID_HANDLE_VALUE.equals(handleSerialPort));
        try {
            lpCommTimeouts = new WinBase.COMMTIMEOUTS();
            assertTrue(Kernel32.INSTANCE.GetCommTimeouts(handleSerialPort, lpCommTimeouts));
        } finally {
            Kernel32Util.closeHandle(handleSerialPort);
        }
    }
}
Also used : HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE)

Example 89 with Kernel32

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

the class Kernel32Test method testSetHandleInformation.

public void testSetHandleInformation() throws IOException {
    File tmp = File.createTempFile("testSetHandleInformation", "jna");
    tmp.deleteOnExit();
    HANDLE hFile = Kernel32.INSTANCE.CreateFile(tmp.getAbsolutePath(), WinNT.GENERIC_READ, WinNT.FILE_SHARE_READ, new WinBase.SECURITY_ATTRIBUTES(), WinNT.OPEN_EXISTING, WinNT.FILE_ATTRIBUTE_NORMAL, null);
    assertFalse(WinBase.INVALID_HANDLE_VALUE.equals(hFile));
    try {
        assertTrue(Kernel32.INSTANCE.SetHandleInformation(hFile, WinBase.HANDLE_FLAG_PROTECT_FROM_CLOSE, 0));
    } finally {
        Kernel32Util.closeHandle(hFile);
    }
}
Also used : File(java.io.File) HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE)

Example 90 with Kernel32

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

the class Kernel32Test method testGetCurrentThread.

public void testGetCurrentThread() {
    HANDLE h = Kernel32.INSTANCE.GetCurrentThread();
    assertNotNull("No current thread handle", h);
    assertFalse("Null current thread handle", h.equals(0));
    // Calling the CloseHandle function with this handle has no effect
    Kernel32Util.closeHandle(h);
}
Also used : 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