Add/edit/remove regedit keys doesnt work (windows)

Hi all, im trying to add, edit and remove some registry windows keys with rust but my code doesnt work and in console i dont see nothing error, but in windows registry i dont find the values.

for example my function is like that

use std::process::Command;

fn add_remove_key .... {
    if enable {
        Command::new("reg")
            .args(&[
                "add",
                r"HKEY_LOCAL_MACHINE\<path>",
                "/v",
                "<name_key>",
                "/t",
                "REG_DWORD",
                "/d",
                "1",
                "/f",
            ])
            .spawn()
            .map_err(|_| "Failed to......".to_string())?;
    } else {
        Command::new("reg")
            .args(&[
                "delete",
                r"HKEY_LOCAL_MACHINE\<path>",
                "/v",
                "<name_key>",
                "/f",
            ])
            .spawn()
            .map_err(|_| "Failed to....".to_string())?;
    }
    Ok(())
}

Does reg set the exit code when there's an error?

You are checking the status of starting reg. You need to wait for it complete to know if it was successful.

Im learning rust from just 1 day :joy:, im using tauri, i didnt check in debug, i have still set my visual code, but i can add this detail, i waited so much, also minutes, and i didnt see nothing in console or in registry, is a command, should be very fast, and the function is not async.

As far as how to check the exit status, look at the difference between spawn() you're using, compared to the methods listed below that (output() and status()).

Unless you’re particularly interested in learning to use Command, which is a somewhat subtle API, you might be better off using one of the crates that updates the registry directly like windows-registry — Rust API Windows // Lib.rs.

2 Likes