Search in sources :

Example 21 with DWORD

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

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

Example 23 with DWORD

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

the class Kernel32Test method testModule32FirstW.

public void testModule32FirstW() {
    HANDLE snapshot = Kernel32.INSTANCE.CreateToolhelp32Snapshot(Tlhelp32.TH32CS_SNAPMODULE, new DWORD(Kernel32.INSTANCE.GetCurrentProcessId()));
    if (snapshot == null) {
        throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
    }
    Win32Exception we = null;
    Tlhelp32.MODULEENTRY32W first = new Tlhelp32.MODULEENTRY32W();
    try {
        if (!Kernel32.INSTANCE.Module32FirstW(snapshot, first)) {
            throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
        }
        // not sure if this will be run against java.exe or javaw.exe but this
        // check tests both
        assertTrue("The first module in the current process should be java.exe or javaw.exe", first.szModule().startsWith("java"));
        assertEquals("The process ID of the module ID should be our process ID", Kernel32.INSTANCE.GetCurrentProcessId(), first.th32ProcessID.intValue());
    } catch (Win32Exception e) {
        we = e;
        // re-throw so finally block is executed
        throw we;
    } finally {
        try {
            Kernel32Util.closeHandle(snapshot);
        } catch (Win32Exception e) {
            if (we == null) {
                we = e;
            } else {
                we.addSuppressed(e);
            }
        }
        if (we != null) {
            throw we;
        }
    }
}
Also used : DWORD(com.sun.jna.platform.win32.WinDef.DWORD) HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE)

Example 24 with DWORD

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

the class Kernel32Test method testGetPrivateProfileSectionNames.

public final void testGetPrivateProfileSectionNames() throws IOException {
    final File tmp = File.createTempFile("testGetPrivateProfileSectionNames", ".ini");
    tmp.deleteOnExit();
    final PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(tmp)));
    try {
        writer.println("[S1]");
        writer.println("[S2]");
    } finally {
        writer.close();
    }
    final char[] buffer = new char[7];
    final DWORD len = Kernel32.INSTANCE.GetPrivateProfileSectionNames(buffer, new DWORD(buffer.length), tmp.getCanonicalPath());
    assertEquals(len.intValue(), 5);
    assertEquals(new String(buffer), "S1\0S2\0\0");
}
Also used : FileWriter(java.io.FileWriter) DWORD(com.sun.jna.platform.win32.WinDef.DWORD) File(java.io.File) PrintWriter(java.io.PrintWriter) BufferedWriter(java.io.BufferedWriter)

Example 25 with DWORD

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

the class Kernel32Test method testGetVersion.

public void testGetVersion() {
    DWORD version = Kernel32.INSTANCE.GetVersion();
    assertTrue("Version high should be non-zero: 0x" + Integer.toHexString(version.getHigh().intValue()), version.getHigh().intValue() != 0);
    assertTrue("Version low should be >= 0: 0x" + Integer.toHexString(version.getLow().intValue()), version.getLow().intValue() >= 0);
}
Also used : DWORD(com.sun.jna.platform.win32.WinDef.DWORD)

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