Skip to content

Commit 448ec24

Browse files
committed
Keep the old code for win7.
1 parent c777d98 commit 448ec24

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

library/std/src/sys/windows/c/windows_sys.lst

+1
Original file line numberDiff line numberDiff line change
@@ -2537,6 +2537,7 @@ Windows.Win32.System.Threading.InitOnceComplete
25372537
Windows.Win32.System.Threading.LPPROC_THREAD_ATTRIBUTE_LIST
25382538
Windows.Win32.System.Threading.LPTHREAD_START_ROUTINE
25392539
Windows.Win32.System.Threading.NORMAL_PRIORITY_CLASS
2540+
Windows.Win32.System.Threading.OpenProcessToken
25402541
Windows.Win32.System.Threading.PROCESS_CREATION_FLAGS
25412542
Windows.Win32.System.Threading.PROCESS_INFORMATION
25422543
Windows.Win32.System.Threading.PROCESS_MODE_BACKGROUND_BEGIN

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

+8
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@
88

99
#![allow(non_snake_case, non_upper_case_globals, non_camel_case_types, dead_code, clippy::all)]
1010
#[link(name = "advapi32")]
11+
extern "system" {
12+
pub fn OpenProcessToken(
13+
processhandle: HANDLE,
14+
desiredaccess: TOKEN_ACCESS_MASK,
15+
tokenhandle: *mut HANDLE,
16+
) -> BOOL;
17+
}
18+
#[link(name = "advapi32")]
1119
extern "system" {
1220
#[link_name = "SystemFunction036"]
1321
pub fn RtlGenRandom(randombuffer: *mut ::core::ffi::c_void, randombufferlength: u32)

library/std/src/sys/windows/os.rs

+26-1
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ pub fn temp_dir() -> PathBuf {
318318
super::fill_utf16_buf(|buf, sz| unsafe { c::GetTempPath2W(sz, buf) }, super::os2path).unwrap()
319319
}
320320

321-
#[cfg(not(target_vendor = "uwp"))]
321+
#[cfg(and(not(target_vendor = "uwp"), not(target_vendor = "win7"))]
322322
fn home_dir_crt() -> Option<PathBuf> {
323323
unsafe {
324324
// Defined in processthreadsapi.h.
@@ -342,6 +342,31 @@ fn home_dir_crt() -> Option<PathBuf> {
342342
}
343343
}
344344

345+
#[cfg(target_vendor = "win7")]
346+
fn home_dir_crt() -> Option<PathBuf> {
347+
unsafe {
348+
use crate::sys::handle::Handle;
349+
350+
let me = c::GetCurrentProcess();
351+
let mut token = ptr::null_mut();
352+
if c::OpenProcessToken(me, c::TOKEN_READ, &mut token) == 0 {
353+
return None;
354+
}
355+
let _handle = Handle::from_raw_handle(token);
356+
super::fill_utf16_buf(
357+
|buf, mut sz| {
358+
match c::GetUserProfileDirectoryW(token, buf, &mut sz) {
359+
0 if api::get_last_error().code != c::ERROR_INSUFFICIENT_BUFFER => 0,
360+
0 => sz,
361+
_ => sz - 1, // sz includes the null terminator
362+
}
363+
},
364+
super::os2path,
365+
)
366+
.ok()
367+
}
368+
}
369+
345370
#[cfg(target_vendor = "uwp")]
346371
fn home_dir_crt() -> Option<PathBuf> {
347372
None

0 commit comments

Comments
 (0)