Kaynağa Gözat

feat: migrate manual implementation to the light `windows-version` crate (#8243)

Amr Bashir 1 yıl önce
ebeveyn
işleme
5e84e92e99

+ 5 - 0
.changes/tauri-utils-windows-version.md

@@ -0,0 +1,5 @@
+---
+'tauri-utils': 'minor:breaking'
+---
+
+Changed `platform::windows_version` to return a `(u32, u32, u32)` instead of `Option<(u32, u32, u32)>`

+ 2 - 9
core/tauri-utils/Cargo.toml

@@ -43,15 +43,8 @@ log = "0.4.20"
 [target."cfg(target_os = \"linux\")".dependencies]
 heck = "0.4"
 
-[target."cfg(windows)".dependencies.windows]
-version = "0.51.1"
-features = [
-  "implement",
-  "Win32_Foundation",
-  "Win32_System_Com",
-  "Win32_System_LibraryLoader",
-  "Win32_System_SystemInformation"
-]
+[target."cfg(windows)".dependencies]
+windows-version = "0.1"
 
 [features]
 build = [ "proc-macro2", "quote" ]

+ 6 - 72
core/tauri-utils/src/platform.rs

@@ -257,85 +257,19 @@ pub fn resource_dir(package_info: &PackageInfo, env: &Env) -> crate::Result<Path
 }
 
 #[cfg(windows)]
-pub use windows_platform::{get_function_impl, is_windows_7, windows_version};
+pub use windows_platform::{is_windows_7, windows_version};
 
 #[cfg(windows)]
 mod windows_platform {
-  use std::{iter::once, os::windows::prelude::OsStrExt};
-  use windows::{
-    core::{PCSTR, PCWSTR},
-    Win32::{
-      Foundation::FARPROC,
-      System::{
-        LibraryLoader::{GetProcAddress, LoadLibraryW},
-        SystemInformation::OSVERSIONINFOW,
-      },
-    },
-  };
-
   /// Checks if we're running on Windows 7.
   pub fn is_windows_7() -> bool {
-    if let Some(v) = windows_version() {
-      // windows 7 is 6.1
-      if v.0 == 6 && v.1 == 1 {
-        return true;
-      }
-    }
-    false
-  }
-
-  fn encode_wide(string: impl AsRef<std::ffi::OsStr>) -> Vec<u16> {
-    string.as_ref().encode_wide().chain(once(0)).collect()
-  }
-
-  /// Helper function to dynamically load function pointer.
-  /// `library` and `function` must be null-terminated.
-  pub fn get_function_impl(library: &str, function: &str) -> Option<FARPROC> {
-    let library = encode_wide(library);
-    assert_eq!(function.chars().last(), Some('\0'));
-    let function = PCSTR::from_raw(function.as_ptr());
-
-    // Library names we will use are ASCII so we can use the A version to avoid string conversion.
-    let module = unsafe { LoadLibraryW(PCWSTR::from_raw(library.as_ptr())) }.unwrap_or_default();
-    if module.is_invalid() {
-      None
-    } else {
-      Some(unsafe { GetProcAddress(module, function) })
-    }
-  }
-
-  macro_rules! get_function {
-    ($lib:expr, $func:ident) => {
-      get_function_impl(concat!($lib, '\0'), concat!(stringify!($func), '\0'))
-        .map(|f| unsafe { std::mem::transmute::<windows::Win32::Foundation::FARPROC, $func>(f) })
-    };
+    let v = windows_version();
+    v.0 == 6 && v.1 == 1
   }
 
   /// Returns a tuple of (major, minor, buildnumber) for the Windows version.
-  pub fn windows_version() -> Option<(u32, u32, u32)> {
-    type RtlGetVersion = unsafe extern "system" fn(*mut OSVERSIONINFOW) -> i32;
-    let handle = get_function!("ntdll.dll", RtlGetVersion);
-    if let Some(rtl_get_version) = handle {
-      unsafe {
-        let mut vi = OSVERSIONINFOW {
-          dwOSVersionInfoSize: 0,
-          dwMajorVersion: 0,
-          dwMinorVersion: 0,
-          dwBuildNumber: 0,
-          dwPlatformId: 0,
-          szCSDVersion: [0; 128],
-        };
-
-        let status = (rtl_get_version)(&mut vi as _);
-
-        if status >= 0 {
-          Some((vi.dwMajorVersion, vi.dwMinorVersion, vi.dwBuildNumber))
-        } else {
-          None
-        }
-      }
-    } else {
-      None
-    }
+  pub fn windows_version() -> (u32, u32, u32) {
+    let v = windows_version::OsVersion::current();
+    (v.major, v.minor, v.build)
   }
 }

+ 1 - 1
core/tauri/src/vibrancy/windows.rs

@@ -12,7 +12,7 @@ use std::ffi::c_void;
 use crate::utils::config::WindowEffectsConfig;
 use crate::window::{Color, Effect};
 use raw_window_handle::HasRawWindowHandle;
-use tauri_utils::platform::{get_function_impl, is_windows_7, windows_version};
+use tauri_utils::platform::{is_windows_7, windows_version};
 use windows::Win32::Foundation::HWND;
 
 pub fn apply_effects(window: impl HasRawWindowHandle, effects: WindowEffectsConfig) {