Search in sources :

Example 51 with Kernel32

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

the class Kernel32Test method testStructureOutArgument.

public void testStructureOutArgument() {
    Kernel32 kernel = Kernel32.INSTANCE;
    WinBase.SYSTEMTIME time = new WinBase.SYSTEMTIME();
    kernel.GetSystemTime(time);
    Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
    assertEquals("Hour not properly set", cal.get(Calendar.HOUR_OF_DAY), time.wHour);
    assertEquals("Day not properly set", cal.get(Calendar.DAY_OF_WEEK) - 1, time.wDayOfWeek);
    assertEquals("Year not properly set", cal.get(Calendar.YEAR), time.wYear);
}
Also used : SYSTEMTIME(com.sun.jna.platform.win32.WinBase.SYSTEMTIME) SYSTEMTIME(com.sun.jna.platform.win32.WinBase.SYSTEMTIME) Calendar(java.util.Calendar)

Example 52 with Kernel32

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

the class Kernel32Test method testSetFileAttributes.

public void testSetFileAttributes() throws IOException {
    File tmp = File.createTempFile("testSetFileAttributes", "jna");
    tmp.deleteOnExit();
    Kernel32.INSTANCE.SetFileAttributes(tmp.getCanonicalPath(), new DWORD(WinNT.FILE_ATTRIBUTE_HIDDEN));
    int attributes = Kernel32.INSTANCE.GetFileAttributes(tmp.getCanonicalPath());
    assertTrue((attributes & WinNT.FILE_ATTRIBUTE_HIDDEN) != 0);
}
Also used : DWORD(com.sun.jna.platform.win32.WinDef.DWORD) File(java.io.File)

Example 53 with Kernel32

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

the class Kernel32Test method testIsWow64Process.

public void testIsWow64Process() {
    try {
        IntByReference isWow64 = new IntByReference(42);
        HANDLE hProcess = Kernel32.INSTANCE.GetCurrentProcess();
        assertTrue(Kernel32.INSTANCE.IsWow64Process(hProcess, isWow64));
        assertTrue(0 == isWow64.getValue() || 1 == isWow64.getValue());
    } catch (UnsatisfiedLinkError e) {
    // IsWow64Process is not available on this OS
    }
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE)

Example 54 with Kernel32

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

the class Kernel32Test method testSetCommState.

public void testSetCommState() {
    WinBase.DCB lpDCB = new WinBase.DCB();
    // 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 state using the invalid handle
    assertFalse(Kernel32.INSTANCE.SetCommState(handleSerialPort, lpDCB));
    // 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 {
            lpDCB = new WinBase.DCB();
            assertTrue(Kernel32.INSTANCE.GetCommState(handleSerialPort, lpDCB));
            DWORD oldBaudRate = new DWORD(lpDCB.BaudRate.longValue());
            lpDCB.BaudRate = new DWORD(WinBase.CBR_110);
            assertTrue(Kernel32.INSTANCE.SetCommState(handleSerialPort, lpDCB));
            WinBase.DCB lpNewDCB = new WinBase.DCB();
            assertTrue(Kernel32.INSTANCE.GetCommState(handleSerialPort, lpNewDCB));
            assertEquals(WinBase.CBR_110, lpNewDCB.BaudRate.intValue());
            lpDCB.BaudRate = oldBaudRate;
            assertTrue(Kernel32.INSTANCE.SetCommState(handleSerialPort, lpDCB));
        } finally {
            Kernel32Util.closeHandle(handleSerialPort);
        }
    }
}
Also used : DWORD(com.sun.jna.platform.win32.WinDef.DWORD) HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE)

Example 55 with Kernel32

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

the class Kernel32Test method testGetFileInformationByHandleEx.

public void testGetFileInformationByHandleEx() 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);
        // New file has non-zero creation time
        assertTrue(0 != fbi.CreationTime.getValue());
        p = new Memory(FILE_STANDARD_INFO.sizeOf());
        if (false == Kernel32.INSTANCE.GetFileInformationByHandleEx(hFile, WinBase.FileStandardInfo, p, new DWORD(p.size()))) {
            fail("GetFileInformationByHandleEx failed with " + Kernel32.INSTANCE.GetLastError());
        }
        FILE_STANDARD_INFO fsi = new FILE_STANDARD_INFO(p);
        // New file has 1 link
        assertEquals(1, fsi.NumberOfLinks);
        p = new Memory(FILE_COMPRESSION_INFO.sizeOf());
        if (false == Kernel32.INSTANCE.GetFileInformationByHandleEx(hFile, WinBase.FileCompressionInfo, p, new DWORD(p.size()))) {
            fail("GetFileInformationByHandleEx failed with " + Kernel32.INSTANCE.GetLastError());
        }
        FILE_COMPRESSION_INFO fci = new FILE_COMPRESSION_INFO(p);
        // Uncompressed file should be zero
        assertEquals(0, fci.CompressionFormat);
        p = new Memory(FILE_ATTRIBUTE_TAG_INFO.sizeOf());
        if (false == Kernel32.INSTANCE.GetFileInformationByHandleEx(hFile, WinBase.FileAttributeTagInfo, p, new DWORD(p.size()))) {
            fail("GetFileInformationByHandleEx failed with " + Kernel32.INSTANCE.GetLastError());
        }
        FILE_ATTRIBUTE_TAG_INFO fati = new FILE_ATTRIBUTE_TAG_INFO(p);
        // New files have the archive bit
        assertEquals(WinNT.FILE_ATTRIBUTE_ARCHIVE, fati.FileAttributes);
        p = new Memory(FILE_ID_INFO.sizeOf());
        if (false == Kernel32.INSTANCE.GetFileInformationByHandleEx(hFile, WinBase.FileIdInfo, p, new DWORD(p.size()))) {
            fail("GetFileInformationByHandleEx failed with " + Kernel32.INSTANCE.GetLastError());
        }
        FILE_ID_INFO fii = new FILE_ID_INFO(p);
        // Volume serial number should be non-zero
        assertFalse(fii.VolumeSerialNumber == 0);
    } finally {
        Kernel32.INSTANCE.CloseHandle(hFile);
    }
}
Also used : FILE_COMPRESSION_INFO(com.sun.jna.platform.win32.WinBase.FILE_COMPRESSION_INFO) FILE_ATTRIBUTE_TAG_INFO(com.sun.jna.platform.win32.WinBase.FILE_ATTRIBUTE_TAG_INFO) Memory(com.sun.jna.Memory) FILE_STANDARD_INFO(com.sun.jna.platform.win32.WinBase.FILE_STANDARD_INFO) DWORD(com.sun.jna.platform.win32.WinDef.DWORD) FILE_ID_INFO(com.sun.jna.platform.win32.WinBase.FILE_ID_INFO) File(java.io.File) HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE) FILE_BASIC_INFO(com.sun.jna.platform.win32.WinBase.FILE_BASIC_INFO)

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