use of com.sun.jna.platform.win32.Kernel32 in project jna by java-native-access.
the class Kernel32Test method testGetSetFileTime.
public void testGetSetFileTime() throws IOException {
File tmp = File.createTempFile("testGetSetFileTime", "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 {
WinBase.FILETIME.ByReference creationTime = new WinBase.FILETIME.ByReference();
WinBase.FILETIME.ByReference accessTime = new WinBase.FILETIME.ByReference();
WinBase.FILETIME.ByReference modifiedTime = new WinBase.FILETIME.ByReference();
Kernel32.INSTANCE.GetFileTime(hFile, creationTime, accessTime, modifiedTime);
assertEquals(creationTime.toDate().getYear(), new Date().getYear());
assertEquals(accessTime.toDate().getYear(), new Date().getYear());
assertEquals(modifiedTime.toDate().getYear(), new Date().getYear());
Kernel32.INSTANCE.SetFileTime(hFile, null, null, new WinBase.FILETIME(new Date(2010, 1, 1)));
assertEquals(2010, new Date(tmp.lastModified()).getYear());
} finally {
Kernel32Util.closeHandle(hFile);
}
}
use of com.sun.jna.platform.win32.Kernel32 in project jna by java-native-access.
the class Kernel32Test method testWaitForMultipleObjects.
public void testWaitForMultipleObjects() {
HANDLE[] handles = new HANDLE[2];
try {
for (int index = 0; index < handles.length; index++) {
HANDLE h = Kernel32.INSTANCE.CreateEvent(null, false, false, null);
assertNotNull("Failed to create event #" + index + ": " + Kernel32.INSTANCE.GetLastError(), h);
handles[index] = h;
}
// handle runs into timeout since it is not triggered
// WAIT_TIMEOUT = 0x00000102
assertEquals(WinError.WAIT_TIMEOUT, Kernel32.INSTANCE.WaitForMultipleObjects(handles.length, handles, false, 1000));
} finally {
Kernel32Util.closeHandles(handles);
}
// invalid Handle
handles[0] = WinBase.INVALID_HANDLE_VALUE;
handles[1] = Kernel32.INSTANCE.CreateEvent(null, false, false, null);
assertNotNull("Failed to create valid event: " + Kernel32.INSTANCE.GetLastError(), handles[1]);
try {
// returns WAIT_FAILED since handle is invalid
assertEquals(WinBase.WAIT_FAILED, Kernel32.INSTANCE.WaitForMultipleObjects(handles.length, handles, false, 5000));
} finally {
Kernel32Util.closeHandle(handles[1]);
}
}
use of com.sun.jna.platform.win32.Kernel32 in project jna by java-native-access.
the class Kernel32Test method testGetCommTimeouts.
public void testGetCommTimeouts() {
WinBase.COMMTIMEOUTS lpCommTimeouts = new WinBase.COMMTIMEOUTS();
// 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 timeouts using the invalid handle
assertFalse(Kernel32.INSTANCE.GetCommTimeouts(handleSerialPort, lpCommTimeouts));
// 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 {
lpCommTimeouts = new WinBase.COMMTIMEOUTS();
assertTrue(Kernel32.INSTANCE.GetCommTimeouts(handleSerialPort, lpCommTimeouts));
} finally {
Kernel32Util.closeHandle(handleSerialPort);
}
}
}
use of com.sun.jna.platform.win32.Kernel32 in project jna by java-native-access.
the class Kernel32Test method testSetHandleInformation.
public void testSetHandleInformation() throws IOException {
File tmp = File.createTempFile("testSetHandleInformation", "jna");
tmp.deleteOnExit();
HANDLE hFile = Kernel32.INSTANCE.CreateFile(tmp.getAbsolutePath(), WinNT.GENERIC_READ, WinNT.FILE_SHARE_READ, new WinBase.SECURITY_ATTRIBUTES(), WinNT.OPEN_EXISTING, WinNT.FILE_ATTRIBUTE_NORMAL, null);
assertFalse(WinBase.INVALID_HANDLE_VALUE.equals(hFile));
try {
assertTrue(Kernel32.INSTANCE.SetHandleInformation(hFile, WinBase.HANDLE_FLAG_PROTECT_FROM_CLOSE, 0));
} finally {
Kernel32Util.closeHandle(hFile);
}
}
use of com.sun.jna.platform.win32.Kernel32 in project jna by java-native-access.
the class Kernel32Test method testGetCurrentThread.
public void testGetCurrentThread() {
HANDLE h = Kernel32.INSTANCE.GetCurrentThread();
assertNotNull("No current thread handle", h);
assertFalse("Null current thread handle", h.equals(0));
// Calling the CloseHandle function with this handle has no effect
Kernel32Util.closeHandle(h);
}
Aggregations