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 
main() -> io::Result<()>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("TestDWORD", &1234567890u32)?;
47     let dword_val: u32 = key.get_value("TestDWORD")?;
48     println!("TestDWORD = {}", dword_val);
49 
50     key.set_value("TestQWORD", &1234567891011121314u64)?;
51     let qword_val: u64 = key.get_value("TestQWORD")?;
52     println!("TestQWORD = {}", qword_val);
53 
54     key.create_subkey("sub\\key")?;
55     hkcu.delete_subkey_all(&path)?;
56 
57     println!("Trying to open nonexistent key...");
58     hkcu.open_subkey(&path).unwrap_or_else(|e| match e.kind() {
59         io::ErrorKind::NotFound => panic!("Key doesn't exist"),
60         io::ErrorKind::PermissionDenied => panic!("Access denied"),
61         _ => panic!("{:?}", e),
62     });
63     Ok(())
64 }
65