How can a new process be launched that prompts UAC?

I'd like to start a new process from Rust that requires a UAC elevation prompt. I am using std::process::Command.

This simplified example code fails with error:

thread 'main' panicked at 'failed to execute process: 
Error { repr: Os { code: 740, message: "The requested operation requires elevation." } }', src\libcore\result.rs:859
use std::process::Command;

fn main() {
    let output = if cfg!(target_os = "windows") {
        Command::new("regedit")
            .output()
            .expect("failed to execute process")
    }
}

This code will launch the new process and properly prompt the user, but it brings up an annoying cmd window for the user that I'd like to avoid:

use std::process::Command;

fn main() {
    let output = if cfg!(target_os = "windows") {
        Command::new("cmd")
            .args(&["/C", "regedit"])
            .output()
            .expect("failed to execute process")
    }
}

Is there a way to launch the new process and have it prompt for UAC but not open the cmd window?

1 Like

i don't have windows anymore but i think there is a runas command

You'll want to use ShellExecuteEx with the open or runas verb.

https://stackoverflow.com/questions/11586139/how-to-run-application-which-requires-admin-rights-from-one-that-doesnt-have-th

2 Likes