use api rather than cmd to retrieve active user name to solve

non-English problem
This commit is contained in:
rustdesk 2021-04-09 09:58:50 +08:00
parent 0503a4d5b6
commit de4bb684af
3 changed files with 40 additions and 2 deletions

View File

@ -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");
}

View File

@ -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<u16> = 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();

View File

@ -363,4 +363,21 @@ extern "C"
{
SHAddToRecentDocs(SHARD_PATHW, path);
}
} // end of extern "C"
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"