use of com.sun.jna.platform.win32.Kernel32 in project jna by java-native-access.
the class Kernel32Test method testGetSystemInfo.
public void testGetSystemInfo() {
SYSTEM_INFO lpSystemInfo = new SYSTEM_INFO();
Kernel32.INSTANCE.GetSystemInfo(lpSystemInfo);
assertTrue(lpSystemInfo.dwNumberOfProcessors.intValue() > 0);
}
use of com.sun.jna.platform.win32.Kernel32 in project jna by java-native-access.
the class Kernel32Test method testWaitForSingleObject.
public void testWaitForSingleObject() {
HANDLE handle = Kernel32.INSTANCE.CreateEvent(null, false, false, null);
assertNotNull("Failed to create event: " + Kernel32.INSTANCE.GetLastError(), handle);
try {
// handle runs into timeout since it is not triggered
// WAIT_TIMEOUT = 0x00000102
assertEquals(WinError.WAIT_TIMEOUT, Kernel32.INSTANCE.WaitForSingleObject(handle, 1000));
} finally {
Kernel32Util.closeHandle(handle);
}
}
use of com.sun.jna.platform.win32.Kernel32 in project jna by java-native-access.
the class Kernel32Test method testGetCommState.
public void testGetCommState() {
WinBase.DCB lpDCB = new WinBase.DCB();
// 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 state using the invalid handle
assertFalse(Kernel32.INSTANCE.GetCommState(handleSerialPort, lpDCB));
// 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 {
lpDCB = new WinBase.DCB();
assertTrue(Kernel32.INSTANCE.GetCommState(handleSerialPort, lpDCB));
switch(lpDCB.BaudRate.intValue()) {
case WinBase.CBR_110:
case WinBase.CBR_1200:
case WinBase.CBR_128000:
case WinBase.CBR_14400:
case WinBase.CBR_19200:
case WinBase.CBR_2400:
case WinBase.CBR_256000:
case WinBase.CBR_300:
case WinBase.CBR_38400:
case WinBase.CBR_4800:
case WinBase.CBR_56000:
case WinBase.CBR_600:
case WinBase.CBR_9600:
break;
default:
fail("Received value of WinBase.DCB.BaudRate is not valid");
}
} finally {
Kernel32Util.closeHandle(handleSerialPort);
}
}
}
use of com.sun.jna.platform.win32.Kernel32 in project jna by java-native-access.
the class Kernel32Test method testDeviceIoControlFsctlReparse.
/**
* NOTE: Due to process elevation, this test must be run as administrator
* @throws IOException
*/
public void testDeviceIoControlFsctlReparse() throws IOException {
Path folder = Files.createTempDirectory("testDeviceIoControlFsctlReparse_FOLDER");
Path link = Files.createTempDirectory("testDeviceIoControlFsctlReparse_LINK");
File delFolder = folder.toFile();
delFolder.deleteOnExit();
File delLink = link.toFile();
delLink.deleteOnExit();
// Required for FSCTL_SET_REPARSE_POINT
Advapi32Util.Privilege restore = new Advapi32Util.Privilege(WinNT.SE_RESTORE_NAME);
try {
restore.enable();
HANDLE hFile = Kernel32.INSTANCE.CreateFile(link.toAbsolutePath().toString(), WinNT.GENERIC_READ | WinNT.FILE_WRITE_ATTRIBUTES | WinNT.FILE_WRITE_EA, WinNT.FILE_SHARE_READ | WinNT.FILE_SHARE_WRITE | WinNT.FILE_SHARE_DELETE, new WinBase.SECURITY_ATTRIBUTES(), WinNT.OPEN_EXISTING, WinNT.FILE_ATTRIBUTE_DIRECTORY | WinNT.FILE_FLAG_BACKUP_SEMANTICS | WinNT.FILE_FLAG_OPEN_REPARSE_POINT, null);
if (WinBase.INVALID_HANDLE_VALUE.equals(hFile)) {
fail("CreateFile failed with " + Kernel32.INSTANCE.GetLastError());
}
try {
SymbolicLinkReparseBuffer symLinkReparseBuffer = new SymbolicLinkReparseBuffer(folder.getFileName().toString(), folder.getFileName().toString(), Ntifs.SYMLINK_FLAG_RELATIVE);
REPARSE_DATA_BUFFER lpBuffer = new REPARSE_DATA_BUFFER(WinNT.IO_REPARSE_TAG_SYMLINK, (short) 0, symLinkReparseBuffer);
assertTrue(Kernel32.INSTANCE.DeviceIoControl(hFile, FSCTL_SET_REPARSE_POINT, lpBuffer.getPointer(), lpBuffer.getSize(), null, 0, null, null));
Memory p = new Memory(REPARSE_DATA_BUFFER.sizeOf());
IntByReference lpBytes = new IntByReference();
assertTrue(Kernel32.INSTANCE.DeviceIoControl(hFile, FSCTL_GET_REPARSE_POINT, null, 0, p, (int) p.size(), lpBytes, null));
// Is a reparse point
lpBuffer = new REPARSE_DATA_BUFFER(p);
assertTrue(lpBytes.getValue() > 0);
assertTrue(lpBuffer.ReparseTag == WinNT.IO_REPARSE_TAG_SYMLINK);
assertEquals(folder.getFileName().toString(), lpBuffer.u.symLinkReparseBuffer.getPrintName());
assertEquals(folder.getFileName().toString(), lpBuffer.u.symLinkReparseBuffer.getSubstituteName());
} finally {
Kernel32Util.closeHandle(hFile);
}
} finally {
restore.close();
}
}
use of com.sun.jna.platform.win32.Kernel32 in project jna by java-native-access.
the class Kernel32Test method testResetEvent.
public void testResetEvent() {
HANDLE handle = Kernel32.INSTANCE.CreateEvent(null, true, false, null);
assertNotNull("Failed to create event: " + Kernel32.INSTANCE.GetLastError(), handle);
try {
// set the event to the signaled state
Kernel32.INSTANCE.SetEvent(handle);
// This should return successfully
assertEquals(WinBase.WAIT_OBJECT_0, Kernel32.INSTANCE.WaitForSingleObject(handle, 1000));
// now reset it to not signaled
Kernel32.INSTANCE.ResetEvent(handle);
// handle runs into timeout since it is not triggered
// WAIT_TIMEOUT = 0x00000102
assertEquals(WinError.WAIT_TIMEOUT, Kernel32.INSTANCE.WaitForSingleObject(handle, 1000));
} finally {
Kernel32Util.closeHandle(handle);
}
}
Aggregations