diff --git a/build.rs b/build.rs index 7127fa0fa..d3cb36ac5 100644 --- a/build.rs +++ b/build.rs @@ -1,7 +1,7 @@ #[cfg(windows)] fn build_windows() { cc::Build::new().file("src/windows.cc").compile("windows"); - // println!("cargo:rustc-link-lib=WtsApi32"); + println!("cargo:rustc-link-lib=WtsApi32"); println!("cargo:rerun-if-changed=build.rs"); println!("cargo:rerun-if-changed=windows.cc"); } diff --git a/src/platform/windows.rs b/src/platform/windows.rs index c4fdaf66d..11145ecb1 100644 --- a/src/platform/windows.rs +++ b/src/platform/windows.rs @@ -633,6 +633,26 @@ fn get_error() -> String { } } +pub fn get_active_username() -> String { + let name = crate::username(); + if name != "SYSTEM" { + return name; + } + extern "C" { + fn get_active_user(path: *mut u16, n: u32) -> u32; + } + let buff_size = 256; + let mut buff: Vec = Vec::with_capacity(buff_size); + buff.resize(buff_size, 0); + let n = unsafe { get_active_user(buff.as_mut_ptr(), buff_size as _) }; + if n == 0 { + return "".to_owned(); + } + let sl = unsafe { std::slice::from_raw_parts(buff.as_ptr(), n as _) }; + String::from_utf16(sl).unwrap_or("??".to_owned()).trim_end_matches('\0').to_owned() +} + +/* pub fn get_active_username() -> String { use std::os::windows::process::CommandExt; let name = crate::username(); @@ -654,6 +674,7 @@ pub fn get_active_username() -> String { } return "".to_owned(); } +*/ pub fn is_prelogin() -> bool { let username = get_active_username(); diff --git a/src/windows.cc b/src/windows.cc index 4614f40ce..8a937a060 100644 --- a/src/windows.cc +++ b/src/windows.cc @@ -363,4 +363,21 @@ extern "C" { SHAddToRecentDocs(SHARD_PATHW, path); } -} // end of extern "C" \ No newline at end of file + + uint32_t get_active_user(PWSTR bufin, uint32_t nin) + { + uint32_t nout = 0; + auto id = WTSGetActiveConsoleSessionId(); + PWSTR buf = NULL; + DWORD n = 0; + if (WTSQuerySessionInformationW(NULL, id, WTSUserName, &buf, &n)) + { + if (buf) { + nout = min(nin, n); + memcpy(bufin, buf, nout); + WTSFreeMemory(buf); + } + } + return nout; + } +} // end of extern "C"