Skip to content

Commit 5e84e92

Browse files
authored
feat: migrate manual implementation to the light windows-version crate (#8243)
1 parent f93148e commit 5e84e92

4 files changed

Lines changed: 14 additions & 82 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'tauri-utils': 'minor:breaking'
3+
---
4+
5+
Changed `platform::windows_version` to return a `(u32, u32, u32)` instead of `Option<(u32, u32, u32)>`

core/tauri-utils/Cargo.toml

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,8 @@ log = "0.4.20"
4343
[target."cfg(target_os = \"linux\")".dependencies]
4444
heck = "0.4"
4545

46-
[target."cfg(windows)".dependencies.windows]
47-
version = "0.51.1"
48-
features = [
49-
"implement",
50-
"Win32_Foundation",
51-
"Win32_System_Com",
52-
"Win32_System_LibraryLoader",
53-
"Win32_System_SystemInformation"
54-
]
46+
[target."cfg(windows)".dependencies]
47+
windows-version = "0.1"
5548

5649
[features]
5750
build = [ "proc-macro2", "quote" ]

core/tauri-utils/src/platform.rs

Lines changed: 6 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -257,85 +257,19 @@ pub fn resource_dir(package_info: &PackageInfo, env: &Env) -> crate::Result<Path
257257
}
258258

259259
#[cfg(windows)]
260-
pub use windows_platform::{get_function_impl, is_windows_7, windows_version};
260+
pub use windows_platform::{is_windows_7, windows_version};
261261

262262
#[cfg(windows)]
263263
mod windows_platform {
264-
use std::{iter::once, os::windows::prelude::OsStrExt};
265-
use windows::{
266-
core::{PCSTR, PCWSTR},
267-
Win32::{
268-
Foundation::FARPROC,
269-
System::{
270-
LibraryLoader::{GetProcAddress, LoadLibraryW},
271-
SystemInformation::OSVERSIONINFOW,
272-
},
273-
},
274-
};
275-
276264
/// Checks if we're running on Windows 7.
277265
pub fn is_windows_7() -> bool {
278-
if let Some(v) = windows_version() {
279-
// windows 7 is 6.1
280-
if v.0 == 6 && v.1 == 1 {
281-
return true;
282-
}
283-
}
284-
false
285-
}
286-
287-
fn encode_wide(string: impl AsRef<std::ffi::OsStr>) -> Vec<u16> {
288-
string.as_ref().encode_wide().chain(once(0)).collect()
289-
}
290-
291-
/// Helper function to dynamically load function pointer.
292-
/// `library` and `function` must be null-terminated.
293-
pub fn get_function_impl(library: &str, function: &str) -> Option<FARPROC> {
294-
let library = encode_wide(library);
295-
assert_eq!(function.chars().last(), Some('\0'));
296-
let function = PCSTR::from_raw(function.as_ptr());
297-
298-
// Library names we will use are ASCII so we can use the A version to avoid string conversion.
299-
let module = unsafe { LoadLibraryW(PCWSTR::from_raw(library.as_ptr())) }.unwrap_or_default();
300-
if module.is_invalid() {
301-
None
302-
} else {
303-
Some(unsafe { GetProcAddress(module, function) })
304-
}
305-
}
306-
307-
macro_rules! get_function {
308-
($lib:expr, $func:ident) => {
309-
get_function_impl(concat!($lib, '\0'), concat!(stringify!($func), '\0'))
310-
.map(|f| unsafe { std::mem::transmute::<windows::Win32::Foundation::FARPROC, $func>(f) })
311-
};
266+
let v = windows_version();
267+
v.0 == 6 && v.1 == 1
312268
}
313269

314270
/// Returns a tuple of (major, minor, buildnumber) for the Windows version.
315-
pub fn windows_version() -> Option<(u32, u32, u32)> {
316-
type RtlGetVersion = unsafe extern "system" fn(*mut OSVERSIONINFOW) -> i32;
317-
let handle = get_function!("ntdll.dll", RtlGetVersion);
318-
if let Some(rtl_get_version) = handle {
319-
unsafe {
320-
let mut vi = OSVERSIONINFOW {
321-
dwOSVersionInfoSize: 0,
322-
dwMajorVersion: 0,
323-
dwMinorVersion: 0,
324-
dwBuildNumber: 0,
325-
dwPlatformId: 0,
326-
szCSDVersion: [0; 128],
327-
};
328-
329-
let status = (rtl_get_version)(&mut vi as _);
330-
331-
if status >= 0 {
332-
Some((vi.dwMajorVersion, vi.dwMinorVersion, vi.dwBuildNumber))
333-
} else {
334-
None
335-
}
336-
}
337-
} else {
338-
None
339-
}
271+
pub fn windows_version() -> (u32, u32, u32) {
272+
let v = windows_version::OsVersion::current();
273+
(v.major, v.minor, v.build)
340274
}
341275
}

core/tauri/src/vibrancy/windows.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use std::ffi::c_void;
1212
use crate::utils::config::WindowEffectsConfig;
1313
use crate::window::{Color, Effect};
1414
use raw_window_handle::HasRawWindowHandle;
15-
use tauri_utils::platform::{get_function_impl, is_windows_7, windows_version};
15+
use tauri_utils::platform::{is_windows_7, windows_version};
1616
use windows::Win32::Foundation::HWND;
1717

1818
pub fn apply_effects(window: impl HasRawWindowHandle, effects: WindowEffectsConfig) {

0 commit comments

Comments
 (0)