Skip to content

Commit

Permalink
Add static check indicating NT vs 9x/ME
Browse files Browse the repository at this point in the history
This will be used/needed for some APIs that exist on both platforms but with different behavior/capabilities.
  • Loading branch information
seritools committed Dec 29, 2023
1 parent e3f6ad0 commit f841709
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 0 deletions.
3 changes: 3 additions & 0 deletions library/std/src/sys/windows/c/windows_sys.lst
Original file line number Diff line number Diff line change
Expand Up @@ -2623,3 +2623,6 @@ Windows.Win32.System.Threading.GetCurrentThreadId
Windows.Win32.Networking.WinSock.WSAPROTOCOL_INFOA
Windows.Win32.Networking.WinSock.WSASocketA
Windows.Win32.Networking.WinSock.WSADuplicateSocketA

// NT vs 9x compat
Windows.Win32.System.SystemInformation.GetVersion
4 changes: 4 additions & 0 deletions library/std/src/sys/windows/c/windows_sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,10 @@ extern "system" {
pub fn GetTickCount() -> u32;
}
#[link(name = "kernel32")]
extern "system" {
pub fn GetVersion() -> u32;
}
#[link(name = "kernel32")]
extern "system" {
pub fn GetWindowsDirectoryW(lpbuffer: PWSTR, usize: u32) -> u32;
}
Expand Down
5 changes: 5 additions & 0 deletions library/std/src/sys/windows/compat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ use crate::ptr::NonNull;
use crate::sync::atomic::Ordering;
use crate::sys::c;

mod version;
pub use version::is_windows_nt;

// This uses a static initializer to preload some imported functions.
// The CRT (C runtime) executes static initializers before `main`
// is called (for binaries) and before `DllMain` is called (for DLLs).
Expand Down Expand Up @@ -63,6 +66,8 @@ unsafe extern "C" fn init() {
// because this function runs during global initialization. For example, DO NOT
// do any dynamic allocation, don't call LoadLibrary, etc.

version::init_windows_version_check();

// check all the different synchronization primitives ...
load_try_enter_critical_section_function();
load_srw_functions();
Expand Down
15 changes: 15 additions & 0 deletions library/std/src/sys/windows/compat/version.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use crate::sys::c;

static mut IS_NT: bool = true;

pub fn init_windows_version_check() {
// according to old MSDN info, the high-order bit is set only on 95/98/ME.
unsafe { IS_NT = c::GetVersion() < 0x8000_0000 };
}

/// Returns true if we are running on a Windows NT-based system. Only use this for APIs where the
/// same API differs in behavior or capability on 9x/ME compared to NT.
#[inline(always)]
pub fn is_windows_nt() -> bool {
unsafe { IS_NT }
}

0 comments on commit f841709

Please sign in to comment.