diff options
Diffstat (limited to 'third_party/rust/winreg/examples')
-rw-r--r-- | third_party/rust/winreg/examples/basic_usage.rs | 50 | ||||
-rw-r--r-- | third_party/rust/winreg/examples/enum.rs | 25 | ||||
-rw-r--r-- | third_party/rust/winreg/examples/installed_apps.rs | 43 | ||||
-rw-r--r-- | third_party/rust/winreg/examples/serialization.rs | 81 | ||||
-rw-r--r-- | third_party/rust/winreg/examples/transactions.rs | 34 |
5 files changed, 233 insertions, 0 deletions
diff --git a/third_party/rust/winreg/examples/basic_usage.rs b/third_party/rust/winreg/examples/basic_usage.rs new file mode 100644 index 0000000000..2a7a8c02d3 --- /dev/null +++ b/third_party/rust/winreg/examples/basic_usage.rs @@ -0,0 +1,50 @@ +// Copyright 2015, Igor Shaula +// Licensed under the MIT License <LICENSE or +// http://opensource.org/licenses/MIT>. This file +// may not be copied, modified, or distributed +// except according to those terms. +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(HKEY_LOCAL_MACHINE); + let cur_ver = hklm.open_subkey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion").unwrap(); + let pf: String = cur_ver.get_value("ProgramFilesDir").unwrap(); + let dp: String = cur_ver.get_value("DevicePath").unwrap(); + println!("ProgramFiles = {}\nDevicePath = {}", pf, dp); + let info = cur_ver.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..."); + 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) + }); +} diff --git a/third_party/rust/winreg/examples/enum.rs b/third_party/rust/winreg/examples/enum.rs new file mode 100644 index 0000000000..9940cfc477 --- /dev/null +++ b/third_party/rust/winreg/examples/enum.rs @@ -0,0 +1,25 @@ +// Copyright 2015, Igor Shaula +// Licensed under the MIT License <LICENSE or +// http://opensource.org/licenses/MIT>. This file +// may not be copied, modified, or distributed +// except according to those terms. +extern crate winreg; +use winreg::RegKey; +use winreg::enums::*; + +fn main() { + println!("File extensions, registered in system:"); + for i in RegKey::predef(HKEY_CLASSES_ROOT) + .enum_keys().map(|x| x.unwrap()) + .filter(|x| x.starts_with(".")) + { + println!("{}", i); + } + + let system = RegKey::predef(HKEY_LOCAL_MACHINE) + .open_subkey("HARDWARE\\DESCRIPTION\\System") + .unwrap(); + for (name, value) in system.enum_values().map(|x| x.unwrap()) { + println!("{} = {:?}", name, value); + } +} diff --git a/third_party/rust/winreg/examples/installed_apps.rs b/third_party/rust/winreg/examples/installed_apps.rs new file mode 100644 index 0000000000..f7d555b90d --- /dev/null +++ b/third_party/rust/winreg/examples/installed_apps.rs @@ -0,0 +1,43 @@ +// Copyright 2017, Igor Shaula +// Licensed under the MIT License <LICENSE or +// http://opensource.org/licenses/MIT>. This file +// may not be copied, modified, or distributed +// except according to those terms. +#[macro_use] +extern crate serde_derive; +extern crate winreg; +use winreg::enums::*; +use std::collections::HashMap; +use std::fmt; + +#[allow(non_snake_case)] +#[derive(Debug, Serialize, Deserialize)] +struct InstalledApp { + DisplayName: Option<String>, + DisplayVersion: Option<String>, + UninstallString: Option<String> +} + +macro_rules! str_from_opt { + ($s:expr) => { $s.as_ref().map(|x| &**x).unwrap_or("") } +} + +impl fmt::Display for InstalledApp { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{}-{}", + str_from_opt!(self.DisplayName), + str_from_opt!(self.DisplayVersion)) + } +} + +fn main() { + let hklm = winreg::RegKey::predef(HKEY_LOCAL_MACHINE); + let uninstall_key = hklm.open_subkey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall") + .expect("key is missing"); + + let apps: HashMap<String, InstalledApp> = uninstall_key.decode().expect("deserialization failed"); + + for (_k, v) in &apps { + println!("{}", v); + } +} diff --git a/third_party/rust/winreg/examples/serialization.rs b/third_party/rust/winreg/examples/serialization.rs new file mode 100644 index 0000000000..bc8c5c7ee9 --- /dev/null +++ b/third_party/rust/winreg/examples/serialization.rs @@ -0,0 +1,81 @@ +// Copyright 2015, Igor Shaula +// Licensed under the MIT License <LICENSE or +// http://opensource.org/licenses/MIT>. This file +// may not be copied, modified, or distributed +// except according to those terms. +#[macro_use] +extern crate serde_derive; +extern crate winreg; +use winreg::enums::*; + +#[derive(Debug, Serialize, Deserialize, PartialEq)] +struct Coords { + x: u32, + y: u32, +} + +#[derive(Debug, Serialize, Deserialize, PartialEq)] +struct Size { + w: u32, + h: u32, +} + +#[derive(Debug, Serialize, Deserialize, PartialEq)] +struct Rectangle { + coords: Coords, + size: Size, +} + +#[derive(Debug, Serialize, Deserialize, PartialEq)] +struct Test { + t_bool: bool, + t_u8: u8, + t_u16: u16, + t_u32: u32, + t_u64: u64, + t_usize: usize, + t_struct: Rectangle, + t_string: String, + t_i8: i8, + t_i16: i16, + t_i32: i32, + t_i64: i64, + t_isize: isize, + t_f64: f64, + t_f32: f32, + // t_char: char, +} + +fn main() { + let hkcu = winreg::RegKey::predef(HKEY_CURRENT_USER); + let key = hkcu.create_subkey("Software\\RustEncode").unwrap(); + let v1 = Test{ + t_bool: false, + t_u8: 127, + t_u16: 32768, + t_u32: 123456789, + t_u64: 123456789101112, + t_usize: 1234567891, + t_struct: Rectangle{ + coords: Coords{ x: 55, y: 77 }, + size: Size{ w: 500, h: 300 }, + }, + t_string: "test 123!".to_owned(), + t_i8: -123, + t_i16: -2049, + t_i32: 20100, + t_i64: -12345678910, + t_isize: -1234567890, + t_f64: -0.01, + t_f32: 3.14, + // t_char: 'a', + }; + + 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); +} diff --git a/third_party/rust/winreg/examples/transactions.rs b/third_party/rust/winreg/examples/transactions.rs new file mode 100644 index 0000000000..9824f1b62e --- /dev/null +++ b/third_party/rust/winreg/examples/transactions.rs @@ -0,0 +1,34 @@ +// Copyright 2015, Igor Shaula +// Licensed under the MIT License <LICENSE or +// http://opensource.org/licenses/MIT>. This file +// may not be copied, modified, or distributed +// except according to those terms. +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(HKEY_CURRENT_USER); + let key = hkcu.create_subkey_transacted("Software\\RustTransaction", &t).unwrap(); + key.set_value("TestQWORD", &1234567891011121314u64).unwrap(); + key.set_value("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 commited."); + } + else { + // this is optional, if transaction wasn't commited, + // it will be rolled back on disposal + t.rollback().unwrap(); + + println!("Transaction wasn't commited, it will be rolled back."); + } +} |