Search in sources :

Example 41 with Kernel32

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

the class Kernel32Test method testGetSystemInfo.

public void testGetSystemInfo() {
    SYSTEM_INFO lpSystemInfo = new SYSTEM_INFO();
    Kernel32.INSTANCE.GetSystemInfo(lpSystemInfo);
    assertTrue(lpSystemInfo.dwNumberOfProcessors.intValue() > 0);
}
Also used : SYSTEM_INFO(com.sun.jna.platform.win32.WinBase.SYSTEM_INFO)

Example 42 with Kernel32

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

the class Kernel32Test method testWaitForSingleObject.

public void testWaitForSingleObject() {
    HANDLE handle = Kernel32.INSTANCE.CreateEvent(null, false, false, null);
    assertNotNull("Failed to create event: " + Kernel32.INSTANCE.GetLastError(), handle);
    try {
        // handle runs into timeout since it is not triggered
        // WAIT_TIMEOUT = 0x00000102
        assertEquals(WinError.WAIT_TIMEOUT, Kernel32.INSTANCE.WaitForSingleObject(handle, 1000));
    } finally {
        Kernel32Util.closeHandle(handle);
    }
}
Also used : HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE)

Example 43 with Kernel32

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

the class Kernel32Test method testGetCommState.

public void testGetCommState() {
    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.GetCommState(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));
            switch(lpDCB.BaudRate.intValue()) {
                case WinBase.CBR_110:
                case WinBase.CBR_1200:
                case WinBase.CBR_128000:
                case WinBase.CBR_14400:
                case WinBase.CBR_19200:
                case WinBase.CBR_2400:
                case WinBase.CBR_256000:
                case WinBase.CBR_300:
                case WinBase.CBR_38400:
                case WinBase.CBR_4800:
                case WinBase.CBR_56000:
                case WinBase.CBR_600:
                case WinBase.CBR_9600:
                    break;
                default:
                    fail("Received value of WinBase.DCB.BaudRate is not valid");
            }
        } finally {
            Kernel32Util.closeHandle(handleSerialPort);
        }
    }
}
Also used : HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE)

Example 44 with Kernel32

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

the class Kernel32Test method testDeviceIoControlFsctlReparse.

/**
     * NOTE: Due to process elevation, this test must be run as administrator
     * @throws IOException
     */
public void testDeviceIoControlFsctlReparse() throws IOException {
    Path folder = Files.createTempDirectory("testDeviceIoControlFsctlReparse_FOLDER");
    Path link = Files.createTempDirectory("testDeviceIoControlFsctlReparse_LINK");
    File delFolder = folder.toFile();
    delFolder.deleteOnExit();
    File delLink = link.toFile();
    delLink.deleteOnExit();
    // Required for FSCTL_SET_REPARSE_POINT
    Advapi32Util.Privilege restore = new Advapi32Util.Privilege(WinNT.SE_RESTORE_NAME);
    try {
        restore.enable();
        HANDLE hFile = Kernel32.INSTANCE.CreateFile(link.toAbsolutePath().toString(), WinNT.GENERIC_READ | WinNT.FILE_WRITE_ATTRIBUTES | WinNT.FILE_WRITE_EA, WinNT.FILE_SHARE_READ | WinNT.FILE_SHARE_WRITE | WinNT.FILE_SHARE_DELETE, new WinBase.SECURITY_ATTRIBUTES(), WinNT.OPEN_EXISTING, WinNT.FILE_ATTRIBUTE_DIRECTORY | WinNT.FILE_FLAG_BACKUP_SEMANTICS | WinNT.FILE_FLAG_OPEN_REPARSE_POINT, null);
        if (WinBase.INVALID_HANDLE_VALUE.equals(hFile)) {
            fail("CreateFile failed with " + Kernel32.INSTANCE.GetLastError());
        }
        try {
            SymbolicLinkReparseBuffer symLinkReparseBuffer = new SymbolicLinkReparseBuffer(folder.getFileName().toString(), folder.getFileName().toString(), Ntifs.SYMLINK_FLAG_RELATIVE);
            REPARSE_DATA_BUFFER lpBuffer = new REPARSE_DATA_BUFFER(WinNT.IO_REPARSE_TAG_SYMLINK, (short) 0, symLinkReparseBuffer);
            assertTrue(Kernel32.INSTANCE.DeviceIoControl(hFile, FSCTL_SET_REPARSE_POINT, lpBuffer.getPointer(), lpBuffer.getSize(), null, 0, null, null));
            Memory p = new Memory(REPARSE_DATA_BUFFER.sizeOf());
            IntByReference lpBytes = new IntByReference();
            assertTrue(Kernel32.INSTANCE.DeviceIoControl(hFile, FSCTL_GET_REPARSE_POINT, null, 0, p, (int) p.size(), lpBytes, null));
            // Is a reparse point
            lpBuffer = new REPARSE_DATA_BUFFER(p);
            assertTrue(lpBytes.getValue() > 0);
            assertTrue(lpBuffer.ReparseTag == WinNT.IO_REPARSE_TAG_SYMLINK);
            assertEquals(folder.getFileName().toString(), lpBuffer.u.symLinkReparseBuffer.getPrintName());
            assertEquals(folder.getFileName().toString(), lpBuffer.u.symLinkReparseBuffer.getSubstituteName());
        } finally {
            Kernel32Util.closeHandle(hFile);
        }
    } finally {
        restore.close();
    }
}
Also used : Path(java.nio.file.Path) REPARSE_DATA_BUFFER(com.sun.jna.platform.win32.Ntifs.REPARSE_DATA_BUFFER) IntByReference(com.sun.jna.ptr.IntByReference) Memory(com.sun.jna.Memory) SymbolicLinkReparseBuffer(com.sun.jna.platform.win32.Ntifs.SymbolicLinkReparseBuffer) File(java.io.File) HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE)

Example 45 with Kernel32

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

the class Kernel32Test method testResetEvent.

public void testResetEvent() {
    HANDLE handle = Kernel32.INSTANCE.CreateEvent(null, true, false, null);
    assertNotNull("Failed to create event: " + Kernel32.INSTANCE.GetLastError(), handle);
    try {
        // set the event to the signaled state
        Kernel32.INSTANCE.SetEvent(handle);
        // This should return successfully
        assertEquals(WinBase.WAIT_OBJECT_0, Kernel32.INSTANCE.WaitForSingleObject(handle, 1000));
        // now reset it to not signaled
        Kernel32.INSTANCE.ResetEvent(handle);
        // handle runs into timeout since it is not triggered
        // WAIT_TIMEOUT = 0x00000102
        assertEquals(WinError.WAIT_TIMEOUT, Kernel32.INSTANCE.WaitForSingleObject(handle, 1000));
    } finally {
        Kernel32Util.closeHandle(handle);
    }
}
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