use of com.sun.jna.platform.win32.Kernel32 in project jna by java-native-access.
the class Kernel32Test method testFileTimeFromLargeInteger.
/**
* Test FILETIME's LARGE_INTEGER constructor
* @throws IOException
*/
public final void testFileTimeFromLargeInteger() throws IOException {
File tmp = File.createTempFile("testGetFileInformationByHandleEx", "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 {
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);
FILETIME ft = new FILETIME(fbi.LastWriteTime);
SYSTEMTIME stUTC = new SYSTEMTIME();
SYSTEMTIME stLocal = new SYSTEMTIME();
Kernel32.INSTANCE.FileTimeToSystemTime(ft, stUTC);
// Covert to local
Kernel32.INSTANCE.SystemTimeToTzSpecificLocalTime(null, stUTC, stLocal);
FileTime calculatedCreateTime = FileTime.fromMillis(stLocal.toCalendar().getTimeInMillis());
// Actual file's createTime
FileTime createTime = Files.getLastModifiedTime(Paths.get(tmp.getAbsolutePath()));
assertEquals(createTime.toMillis(), calculatedCreateTime.toMillis());
} finally {
Kernel32.INSTANCE.CloseHandle(hFile);
}
}
use of com.sun.jna.platform.win32.Kernel32 in project jna by java-native-access.
the class Kernel32Test method testWriteProcessMemory.
public void testWriteProcessMemory() {
Kernel32 kernel = Kernel32.INSTANCE;
boolean successWrite = kernel.WriteProcessMemory(null, Pointer.NULL, Pointer.NULL, 1, null);
assertFalse(successWrite);
assertEquals(kernel.GetLastError(), WinError.ERROR_INVALID_HANDLE);
ByteBuffer bufDest = ByteBuffer.allocateDirect(4);
bufDest.put(new byte[] { 0, 1, 2, 3 });
ByteBuffer bufSrc = ByteBuffer.allocateDirect(4);
bufSrc.put(new byte[] { 5, 10, 15, 20 });
Pointer ptrSrc = Native.getDirectBufferPointer(bufSrc);
Pointer ptrDest = Native.getDirectBufferPointer(bufDest);
HANDLE selfHandle = kernel.GetCurrentProcess();
//Write only the first three
kernel.WriteProcessMemory(selfHandle, ptrDest, ptrSrc, 3, null);
assertEquals(bufDest.get(0), 5);
assertEquals(bufDest.get(1), 10);
assertEquals(bufDest.get(2), 15);
assertEquals(bufDest.get(3), 3);
}
use of com.sun.jna.platform.win32.Kernel32 in project jna by java-native-access.
the class Kernel32Test method testOpenThread.
public void testOpenThread() {
HANDLE h = Kernel32.INSTANCE.OpenThread(WinNT.THREAD_ALL_ACCESS, false, Kernel32.INSTANCE.GetCurrentThreadId());
assertNotNull(h);
assertFalse(h.equals(0));
Kernel32Util.closeHandle(h);
}
use of com.sun.jna.platform.win32.Kernel32 in project jna by java-native-access.
the class Kernel32VolumeManagementFunctionsTest method testEnumVolumes.
@Test
public void testEnumVolumes() {
char[] lpszVolumeName = new char[WinDef.MAX_PATH + 1];
HANDLE hFindVolume = assertValidHandle("FindFirstVolume", Kernel32.INSTANCE.FindFirstVolume(lpszVolumeName, lpszVolumeName.length));
try {
int foundPaths = 0;
do {
String volumeGUID = Native.toString(lpszVolumeName);
testEnumVolumeMountMoints(volumeGUID);
foundPaths += testGetVolumePathNamesForVolumeName(volumeGUID);
} while (Kernel32.INSTANCE.FindNextVolume(hFindVolume, lpszVolumeName, lpszVolumeName.length));
assertTrue("No paths were found", foundPaths > 0);
int hr = Kernel32.INSTANCE.GetLastError();
assertEquals("Bad volumes enum termination reason", WinError.ERROR_NO_MORE_FILES, hr);
} finally {
assertCallSucceeded("FindVolumeClose", Kernel32.INSTANCE.FindVolumeClose(hFindVolume));
}
}
use of com.sun.jna.platform.win32.Kernel32 in project jna by java-native-access.
the class Kernel32Test method testDeviceIoControlFsctlCompression.
public void testDeviceIoControlFsctlCompression() throws IOException {
File tmp = File.createTempFile("testDeviceIoControlFsctlCompression", "jna");
tmp.deleteOnExit();
HANDLE hFile = Kernel32.INSTANCE.CreateFile(tmp.getAbsolutePath(), WinNT.GENERIC_ALL, WinNT.FILE_SHARE_READ, new WinBase.SECURITY_ATTRIBUTES(), WinNT.OPEN_EXISTING, WinNT.FILE_ATTRIBUTE_NORMAL, null);
assertFalse(WinBase.INVALID_HANDLE_VALUE.equals(hFile));
try {
ShortByReference lpBuffer = new ShortByReference();
IntByReference lpBytes = new IntByReference();
if (false == Kernel32.INSTANCE.DeviceIoControl(hFile, FSCTL_GET_COMPRESSION, null, 0, lpBuffer.getPointer(), USHORT.SIZE, lpBytes, null)) {
fail("DeviceIoControl failed with " + Kernel32.INSTANCE.GetLastError());
}
assertEquals(WinNT.COMPRESSION_FORMAT_NONE, lpBuffer.getValue());
assertEquals(USHORT.SIZE, lpBytes.getValue());
lpBuffer = new ShortByReference((short) WinNT.COMPRESSION_FORMAT_LZNT1);
if (false == Kernel32.INSTANCE.DeviceIoControl(hFile, FSCTL_SET_COMPRESSION, lpBuffer.getPointer(), USHORT.SIZE, null, 0, lpBytes, null)) {
fail("DeviceIoControl failed with " + Kernel32.INSTANCE.GetLastError());
}
if (false == Kernel32.INSTANCE.DeviceIoControl(hFile, FSCTL_GET_COMPRESSION, null, 0, lpBuffer.getPointer(), USHORT.SIZE, lpBytes, null)) {
fail("DeviceIoControl failed with " + Kernel32.INSTANCE.GetLastError());
}
assertEquals(WinNT.COMPRESSION_FORMAT_LZNT1, lpBuffer.getValue());
assertEquals(USHORT.SIZE, lpBytes.getValue());
} finally {
Kernel32Util.closeHandle(hFile);
}
}
Aggregations