Search in sources :

Example 96 with Kernel32

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

the class Kernel32Test method testSetSystemTime.

public void testSetSystemTime() {
    Kernel32 kernel = Kernel32.INSTANCE;
    WinBase.SYSTEMTIME time = new WinBase.SYSTEMTIME();
    kernel.GetSystemTime(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("SetSystemTime", kernel.SetSystemTime(expected))) {
            WinBase.SYSTEMTIME actual = new WinBase.SYSTEMTIME();
            kernel.GetSystemTime(actual);
            assertEquals("Mismatched hour value", expected.wHour, actual.wHour);
        }
    } finally {
        assertTimeSettingOperationSucceeded("Restore original system time", kernel.SetSystemTime(time));
    }
}
Also used : SYSTEMTIME(com.sun.jna.platform.win32.WinBase.SYSTEMTIME) SYSTEMTIME(com.sun.jna.platform.win32.WinBase.SYSTEMTIME)

Example 97 with Kernel32

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

the class Kernel32Test method testMoveFileEx.

public void testMoveFileEx() throws IOException {
    File source = File.createTempFile("testMoveFileEx", "jna");
    source.deleteOnExit();
    File destination = File.createTempFile("testCopyFile", "jna");
    destination.deleteOnExit();
    Kernel32.INSTANCE.MoveFileEx(source.getCanonicalPath(), destination.getCanonicalPath(), new DWORD(WinBase.MOVEFILE_REPLACE_EXISTING));
    assertTrue(destination.exists());
    assertFalse(source.exists());
}
Also used : DWORD(com.sun.jna.platform.win32.WinDef.DWORD) File(java.io.File)

Example 98 with Kernel32

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

the class Kernel32Test method testGetProcessList.

public void testGetProcessList() throws IOException {
    HANDLE processEnumHandle = Kernel32.INSTANCE.CreateToolhelp32Snapshot(Tlhelp32.TH32CS_SNAPALL, new WinDef.DWORD(0));
    assertFalse(WinBase.INVALID_HANDLE_VALUE.equals(processEnumHandle));
    try {
        Tlhelp32.PROCESSENTRY32.ByReference processEntry = new Tlhelp32.PROCESSENTRY32.ByReference();
        assertTrue(Kernel32.INSTANCE.Process32First(processEnumHandle, processEntry));
        List<Long> processIdList = new ArrayList<Long>();
        processIdList.add(processEntry.th32ProcessID.longValue());
        while (Kernel32.INSTANCE.Process32Next(processEnumHandle, processEntry)) {
            processIdList.add(processEntry.th32ProcessID.longValue());
        }
        assertTrue(processIdList.size() > 4);
    } finally {
        Kernel32Util.closeHandle(processEnumHandle);
    }
}
Also used : ArrayList(java.util.ArrayList) DWORD(com.sun.jna.platform.win32.WinDef.DWORD) 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)

Example 99 with Kernel32

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

the class Kernel32Test method testLoadLibraryEx.

public void testLoadLibraryEx() {
    String winDir = Kernel32Util.getEnvironmentVariable("WINDIR");
    assertNotNull("No WINDIR value returned", winDir);
    assertTrue("Specified WINDIR does not exist: " + winDir, new File(winDir).exists());
    HMODULE hModule = null;
    try {
        hModule = Kernel32.INSTANCE.LoadLibraryEx(new File(winDir, "explorer.exe").getAbsolutePath(), null, Kernel32.LOAD_LIBRARY_AS_DATAFILE);
        if (hModule == null) {
            throw new Win32Exception(Native.getLastError());
        }
        assertNotNull("hModule should not be null.", hModule);
    } finally {
        if (hModule != null) {
            if (!Kernel32.INSTANCE.FreeLibrary(hModule)) {
                throw new Win32Exception(Native.getLastError());
            }
        }
    }
}
Also used : HMODULE(com.sun.jna.platform.win32.WinDef.HMODULE) File(java.io.File)

Example 100 with Kernel32

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

the class Kernel32Test method main.

public static void main(String[] args) {
    OSVERSIONINFO lpVersionInfo = new OSVERSIONINFO();
    assertTrue(Kernel32.INSTANCE.GetVersionEx(lpVersionInfo));
    System.out.println("Operating system: " + lpVersionInfo.dwMajorVersion.longValue() + "." + lpVersionInfo.dwMinorVersion.longValue() + " (" + lpVersionInfo.dwBuildNumber + ")" + " [" + Native.toString(lpVersionInfo.szCSDVersion) + "]");
    junit.textui.TestRunner.run(Kernel32Test.class);
}
Also used : OSVERSIONINFO(com.sun.jna.platform.win32.WinNT.OSVERSIONINFO)

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