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