Sample elevating

This commit is contained in:
Kevin Bost
2025-08-16 23:39:10 -07:00
parent 7bf2e49339
commit 8b1f5d06f7
2 changed files with 71 additions and 6 deletions

View File

@@ -55,6 +55,8 @@ pub fn apply_package_impl(old_locator: &VelopackLocator, package: &PathBuf, run_
vec!["apply".into(), "--norestart".into(), "--package".into(), package.into(), "--root".into(), root_path.into()];
let exe_path = std::env::current_exe()?;
let work_dir: Option<String> = None; // same as this process
// NB: show_window must be true for dialogs to be shown
// https://learn.microsoft.com/en-us/windows/win32/api/commctrl/nf-commctrl-taskdialogindirect#remarks
let process_handle = process::run_process_as_admin(&exe_path, args, work_dir, false)?;
info!("Waiting (up to 10 minutes) for elevated process (pid: {}) to exit...", process_handle.pid());

View File

@@ -1,10 +1,73 @@
use anyhow::Result;
use std::{fs::OpenOptions, io::Write};
use chrono::Local;
use velopack_bins::windows::splash;
use std::{collections::HashMap, fs::OpenOptions, io::Write, path::PathBuf, thread};
fn log_message(message: &str) -> Result<()> {
// println!("{}", message);
// let exe_path = std::env::current_exe()?;
// let binding = PathBuf::from(".");
// let exe_dir = exe_path.parent().unwrap_or(&binding);
// let log_path = exe_dir.join("testapp.log");
// let mut file = OpenOptions::new().create(true).append(true).open(log_path)?;
// writeln!(file, "[{}] {}", std::process::id(), message)?;
log::info!("[{}] {}", std::process::id(), message);
Ok(())
}
fn main() -> Result<()> {
if let Err(e) = run() {
let error_msg = format!("Error: {}", e);
let _ = log_message(&error_msg);
return Err(e);
}
Ok(())
}
fn run() -> Result<()> {
let args = std::env::args().collect::<Vec<_>>();
let exe_path = std::env::current_exe()?;
let binding = PathBuf::from(".");
let exe_dir = exe_path.parent().unwrap_or(&binding);
let log_path = exe_dir.join("testapp.log");
velopack::logging::init_logging("testapp", Some(&log_path), false, true, None);
// Common paths
let exe_path = r"D:\Dev\Velopack\velopack\target\debug\testapp.exe";
let working_dir = r"D:\Dev\Velopack\velopack\target\debug";
// Common arguments
let step0_arg = "--step0";
let step1_arg = "--step1";
//let step2_arg = "--step2";
let mut set_env = HashMap::new();
set_env.insert("TEST_ENV".to_string(), "true".to_string());
if args.iter().any(|arg| arg == step1_arg) {
log_message("showing progress (launch self FROM elevated process)...")?;
let tx = splash::show_progress_dialog("Title", "Message");
let _ = tx.send(splash::MSG_INDEFINITE);
thread::sleep(std::time::Duration::from_secs(3));
let _ = tx.send(splash::MSG_CLOSE);
//} else if args.iter().any(|arg| arg == step2_arg) {
// log_message("We have successfully launched from an elevated process!")?;
} else if args.iter().any(|arg| arg == step0_arg) {
log_message("About to run step 1 (launch self as admin)...")?;
velopack::process::run_process_as_admin(exe_path, vec![step1_arg.into()], Some(working_dir), true)?;
} else if args.len() < 2 {
// log_message(&format!("Args: {:?}", args))?;
log_message("About to run step 0 (relaunch self as NOT admin)")?;
velopack::process::run_process(exe_path, vec![step0_arg.into()], Some(working_dir), true, Some(set_env))?;
}
// thread::sleep(std::time::Duration::from_secs(60));
fn main() -> Result<()> {
let args: Vec<String> = std::env::args().skip(1).collect();
let line = args.join(" ") + "\n";
let mut file = OpenOptions::new().create(true).append(true).open("args.txt")?;
file.write_all(line.as_bytes())?;
Ok(())
}