|
| 1 | +use once_cell::sync::Lazy; |
| 2 | +use windows_registry::{Value, LOCAL_MACHINE}; |
| 3 | +use windows_result::{Error, Result}; |
| 4 | +use windows_version::OsVersion; |
1 | 5 |
|
| 6 | +static OS_RELEASE: Lazy<String> = Lazy::new(release); |
| 7 | +static OS_VERSION: Lazy<String> = Lazy::new(|| version().unwrap_or_default()); |
| 8 | +pub static EOL: &str = "\r\n"; |
| 9 | + |
| 10 | +pub fn get_type() -> &'static str { |
| 11 | + // In theory there are more types linx MinGW but in practice this is good enough |
| 12 | + "Windows_NT" |
| 13 | +} |
| 14 | + |
| 15 | +pub fn get_release() -> &'static str { |
| 16 | + &OS_RELEASE |
| 17 | +} |
| 18 | + |
| 19 | +pub fn get_version() -> &'static str { |
| 20 | + &OS_VERSION |
| 21 | +} |
| 22 | + |
| 23 | +fn release() -> String { |
| 24 | + let version = OsVersion::current(); |
| 25 | + format!("{}.{}.{}", version.major, version.minor, version.build) |
| 26 | +} |
| 27 | + |
| 28 | +fn version() -> Result<String> { |
| 29 | + let version = OsVersion::current(); |
| 30 | + |
| 31 | + let registry_key = LOCAL_MACHINE.open("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion")?; |
| 32 | + let value = registry_key.get_value("ProductName")?; |
| 33 | + let Value::String(mut value) = value else { |
| 34 | + return Err(Error::empty()); |
| 35 | + }; |
| 36 | + |
| 37 | + // Windows 11 shares dwMajorVersion with Windows 10 |
| 38 | + // this workaround tries to disambiguate that by checking |
| 39 | + // if the dwBuildNumber is from Windows 11 releases (>= 22000). |
| 40 | + if version.major == 10 && version.build >= 22000 && value.starts_with("Windows 10") { |
| 41 | + value.replace_range(9..10, "1"); |
| 42 | + } |
| 43 | + |
| 44 | + Ok(value) |
| 45 | +} |
0 commit comments