|
| 1 | +/* |
| 2 | + Ask the user to run the program with admin privileged. |
| 3 | + |
| 4 | + Author @5mukx |
| 5 | + */ |
| 6 | + |
| 7 | +use std::process::Command; |
| 8 | +use std::ptr::null_mut; |
| 9 | +use winapi::um::shellapi::ShellExecuteW; |
| 10 | +use winapi::um::winnt::{HANDLE, TOKEN_ELEVATION, TOKEN_QUERY}; |
| 11 | +use winapi::um::processthreadsapi::OpenProcessToken; |
| 12 | +use winapi::um::securitybaseapi::GetTokenInformation; |
| 13 | +use winapi::um::winuser::{MessageBoxW, MB_ICONERROR, MB_OK}; |
| 14 | + |
| 15 | +fn is_running_as_admin() -> bool { |
| 16 | + unsafe { |
| 17 | + let mut token_handle: HANDLE = null_mut(); |
| 18 | + if OpenProcessToken( |
| 19 | + winapi::um::processthreadsapi::GetCurrentProcess(), |
| 20 | + TOKEN_QUERY, |
| 21 | + &mut token_handle, |
| 22 | + ) == 0 |
| 23 | + { |
| 24 | + return false; |
| 25 | + } |
| 26 | + |
| 27 | + let mut elevation: TOKEN_ELEVATION = std::mem::zeroed(); |
| 28 | + let mut size = 0; |
| 29 | + |
| 30 | + let success = GetTokenInformation( |
| 31 | + token_handle, |
| 32 | + winapi::um::winnt::TokenElevation, |
| 33 | + &mut elevation as *mut _ as *mut _, |
| 34 | + std::mem::size_of::<TOKEN_ELEVATION>() as u32, |
| 35 | + &mut size, |
| 36 | + ) != 0; |
| 37 | + |
| 38 | + winapi::um::handleapi::CloseHandle(token_handle); |
| 39 | + |
| 40 | + success && elevation.TokenIsElevated != 0 |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +fn run_as_admin() { |
| 45 | + unsafe { |
| 46 | + let current_exe = std::env::current_exe().unwrap(); |
| 47 | + let current_exe_wide: Vec<u16> = current_exe |
| 48 | + .to_string_lossy() |
| 49 | + .encode_utf16() |
| 50 | + .chain(Some(0)) |
| 51 | + .collect(); |
| 52 | + |
| 53 | + ShellExecuteW( |
| 54 | + null_mut(), |
| 55 | + "runas\0".encode_utf16().chain(Some(0)).collect::<Vec<u16>>().as_ptr(), |
| 56 | + current_exe_wide.as_ptr(), |
| 57 | + null_mut(), |
| 58 | + null_mut(), |
| 59 | + winapi::um::winuser::SW_SHOWNORMAL, |
| 60 | + ); |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +fn main() { |
| 65 | + // Avoid infinite relaunches |
| 66 | + let elevated_env_var = "RUNNING_ELEVATED"; |
| 67 | + |
| 68 | + if std::env::var(elevated_env_var).is_err() { |
| 69 | + if !is_running_as_admin() { |
| 70 | + println!("This program requires administrator privileges."); |
| 71 | + unsafe { |
| 72 | + MessageBoxW( |
| 73 | + null_mut(), |
| 74 | + "Please run the program as Administrator.".encode_utf16().chain(Some(0)).collect::<Vec<u16>>().as_ptr(), |
| 75 | + "Administrator Access Required".encode_utf16().chain(Some(0)).collect::<Vec<u16>>().as_ptr(), |
| 76 | + MB_ICONERROR | MB_OK, |
| 77 | + ); |
| 78 | + } |
| 79 | + |
| 80 | + std::env::set_var(elevated_env_var, "1"); |
| 81 | + run_as_admin(); |
| 82 | + |
| 83 | + std::process::exit(0); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + // Your privileged operations here |
| 88 | + println!("Running with administrator privileges!"); |
| 89 | + |
| 90 | +} |
| 91 | + |
0 commit comments