| 1 |
// Copyright 2015, Igor Shaula |
| 2 |
// Licensed under the MIT License <LICENSE or |
| 3 |
// http://opensource.org/licenses/MIT>. This file |
| 4 |
// may not be copied, modified, or distributed |
| 5 |
// except according to those terms. |
| 6 |
extern crate winreg; |
| 7 |
use std::io; |
| 8 |
use std::path::Path; |
| 9 |
use winreg::enums::*; |
| 10 |
use winreg::RegKey; |
| 11 |
|
| 12 |
fn main() -> io::Result<()> { |
| 13 |
println!("Reading some system info..."); |
| 14 |
let hklm = RegKey::predef(HKEY_LOCAL_MACHINE); |
| 15 |
let cur_ver = hklm.open_subkey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion")?; |
| 16 |
let pf: String = cur_ver.get_value("ProgramFilesDir")?; |
| 17 |
let dp: String = cur_ver.get_value("DevicePath")?; |
| 18 |
println!("ProgramFiles = {}\nDevicePath = {}", pf, dp); |
| 19 |
let info = cur_ver.query_info()?; |
| 20 |
println!("info = {:?}", info); |
| 21 |
let mt = info.get_last_write_time_system(); |
| 22 |
println!( |
| 23 |
"last_write_time as winapi::um::minwinbase::SYSTEMTIME = {}-{:02}-{:02} {:02}:{:02}:{:02}", |
| 24 |
mt.wYear, mt.wMonth, mt.wDay, mt.wHour, mt.wMinute, mt.wSecond |
| 25 |
); |
| 26 |
println!( |
| 27 |
"last_write_time as chrono::NaiveDateTime = {}", |
| 28 |
info.get_last_write_time_chrono() |
| 29 |
); |
| 30 |
|
| 31 |
println!("And now lets write something..."); |
| 32 |
let hkcu = RegKey::predef(HKEY_CURRENT_USER); |
| 33 |
let path = Path::new("Software").join("WinregRsExample1"); |
| 34 |
let (key, disp) = hkcu.create_subkey(&path)?; |
| 35 |
|
| 36 |
match disp { |
| 37 |
REG_CREATED_NEW_KEY => println!("A new key has been created"), |
| 38 |
REG_OPENED_EXISTING_KEY => println!("An existing key has been opened"), |
| 39 |
} |
| 40 |
|
| 41 |
key.set_value("TestSZ", &"written by Rust")?; |
| 42 |
let sz_val: String = key.get_value("TestSZ")?; |
| 43 |
key.delete_value("TestSZ")?; |
| 44 |
println!("TestSZ = {}", sz_val); |
| 45 |
|
| 46 |
key.set_value("TestMultiSZ", &vec!["written", "by", "Rust"])?; |
| 47 |
let multi_sz_val: Vec<String> = key.get_value("TestMultiSZ")?; |
| 48 |
key.delete_value("TestMultiSZ")?; |
| 49 |
println!("TestMultiSZ = {:?}", multi_sz_val); |
| 50 |
|
| 51 |
key.set_value("TestDWORD", &1_234_567_890u32)?; |
| 52 |
let dword_val: u32 = key.get_value("TestDWORD")?; |
| 53 |
println!("TestDWORD = {}", dword_val); |
| 54 |
|
| 55 |
key.set_value("TestQWORD", &1_234_567_891_011_121_314u64)?; |
| 56 |
let qword_val: u64 = key.get_value("TestQWORD")?; |
| 57 |
println!("TestQWORD = {}", qword_val); |
| 58 |
|
| 59 |
key.create_subkey("sub\\key")?; |
| 60 |
hkcu.delete_subkey_all(&path)?; |
| 61 |
|
| 62 |
println!("Trying to open nonexistent key..."); |
| 63 |
hkcu.open_subkey(&path).unwrap_or_else(|e| match e.kind() { |
| 64 |
io::ErrorKind::NotFound => panic!("Key doesn't exist"), |
| 65 |
io::ErrorKind::PermissionDenied => panic!("Access denied"), |
| 66 |
_ => panic!("{:?}", e), |
| 67 |
}); |
| 68 |
Ok(()) |
| 69 |
} |