use of com.sun.jna.platform.win32.Kernel32 in project jna by java-native-access.
the class Kernel32Test method testFindFirstFile.
public void testFindFirstFile() throws IOException {
Path tmpDir = Files.createTempDirectory("testFindFirstFile");
File tmpFile = new File(Files.createTempFile(tmpDir, "testFindFirstFile", ".jna").toString());
Memory p = new Memory(WIN32_FIND_DATA.sizeOf());
HANDLE hFile = Kernel32.INSTANCE.FindFirstFile(tmpDir.toAbsolutePath().toString() + "\\*", p);
assertFalse(WinBase.INVALID_HANDLE_VALUE.equals(hFile));
try {
// Get data and confirm the 1st name is . for the directory itself.
WIN32_FIND_DATA fd = new WIN32_FIND_DATA(p);
String actualFileName = new String(fd.getFileName());
assertTrue(actualFileName.contentEquals("."));
// Get data and confirm the 2nd name is .. for the directory's parent
assertTrue(Kernel32.INSTANCE.FindNextFile(hFile, p));
fd = new WIN32_FIND_DATA(p);
actualFileName = new String(fd.getFileName());
assertTrue(actualFileName.contentEquals(".."));
// Get data and confirm the 3rd name is the tmp file name
assertTrue(Kernel32.INSTANCE.FindNextFile(hFile, p));
fd = new WIN32_FIND_DATA(p);
actualFileName = new String(fd.getFileName());
assertTrue(actualFileName.contentEquals(tmpFile.getName()));
// No more files in directory
assertFalse(Kernel32.INSTANCE.FindNextFile(hFile, p));
assertEquals(WinNT.ERROR_NO_MORE_FILES, Kernel32.INSTANCE.GetLastError());
} finally {
Kernel32.INSTANCE.FindClose(hFile);
tmpFile.delete();
Files.delete(tmpDir);
}
}
use of com.sun.jna.platform.win32.Kernel32 in project jna by java-native-access.
the class Kernel32Test method testSystemTimeToFileTimeAndFileTimeToSystemTime.
/**
* Test both SystemTimeToFileTime and FileTimeToSystemTime
* @throws IOException
*/
public final void testSystemTimeToFileTimeAndFileTimeToSystemTime() throws IOException {
WinBase.SYSTEMTIME systemTime = new WinBase.SYSTEMTIME();
Kernel32.INSTANCE.GetSystemTime(systemTime);
WinBase.FILETIME fileTime = new WinBase.FILETIME();
if (false == Kernel32.INSTANCE.SystemTimeToFileTime(systemTime, fileTime)) {
fail("SystemTimeToFileTime failed with " + Kernel32.INSTANCE.GetLastError());
}
WinBase.SYSTEMTIME newSystemTime = new WinBase.SYSTEMTIME();
if (false == Kernel32.INSTANCE.FileTimeToSystemTime(fileTime, newSystemTime)) {
fail("FileTimeToSystemTime failed with " + Kernel32.INSTANCE.GetLastError());
}
assertEquals(systemTime.wYear, newSystemTime.wYear);
assertEquals(systemTime.wDay, newSystemTime.wDay);
assertEquals(systemTime.wMonth, newSystemTime.wMonth);
assertEquals(systemTime.wHour, newSystemTime.wHour);
assertEquals(systemTime.wMinute, newSystemTime.wMinute);
assertEquals(systemTime.wSecond, newSystemTime.wSecond);
assertEquals(systemTime.wMilliseconds, newSystemTime.wMilliseconds);
}
use of com.sun.jna.platform.win32.Kernel32 in project jna by java-native-access.
the class Kernel32Test method testGetSystemTimes.
public void testGetSystemTimes() {
Kernel32 kernel = Kernel32.INSTANCE;
FILETIME lpIdleTime = new FILETIME();
FILETIME lpKernelTime = new FILETIME();
FILETIME lpUserTime = new FILETIME();
boolean succ = kernel.GetSystemTimes(lpIdleTime, lpKernelTime, lpUserTime);
assertTrue(succ);
long idleTime = lpIdleTime.toDWordLong().longValue();
long kernelTime = lpKernelTime.toDWordLong().longValue();
long userTime = lpUserTime.toDWordLong().longValue();
// All should be >= 0. kernel includes idle.
assertTrue(idleTime >= 0);
assertTrue(kernelTime >= idleTime);
assertTrue(userTime >= 0);
}
use of com.sun.jna.platform.win32.Kernel32 in project jna by java-native-access.
the class Kernel32Test method testFindFirstFileExFindExInfoStandard.
public void testFindFirstFileExFindExInfoStandard() throws IOException {
Path tmpDir = Files.createTempDirectory("testFindFirstFileExFindExInfoStandard");
File tmpFile = new File(Files.createTempFile(tmpDir, "testFindFirstFileExFindExInfoStandard", ".jna").toString());
Memory p = new Memory(WIN32_FIND_DATA.sizeOf());
HANDLE hFile = Kernel32.INSTANCE.FindFirstFileEx(tmpDir.toAbsolutePath().toString() + "\\*", WinBase.FindExInfoStandard, p, WinBase.FindExSearchNameMatch, null, new DWORD(0));
assertFalse(WinBase.INVALID_HANDLE_VALUE.equals(hFile));
try {
// Get data and confirm the 1st name is . for the directory itself.
WIN32_FIND_DATA fd = new WIN32_FIND_DATA(p);
String actualFileName = new String(fd.getFileName());
assertTrue(actualFileName.contentEquals("."));
// Get data and confirm the 2nd name is .. for the directory's parent
assertTrue(Kernel32.INSTANCE.FindNextFile(hFile, p));
fd = new WIN32_FIND_DATA(p);
actualFileName = new String(fd.getFileName());
assertTrue(actualFileName.contentEquals(".."));
// Get data and confirm the 3rd name is the tmp file name
assertTrue(Kernel32.INSTANCE.FindNextFile(hFile, p));
fd = new WIN32_FIND_DATA(p);
actualFileName = new String(fd.getFileName());
assertTrue(actualFileName.contentEquals(tmpFile.getName()));
// No more files in directory
assertFalse(Kernel32.INSTANCE.FindNextFile(hFile, p));
assertEquals(WinNT.ERROR_NO_MORE_FILES, Kernel32.INSTANCE.GetLastError());
} finally {
Kernel32.INSTANCE.FindClose(hFile);
tmpFile.delete();
Files.delete(tmpDir);
}
}
use of com.sun.jna.platform.win32.Kernel32 in project jna by java-native-access.
the class Kernel32Test method testEnumResourceNames.
public void testEnumResourceNames() {
// "14" is the type name of the My Computer icon in explorer.exe
Pointer pointer = new Memory(Native.WCHAR_SIZE * 3);
pointer.setWideString(0, "14");
WinBase.EnumResNameProc ernp = new WinBase.EnumResNameProc() {
@Override
public boolean invoke(HMODULE module, Pointer type, Pointer name, Pointer lParam) {
return true;
}
};
// null HMODULE means use this process / its EXE
// there are no type "14" resources in it.
boolean result = Kernel32.INSTANCE.EnumResourceNames(null, pointer, ernp, null);
assertFalse("EnumResourceNames should have failed.", result);
assertEquals("GetLastError should be set to 1813", WinError.ERROR_RESOURCE_TYPE_NOT_FOUND, Kernel32.INSTANCE.GetLastError());
}
Aggregations