How can a new process be launched that prompts UAC?


#1

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?


#2

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


#3

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