use crate::utils::{HIDE_WINDOW_FLAG, Println};
use ansi_term::Color::{Red, Yellow};
use std::env;
use std::os::windows::process::CommandExt;
use std::process::{Command, exit};
pub fn is_elevated() -> bool {
unsafe {
use std::mem;
use winapi::shared::minwindef::DWORD;
use winapi::shared::minwindef::LPVOID;
use winapi::um::processthreadsapi::GetCurrentProcess;
use winapi::um::processthreadsapi::OpenProcessToken;
use winapi::um::securitybaseapi::GetTokenInformation;
use winapi::um::winnt::HANDLE;
use winapi::um::winnt::TOKEN_ELEVATION;
use winapi::um::winnt::TOKEN_QUERY;
use winapi::um::winnt::TokenElevation;
let mut current_token_ptr: HANDLE = mem::zeroed();
let mut token_elevation: TOKEN_ELEVATION = mem::zeroed();
let token_elevation_type_ptr: *mut TOKEN_ELEVATION = &mut token_elevation;
let mut size: DWORD = 0;
let result = OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &mut current_token_ptr);
if result != 0 {
let buffer_len = size_of::<TOKEN_ELEVATION>() as u32;
let result = GetTokenInformation(
current_token_ptr,
TokenElevation,
token_elevation_type_ptr as LPVOID,
buffer_len,
&mut size,
);
if result != 0 {
return token_elevation.TokenIsElevated != 0;
}
}
}
false
}
pub fn request_restart_with_privileges_elevate(is_hide_window: bool, is_exit: bool) {
let current_exe = env::current_exe().expect("Failed to get current executable path");
println!(
"current executable path is {}",
Yellow.paint(current_exe.display().to_string())
);
let mut com = Command::new("powershell");
if is_hide_window {
com.creation_flags(HIDE_WINDOW_FLAG);
}
let output = com
.arg(format!(
"powershell -Command \"Start-Process '{}'\" -Verb RunAs",
current_exe.display()
))
.output()
.expect("Failed to execute command");
output.print_ln();
let status = output.status;
if is_exit {
if status.success() && status.code().unwrap() == 0 {
exit(0);
} else {
println!("{} {}", Red.paint("用户取消授权!"), status.code().unwrap());
}
}
}