Skip to content

Commit f9d7036

Browse files
committed
Add startup check indicating NT vs 9x/ME
This will be used/needed for some APIs that exist on both platforms but with different behavior/capabilities.
1 parent 16aaea2 commit f9d7036

File tree

4 files changed

+38
-0
lines changed

4 files changed

+38
-0
lines changed

library/std/src/sys/pal/windows/c/bindings.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2495,6 +2495,7 @@ Windows.Win32.System.SystemInformation.GetSystemDirectoryW
24952495
Windows.Win32.System.SystemInformation.GetSystemInfo
24962496
Windows.Win32.System.SystemInformation.GetSystemTimeAsFileTime
24972497
Windows.Win32.System.SystemInformation.GetSystemTimePreciseAsFileTime
2498+
Windows.Win32.System.SystemInformation.GetVersion
24982499
Windows.Win32.System.SystemInformation.GetWindowsDirectoryW
24992500
Windows.Win32.System.SystemInformation.PROCESSOR_ARCHITECTURE
25002501
Windows.Win32.System.SystemInformation.SYSTEM_INFO

library/std/src/sys/pal/windows/c/windows_sys.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ windows_targets::link!("kernel32.dll" "system" fn GetSystemInfo(lpsysteminfo : *
6161
windows_targets::link!("kernel32.dll" "system" fn GetSystemTimeAsFileTime(lpsystemtimeasfiletime : *mut FILETIME));
6262
windows_targets::link!("kernel32.dll" "system" fn GetSystemTimePreciseAsFileTime(lpsystemtimeasfiletime : *mut FILETIME));
6363
windows_targets::link!("kernel32.dll" "system" fn GetTempPathW(nbufferlength : u32, lpbuffer : PWSTR) -> u32);
64+
windows_targets::link!("kernel32.dll" "system" fn GetVersion() -> u32);
6465
windows_targets::link!("kernel32.dll" "system" fn GetWindowsDirectoryW(lpbuffer : PWSTR, usize : u32) -> u32);
6566
windows_targets::link!("kernel32.dll" "system" fn InitOnceBeginInitialize(lpinitonce : *mut INIT_ONCE, dwflags : u32, fpending : *mut BOOL, lpcontext : *mut *mut core::ffi::c_void) -> BOOL);
6667
windows_targets::link!("kernel32.dll" "system" fn InitOnceComplete(lpinitonce : *mut INIT_ONCE, dwflags : u32, lpcontext : *const core::ffi::c_void) -> BOOL);

library/std/src/sys/pal/windows/compat.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ use crate::ffi::{CStr, c_void};
2323
use crate::ptr::NonNull;
2424
use crate::sys::c;
2525

26+
#[cfg(target_vendor = "rust9x")]
27+
pub(crate) mod checks;
28+
2629
// This uses a static initializer to preload some imported functions.
2730
// The CRT (C runtime) executes static initializers before `main`
2831
// is called (for binaries) and before `DllMain` is called (for DLLs).
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
use crate::sys::c;
2+
3+
/// Returns true if we are running on a Windows NT-based system. Only use this for APIs where the
4+
/// same API differs in behavior or capability on 9x/ME compared to NT.
5+
#[allow(dead_code)]
6+
#[inline(always)]
7+
#[cfg(target_arch = "x86")]
8+
pub fn is_windows_nt() -> bool {
9+
unsafe { IS_NT }
10+
}
11+
12+
#[allow(dead_code)]
13+
#[inline(always)]
14+
#[cfg(target_arch = "x86_64")]
15+
pub fn is_windows_nt() -> bool {
16+
true // let me know once someone ported 9x to 64bit LOL
17+
}
18+
19+
pub fn init_rust9x_checks() {
20+
// DO NOT do anything interesting or complicated in this function! DO NOT call
21+
// any Rust functions or CRT functions if those functions touch any global state,
22+
// because this function runs during global initialization. For example, DO NOT
23+
// do any dynamic allocation, don't call LoadLibrary, etc.
24+
25+
init_windows_version_check();
26+
}
27+
28+
static mut IS_NT: bool = true;
29+
30+
fn init_windows_version_check() {
31+
// according to old MSDN info, the high-order bit is set only on 95/98/ME.
32+
unsafe { IS_NT = c::GetVersion() < 0x8000_0000 };
33+
}

0 commit comments

Comments
 (0)