Rust bindings to MS Windows Registry API. Work in progress.
Current features:
* Basic registry operations:
* open/create/delete keys
* read and write values
* seamless conversion between REG_*
types and rust primitives
* String
and OsString
<= REG_SZ
, REG_EXPAND_SZ
or REG_MULTI_SZ
* String
, &str
and OsStr
=> REG_SZ
* u32
<=> REG_DWORD
* u64
<=> REG_QWORD
* Iteration through key names and through values
* Transactions
* Transacted serialization of rust types into/from registry (only primitives and structures for now)
```toml
[dependencies] winreg = "0.4" ```
```rust extern crate winreg; use std::path::Path; use std::io; use winreg::RegKey; use winreg::enums::*;
fn main() { println!("Reading some system info..."); let hklm = RegKey::predef(HKEYLOCALMACHINE); let curver = hklm.opensubkeywithflags("SOFTWARE\Microsoft\Windows\CurrentVersion", KEYREAD).unwrap(); let pf: String = curver.getvalue("ProgramFilesDir").unwrap(); let dp: String = curver.getvalue("DevicePath").unwrap(); println!("ProgramFiles = {}\nDevicePath = {}", pf, dp); let info = curver.query_info().unwrap(); println!("info = {:?}", info);
println!("And now lets write something...");
let hkcu = RegKey::predef(HKEY_CURRENT_USER);
let path = Path::new("Software").join("WinregRsExample1");
let key = hkcu.create_subkey(&path).unwrap();
key.set_value("TestSZ", &"written by Rust").unwrap();
let sz_val: String = key.get_value("TestSZ").unwrap();
key.delete_value("TestSZ").unwrap();
println!("TestSZ = {}", sz_val);
key.set_value("TestDWORD", &1234567890u32).unwrap();
let dword_val: u32 = key.get_value("TestDWORD").unwrap();
println!("TestDWORD = {}", dword_val);
key.set_value("TestQWORD", &1234567891011121314u64).unwrap();
let qword_val: u64 = key.get_value("TestQWORD").unwrap();
println!("TestQWORD = {}", qword_val);
key.create_subkey("sub\\key").unwrap();
hkcu.delete_subkey_all(&path).unwrap();
println!("Trying to open nonexistent key...");
let key2 = hkcu.open_subkey(&path)
.unwrap_or_else(|e| match e.kind() {
io::ErrorKind::NotFound => panic!("Key doesn't exist"),
io::ErrorKind::PermissionDenied => panic!("Access denied"),
_ => panic!("{:?}", e)
});
} ```
```rust extern crate winreg; use winreg::RegKey; use winreg::enums::*;
fn main() { println!("File extensions, registered in system:"); for i in RegKey::predef(HKEYCLASSESROOT) .enumkeys().map(|x| x.unwrap()) .filter(|x| x.startswith(".")) { println!("{}", i); }
let system = RegKey::predef(HKEY_LOCAL_MACHINE)
.open_subkey_with_flags("HARDWARE\\DESCRIPTION\\System", KEY_READ)
.unwrap();
for (name, value) in system.enum_values().map(|x| x.unwrap()) {
println!("{} = {:?}", name, value);
}
} ```
```rust extern crate winreg; use std::io; use winreg::RegKey; use winreg::enums::*; use winreg::transaction::Transaction;
fn main() { let t = Transaction::new().unwrap(); let hkcu = RegKey::predef(HKEYCURRENTUSER); let key = hkcu.createsubkeytransacted("Software\RustTransaction", &t).unwrap(); key.setvalue("TestQWORD", &1234567891011121314u64).unwrap(); key.setvalue("TestDWORD", &1234567890u32).unwrap();
println!("Commit transaction? [y/N]:");
let mut input = String::new();
io::stdin().read_line(&mut input).unwrap();
input = input.trim_right().to_owned();
if input == "y" || input == "Y" {
t.commit().unwrap();
println!("Transaction committed.");
}
else {
// this is optional, if transaction wasn't committed,
// it will be rolled back on disposal
t.rollback().unwrap();
println!("Transaction wasn't committed, it will be rolled back.");
}
} ```
```rust extern crate rustc_serialize; extern crate winreg; use winreg::enums::*;
struct Rectangle{ x: u32, y: u32, w: u32, h: u32, }
struct Test { tbool: bool, tu8: u8, tu16: u16, tu32: u32, tu64: u64, tusize: usize, tstruct: Rectangle, tstring: String, ti8: i8, ti16: i16, ti32: i32, ti64: i64, tisize: isize, tf64: f64, t_f32: f32, }
fn main() { let hkcu = winreg::RegKey::predef(HKEYCURRENTUSER); let key = hkcu.createsubkey("Software\RustEncode").unwrap(); let v1 = Test{ tbool: false, tu8: 127, tu16: 32768, tu32: 123456789, tu64: 123456789101112, tusize: 123456789101112, tstruct: Rectangle{ x: 55, y: 77, w: 500, h: 300, }, tstring: "test 123!".toowned(), ti8: -123, ti16: -2049, ti32: 20100, ti64: -12345678910, tisize: -1234567890, tf64: -0.01, t_f32: 3.14, };
key.encode(&v1).unwrap();
let v2: Test = key.decode().unwrap();
println!("Decoded {:?}", v2);
// This shows `false` because f32 and f64 encoding/decoding is NOT precise
println!("Equal to encoded: {:?}", v1 == v2);
} ```
FromRegValue
for OsString
and ToRegValue
for OsStr
(#8)copy_tree
method to RegKey
unwrap
sto_string
with to_owned
FromRegValue
trait now requires Sized
(fixes build with rust 1.4)winapi
version to fix buildstd::io::{Error,Result}
instead of own RegError
and RegResult