Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add windows-registry crate #2848

Merged
merged 6 commits into from
Feb 16, 2024
Merged

Add windows-registry crate #2848

merged 6 commits into from
Feb 16, 2024

Conversation

kennykerr
Copy link
Collaborator

This update adds the new windows-registry crate in the spirit of windows-result (#2847) and windows-version (#2702) to provide simple, safe, and efficient access to the Windows registry. The windows-registry crate takes advantage of the windows-result crate for error handling and works naturally with other windows-rs crates but does not depend on the larger windows or windows-sys crates.

Here's a simple example of reading registry key values:

use windows_registry::*;

fn main() -> Result<()> {
    let key = LOCAL_MACHINE.open(r#"SOFTWARE\Microsoft\Windows NT\CurrentVersion"#)?;
    println!("{}", key.get_string("ProductName")?);
    Ok(())
}

Here's an example of writing and reading values:

use windows_registry::*;

fn main() -> Result<()> {
    let key = CURRENT_USER.create(r#"software\\windows-rs"#)?;

    key.set_u32("number", 123)?;
    key.set_string("name", "Rust")?;

    println!("{}", key.get_u32("number")?);
    println!("{}", key.get_string("name")?);

    Ok(())
}

Here's an example of how you might recursively print keys and values:

use windows_registry::*;

fn main() -> Result<()> {
    let key = LOCAL_MACHINE.open(r#"SOFTWARE\Microsoft\Windows NT\CurrentVersion"#)?;
    print_tree(&key)?;
    Ok(())
}

fn print_tree(key: &Key) -> Result<()> {
    for subkey in key.keys()? {
        print_tree(&key.open(subkey)?)?;
    }

    for (name, value) in key.values()? {
        println!("{name}: {value:?}");
    }

    Ok(())
}

Copy link
Collaborator

@ChrisDenton ChrisDenton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! I think a brief top level example would be nice to have just to give people an idea of how to get started exploring the API.

crates/libs/registry/src/key.rs Outdated Show resolved Hide resolved
@kennykerr
Copy link
Collaborator Author

I also added a readme example - thanks for the feedback Chris!

@kennykerr kennykerr merged commit 9ae15f5 into master Feb 16, 2024
65 checks passed
@kennykerr kennykerr deleted the windows-registry branch February 16, 2024 18:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants