nt_version/lib.rs
1//! Queries the major, minor and build version of Windows (NT) efficiently
2//! with usage of undocumented NTDLL functions. **This crate is no_std.**
3//!
4//! It only has one function: [get](fn.get.html),
5//! and it's recommended you use it explicitly like `nt_version::get()` because of this.
6//!
7//!
8//! ## Build Error?
9//! If building fails with a linker error, you're missing `ntdll.lib` from your system.
10//! It doesn't come on older versions of Windows with the SDK and you need to install the DDK.
11//!
12//! You can alternatively enable the **fallback** feature which queries the function pointer at runtime.
13
14#![no_std]
15
16#[cfg(not(target_os = "windows"))]
17compile_error!("This crate is for querying Windows, but the target isn't Windows.");
18
19#[cfg(not(feature = "fallback"))]
20mod internal {
21 #[link(name = "ntdll")]
22 extern "system" {
23 pub fn RtlGetNtVersionNumbers(major: *mut u32, minor: *mut u32, build: *mut u32);
24 }
25}
26
27#[cfg(feature = "fallback")]
28mod internal {
29 #![allow(non_snake_case)]
30
31 use core::{mem::transmute, ptr};
32 use winapi::um::libloaderapi::{GetModuleHandleA, GetProcAddress};
33
34 static mut NTDLL_FUNCTION: *const () = ptr::null();
35 pub unsafe fn RtlGetNtVersionNumbers(major: *mut u32, minor: *mut u32, build: *mut u32) {
36 if NTDLL_FUNCTION.is_null() {
37 NTDLL_FUNCTION = GetProcAddress(
38 GetModuleHandleA(b"ntdll.dll\0".as_ptr() as *const _),
39 b"RtlGetNtVersionNumbers\0".as_ptr() as *const _,
40 ) as *const _;
41 }
42 transmute::<_, extern "system" fn(*mut u32, *mut u32, *mut u32)>(NTDLL_FUNCTION)(
43 major, minor, build,
44 );
45 }
46}
47
48/// Queries the (major, minor, build) version of the Windows NT system.
49/// The versions correspond to this table (transcribed from [here](http://www.geoffchappell.com/studies/windows/win32/ntdll/history/index.htm)):
50///
51/// | Version | Windows | NT |
52/// | ------- | ------- | -- |
53/// | 3.51 | | Windows NT 3.51 |
54/// | 4.0 | Windows 95 | Windows NT 4.0 |
55/// | 4.10 | Windows 98 | |
56/// | 4.90 | Windows Me | |
57/// | 5.0 | | Windows 2000 |
58/// | 5.1 | | Windows XP |
59/// | 5.2 | | Windows Server 2003 |
60/// | 6.0 | | Windows Vista / Windows Server 2008 |
61/// | 6.1 | | Windows 7 / Windows Server 2008 R2 |
62/// | 6.2 | | Windows 8 |
63/// | 6.3 | | Windows 8.1 |
64/// | 10.0 | | Windows 10 |
65pub fn get() -> (u32, u32, u32) {
66 let (mut major, mut minor, mut build) = (0u32, 0u32, 0u32);
67 unsafe {
68 internal::RtlGetNtVersionNumbers(&mut major as _, &mut minor as _, &mut build as _);
69 }
70 (major, minor, build)
71}