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));
}
}
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());
}
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);
}
}
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());
}
}
}
}
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);
}
Aggregations