从注册表中读取 Windows 版本信息 (#4760)

This commit is contained in:
Glavo
2025-11-08 20:13:38 +08:00
committed by GitHub
parent cbb4b0c51b
commit 783cadcf77

View File

@@ -19,7 +19,7 @@ package org.jackhuang.hmcl.util.platform;
import org.jackhuang.hmcl.util.KeyValuePairUtils; import org.jackhuang.hmcl.util.KeyValuePairUtils;
import org.jackhuang.hmcl.util.platform.windows.Kernel32; import org.jackhuang.hmcl.util.platform.windows.Kernel32;
import org.jackhuang.hmcl.util.platform.windows.WinTypes; import org.jackhuang.hmcl.util.platform.windows.WinReg;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
@@ -150,13 +150,35 @@ public enum OperatingSystem {
OSVersion.Windows windowsVersion = null; OSVersion.Windows windowsVersion = null;
Kernel32 kernel32 = Kernel32.INSTANCE; Kernel32 kernel32 = Kernel32.INSTANCE;
WinReg reg = WinReg.INSTANCE;
// Get Windows version number // Get Windows version number
if (kernel32 != null) { if (reg != null) {
WinTypes.OSVERSIONINFOEXW osVersionInfo = new WinTypes.OSVERSIONINFOEXW(); var baseVersion = OSVersion.Windows.parse(System.getProperty("os.version"));
if (kernel32.GetVersionExW(osVersionInfo)) { int majorVersion = baseVersion.major();
windowsVersion = new OSVersion.Windows(osVersionInfo.dwMajorVersion, osVersionInfo.dwMinorVersion, osVersionInfo.dwBuildNumber); int minorVersion = baseVersion.minor();
} else int buildNumber = baseVersion.build();
System.err.println("Failed to obtain OS version number (" + kernel32.GetLastError() + ")"); int revision = baseVersion.revision();
Object currentBuild = reg.queryValue(WinReg.HKEY.HKEY_LOCAL_MACHINE,
"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", "CurrentBuild");
if (currentBuild instanceof String currentBuildStr) {
try {
buildNumber = Integer.parseInt(currentBuildStr);
} catch (NumberFormatException e) {
System.err.println("Invalid Windows build number: " + currentBuildStr);
}
}
if (majorVersion >= 10) {
Object ubr = reg.queryValue(WinReg.HKEY.HKEY_LOCAL_MACHINE,
"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", "UBR");
if (ubr instanceof Integer ubrValue)
revision = ubrValue;
}
windowsVersion = new OSVersion.Windows(majorVersion, minorVersion, buildNumber, revision);
} }
if (windowsVersion == null) { if (windowsVersion == null) {
@@ -201,6 +223,16 @@ public enum OperatingSystem {
if (osName.equals("Windows 10") && windowsVersion.isAtLeast(OSVersion.WINDOWS_11)) if (osName.equals("Windows 10") && windowsVersion.isAtLeast(OSVersion.WINDOWS_11))
osName = "Windows 11"; osName = "Windows 11";
if (windowsVersion.isAtLeast(OSVersion.WINDOWS_10) && reg != null) {
Object displayVersion = reg.queryValue(WinReg.HKEY.HKEY_LOCAL_MACHINE,
"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", "DisplayVersion");
if (displayVersion instanceof String displayVersionStr
&& displayVersionStr.matches("\\d{2}H\\d")) {
osName = osName + " " + displayVersionStr;
}
}
SYSTEM_NAME = osName; SYSTEM_NAME = osName;
SYSTEM_VERSION = windowsVersion; SYSTEM_VERSION = windowsVersion;
SYSTEM_BUILD_NUMBER = windowsVersion.build(); SYSTEM_BUILD_NUMBER = windowsVersion.build();