Skip to content

Commit b40ad6d

Browse files
committed
-convert to FFI
1 parent c16cd72 commit b40ad6d

File tree

1 file changed

+8
-13
lines changed

1 file changed

+8
-13
lines changed

src/main/java/mediathek/windows/WindowsVersionHelper.java

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,9 @@
2121
import java.lang.foreign.*;
2222

2323
public class WindowsVersionHelper {
24-
25-
// Constants for VerSetConditionMask dwTypeBitMask parameter
2624
private static final int VER_MINORVERSION = 0x0000001;
2725
private static final int VER_MAJORVERSION = 0x0000002;
2826
private static final int VER_SERVICEPACKMAJOR = 0x0000020;
29-
30-
// Constants for VerSetConditionMask dwCondition parameter
3127
private static final byte VER_GREATER_EQUAL = 3;
3228

3329
private static final GroupLayout OSVERSIONINFOEXW_LAYOUT = MemoryLayout.structLayout(
@@ -48,30 +44,30 @@ public class WindowsVersionHelper {
4844

4945

5046
public static boolean IsWindows10OrGreater() throws Throwable {
51-
return isWindowsVersionOrGreater(10,0,0);
47+
return isWindowsVersionOrGreater(10, 0, 0);
5248
}
5349

5450
public static boolean isWindowsVersionOrGreater(int major, int minor, int servicePackMajor) throws Throwable {
5551
try (Arena arena = Arena.ofConfined()) {
5652
SymbolLookup kernel32 = SymbolLookup.libraryLookup("kernel32.dll", Arena.global());
5753

58-
var NATIVE_LINKER = Linker.nativeLinker();
59-
var MH_VerSetConditionMask = NATIVE_LINKER.downcallHandle(
54+
var nativeLinker = Linker.nativeLinker();
55+
var MH_VerSetConditionMask = nativeLinker.downcallHandle(
6056
kernel32.find("VerSetConditionMask").orElseThrow(() -> new UnsatisfiedLinkError("VerSetConditionMask not found")),
6157
FunctionDescriptor.of(ValueLayout.JAVA_LONG, ValueLayout.JAVA_LONG, ValueLayout.JAVA_INT, ValueLayout.JAVA_BYTE)
6258
);
6359

64-
var MH_VerifyVersionInfoW = NATIVE_LINKER.downcallHandle(
60+
var MH_VerifyVersionInfoW = nativeLinker.downcallHandle(
6561
kernel32.find("VerifyVersionInfoW").orElseThrow(() -> new UnsatisfiedLinkError("VerifyVersionInfoW not found")),
6662
FunctionDescriptor.of(ValueLayout.JAVA_INT, ValueLayout.ADDRESS, ValueLayout.JAVA_INT, ValueLayout.JAVA_LONG)
6763
);
68-
MemorySegment osvi = arena.allocate(OSVERSIONINFOEXW_LAYOUT);
6964

7065
var VH_dwOSVersionInfoSize = OSVERSIONINFOEXW_LAYOUT.varHandle(MemoryLayout.PathElement.groupElement("dwOSVersionInfoSize"));
7166
var VH_dwMajorVersion = OSVERSIONINFOEXW_LAYOUT.varHandle(MemoryLayout.PathElement.groupElement("dwMajorVersion"));
7267
var VH_dwMinorVersion = OSVERSIONINFOEXW_LAYOUT.varHandle(MemoryLayout.PathElement.groupElement("dwMinorVersion"));
7368
var VH_wServicePackMajor = OSVERSIONINFOEXW_LAYOUT.varHandle(MemoryLayout.PathElement.groupElement("wServicePackMajor"));
7469

70+
var osvi = arena.allocate(OSVERSIONINFOEXW_LAYOUT);
7571
VH_dwOSVersionInfoSize.set(osvi, 0L, (int) OSVERSIONINFOEXW_STRUCT_SIZE);
7672
VH_dwMajorVersion.set(osvi, 0L, major);
7773
VH_dwMinorVersion.set(osvi, 0L, minor);
@@ -82,9 +78,7 @@ public static boolean isWindowsVersionOrGreater(int major, int minor, int servic
8278
conditionMask = (long) MH_VerSetConditionMask.invokeExact(conditionMask, VER_MINORVERSION, VER_GREATER_EQUAL);
8379
conditionMask = (long) MH_VerSetConditionMask.invokeExact(conditionMask, VER_SERVICEPACKMAJOR, VER_GREATER_EQUAL);
8480

85-
int typeMask = VER_MAJORVERSION | VER_MINORVERSION | VER_SERVICEPACKMAJOR;
86-
87-
int result = (int) MH_VerifyVersionInfoW.invokeExact(osvi, typeMask, conditionMask);
81+
int result = (int) MH_VerifyVersionInfoW.invokeExact(osvi, VER_MAJORVERSION | VER_MINORVERSION | VER_SERVICEPACKMAJOR, conditionMask);
8882

8983
return result != 0;
9084
}
@@ -107,7 +101,8 @@ public static void main(String[] args) {
107101
boolean isFutureVersion = v.isWindowsVersionOrGreater(99, 0, 0);
108102
System.out.println("Is current OS Windows 99.0 SP0 or greater? " + isFutureVersion);
109103

110-
} catch (Throwable t) {
104+
}
105+
catch (Throwable t) {
111106
System.err.println("An error occurred during the Windows version check:");
112107
t.printStackTrace();
113108
}

0 commit comments

Comments
 (0)