Search in sources :

Example 91 with Kernel32

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

the class Kernel32Test method testSetLocaltime.

public void testSetLocaltime() {
    Kernel32 kernel = Kernel32.INSTANCE;
    WinBase.SYSTEMTIME time = new WinBase.SYSTEMTIME();
    kernel.GetLocalTime(time);
    try {
        WinBase.SYSTEMTIME expected = new WinBase.SYSTEMTIME();
        expected.wYear = time.wYear;
        expected.wMonth = time.wMonth;
        expected.wDay = time.wDay;
        expected.wHour = time.wHour;
        expected.wMinute = time.wMinute;
        expected.wSecond = time.wSecond;
        expected.wMilliseconds = time.wMilliseconds;
        if (expected.wHour > 0) {
            expected.wHour--;
        } else {
            expected.wHour++;
        }
        if (assertTimeSettingOperationSucceeded("SetLocalTime", kernel.SetLocalTime(expected))) {
            WinBase.SYSTEMTIME actual = new WinBase.SYSTEMTIME();
            kernel.GetLocalTime(actual);
            assertEquals("Mismatched hour value", expected.wHour, actual.wHour);
        }
    } finally {
        assertTimeSettingOperationSucceeded("Restore local time", kernel.SetLocalTime(time));
    }
}
Also used : SYSTEMTIME(com.sun.jna.platform.win32.WinBase.SYSTEMTIME) SYSTEMTIME(com.sun.jna.platform.win32.WinBase.SYSTEMTIME)

Example 92 with Kernel32

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

the class Kernel32Test method testQueryFullProcessImageName.

public void testQueryFullProcessImageName() {
    int pid = Kernel32.INSTANCE.GetCurrentProcessId();
    HANDLE h = Kernel32.INSTANCE.OpenProcess(WinNT.PROCESS_QUERY_INFORMATION, false, pid);
    assertNotNull("Failed (" + Kernel32.INSTANCE.GetLastError() + ") to get process ID=" + pid + " handle", h);
    try {
        char[] path = new char[WinDef.MAX_PATH];
        IntByReference lpdwSize = new IntByReference(path.length);
        boolean b = Kernel32.INSTANCE.QueryFullProcessImageName(h, 0, path, lpdwSize);
        assertTrue("Failed (" + Kernel32.INSTANCE.GetLastError() + ") to query process image name", b);
        assertTrue("Failed to query process image name, empty path returned", lpdwSize.getValue() > 0);
    } finally {
        Kernel32Util.closeHandle(h);
    }
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE)

Example 93 with Kernel32

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

the class Kernel32Test method testSetFileInformationByHandleFileBasicInfo.

public void testSetFileInformationByHandleFileBasicInfo() throws IOException, InterruptedException {
    File tmp = File.createTempFile("testSetFileInformationByHandleFileBasicInfo", "jna");
    tmp.deleteOnExit();
    HANDLE hFile = Kernel32.INSTANCE.CreateFile(tmp.getAbsolutePath(), WinNT.GENERIC_READ | WinNT.GENERIC_WRITE, WinNT.FILE_SHARE_READ | 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);
        // Add TEMP attribute
        fbi.FileAttributes = fbi.FileAttributes | WinNT.FILE_ATTRIBUTE_TEMPORARY;
        fbi.ChangeTime = new WinNT.LARGE_INTEGER(0);
        fbi.CreationTime = new WinNT.LARGE_INTEGER(0);
        fbi.LastAccessTime = new WinNT.LARGE_INTEGER(0);
        fbi.LastWriteTime = new WinNT.LARGE_INTEGER(0);
        fbi.write();
        if (false == Kernel32.INSTANCE.SetFileInformationByHandle(hFile, WinBase.FileBasicInfo, fbi.getPointer(), new DWORD(FILE_BASIC_INFO.sizeOf())))
            fail("GetFileInformationByHandleEx failed with " + Kernel32.INSTANCE.GetLastError());
        if (false == Kernel32.INSTANCE.GetFileInformationByHandleEx(hFile, WinBase.FileBasicInfo, p, new DWORD(p.size())))
            fail("GetFileInformationByHandleEx failed with " + Kernel32.INSTANCE.GetLastError());
        fbi = new FILE_BASIC_INFO(p);
        assertTrue((fbi.FileAttributes & WinNT.FILE_ATTRIBUTE_TEMPORARY) != 0);
    } finally {
        Kernel32.INSTANCE.CloseHandle(hFile);
    }
}
Also used : Memory(com.sun.jna.Memory) DWORD(com.sun.jna.platform.win32.WinDef.DWORD) 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 94 with Kernel32

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

the class Kernel32Test method testSetFileInformationByHandleFileDispositionInfo.

public void testSetFileInformationByHandleFileDispositionInfo() throws IOException, InterruptedException {
    File tmp = File.createTempFile("testSetFileInformationByHandleFileDispositionInfo", "jna");
    HANDLE hFile = Kernel32.INSTANCE.CreateFile(tmp.getAbsolutePath(), WinNT.GENERIC_WRITE | WinNT.DELETE, WinNT.FILE_SHARE_WRITE, new WinBase.SECURITY_ATTRIBUTES(), WinNT.OPEN_EXISTING, WinNT.FILE_ATTRIBUTE_NORMAL, null);
    assertFalse(WinBase.INVALID_HANDLE_VALUE.equals(hFile));
    try {
        FILE_DISPOSITION_INFO fdi = new FILE_DISPOSITION_INFO(true);
        if (false == Kernel32.INSTANCE.SetFileInformationByHandle(hFile, WinBase.FileDispositionInfo, fdi.getPointer(), new DWORD(FILE_DISPOSITION_INFO.sizeOf())))
            fail("SetFileInformationByHandle failed with " + Kernel32.INSTANCE.GetLastError());
    } finally {
        Kernel32.INSTANCE.CloseHandle(hFile);
    }
    assertFalse(Files.exists(Paths.get(tmp.getAbsolutePath())));
}
Also used : FILE_DISPOSITION_INFO(com.sun.jna.platform.win32.WinBase.FILE_DISPOSITION_INFO) DWORD(com.sun.jna.platform.win32.WinDef.DWORD) File(java.io.File) HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE)

Example 95 with Kernel32

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

the class Kernel32Test method testOpenProcess.

public void testOpenProcess() {
    HANDLE h = Kernel32.INSTANCE.OpenProcess(0, false, Kernel32.INSTANCE.GetCurrentProcessId());
    assertNull(h);
    // opening your own process fails with access denied
    assertEquals(WinError.ERROR_ACCESS_DENIED, Kernel32.INSTANCE.GetLastError());
}
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