winhelp.rs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #![cfg(target_os = "windows")]
  2. #![allow(non_snake_case)]
  3. #![allow(non_camel_case_types)]
  4. //!
  5. //! From https://github.com/tauri-apps/window-vibrancy/blob/dev/src/windows.rs
  6. //!
  7. use windows_sys::Win32::{
  8. Foundation::*,
  9. System::{LibraryLoader::*, SystemInformation::*},
  10. };
  11. fn get_function_impl(library: &str, function: &str) -> Option<FARPROC> {
  12. assert_eq!(library.chars().last(), Some('\0'));
  13. assert_eq!(function.chars().last(), Some('\0'));
  14. let module = unsafe { LoadLibraryA(library.as_ptr()) };
  15. if module == 0 {
  16. return None;
  17. }
  18. Some(unsafe { GetProcAddress(module, function.as_ptr()) })
  19. }
  20. macro_rules! get_function {
  21. ($lib:expr, $func:ident) => {
  22. get_function_impl(concat!($lib, '\0'), concat!(stringify!($func), '\0')).map(|f| unsafe {
  23. std::mem::transmute::<::windows_sys::Win32::Foundation::FARPROC, $func>(f)
  24. })
  25. };
  26. }
  27. /// Returns a tuple of (major, minor, buildnumber)
  28. fn get_windows_ver() -> Option<(u32, u32, u32)> {
  29. type RtlGetVersion = unsafe extern "system" fn(*mut OSVERSIONINFOW) -> i32;
  30. let handle = get_function!("ntdll.dll", RtlGetVersion);
  31. if let Some(rtl_get_version) = handle {
  32. unsafe {
  33. let mut vi = OSVERSIONINFOW {
  34. dwOSVersionInfoSize: 0,
  35. dwMajorVersion: 0,
  36. dwMinorVersion: 0,
  37. dwBuildNumber: 0,
  38. dwPlatformId: 0,
  39. szCSDVersion: [0; 128],
  40. };
  41. let status = (rtl_get_version)(&mut vi as _);
  42. if status >= 0 {
  43. Some((vi.dwMajorVersion, vi.dwMinorVersion, vi.dwBuildNumber))
  44. } else {
  45. None
  46. }
  47. }
  48. } else {
  49. None
  50. }
  51. }
  52. pub fn is_win11() -> bool {
  53. let v = get_windows_ver().unwrap_or_default();
  54. v.2 >= 22000
  55. }
  56. #[test]
  57. fn test_version() {
  58. dbg!(get_windows_ver().unwrap_or_default());
  59. }