winreg/lib.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
// Copyright 2023, 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.
//! Crate for accessing MS Windows registry
//!
//!## Usage
//!
//!### Basic usage
//!
//!```toml,ignore
//!# Cargo.toml
//![dependencies]
//!winreg = "0.55"
//!```
//!
//!```no_run
//!use std::io;
//!use std::path::Path;
//!use winreg::enums::*;
//!use winreg::RegKey;
//!
//!fn main() -> io::Result<()> {
//! println!("Reading some system info...");
//! let hklm = RegKey::predef(HKEY_LOCAL_MACHINE);
//! let cur_ver = hklm.open_subkey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion")?;
//! let pf: String = cur_ver.get_value("ProgramFilesDir")?;
//! let dp: String = cur_ver.get_value("DevicePath")?;
//! println!("ProgramFiles = {}\nDevicePath = {}", pf, dp);
//! let info = cur_ver.query_info()?;
//! println!("info = {:?}", info);
//! let mt = info.get_last_write_time_system();
//! println!(
//! "last_write_time as windows_sys::Win32::Foundation::SYSTEMTIME = {}-{:02}-{:02} {:02}:{:02}:{:02}",
//! mt.wYear, mt.wMonth, mt.wDay, mt.wHour, mt.wMinute, mt.wSecond
//! );
//!
//! // enable `chrono` feature on `winreg` to make this work
//! // println!(
//! // "last_write_time as chrono::NaiveDateTime = {}",
//! // info.get_last_write_time_chrono()
//! // );
//!
//! println!("And now lets write something...");
//! let hkcu = RegKey::predef(HKEY_CURRENT_USER);
//! let path = Path::new("Software").join("WinregRsExample1");
//! let (key, disp) = hkcu.create_subkey(&path)?;
//!
//! match disp {
//! REG_CREATED_NEW_KEY => println!("A new key has been created"),
//! REG_OPENED_EXISTING_KEY => println!("An existing key has been opened"),
//! }
//!
//! key.set_value("TestSZ", &"written by Rust")?;
//! let sz_val: String = key.get_value("TestSZ")?;
//! key.delete_value("TestSZ")?;
//! println!("TestSZ = {}", sz_val);
//!
//! key.set_value("TestMultiSZ", &vec!["written", "by", "Rust"])?;
//! let multi_sz_val: Vec<String> = key.get_value("TestMultiSZ")?;
//! key.delete_value("TestMultiSZ")?;
//! println!("TestMultiSZ = {:?}", multi_sz_val);
//!
//! key.set_value("TestDWORD", &1234567890u32)?;
//! let dword_val: u32 = key.get_value("TestDWORD")?;
//! println!("TestDWORD = {}", dword_val);
//!
//! key.set_value("TestQWORD", &1234567891011121314u64)?;
//! let qword_val: u64 = key.get_value("TestQWORD")?;
//! println!("TestQWORD = {}", qword_val);
//!
//! key.create_subkey("sub\\key")?;
//! hkcu.delete_subkey_all(&path)?;
//!
//! 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),
//! });
//! Ok(())
//!}
//!```
//!
//!### Iterators
//!
//!```no_run
//!use std::io;
//!use winreg::RegKey;
//!use winreg::enums::*;
//!
//!fn main() -> io::Result<()> {
//! 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")?;
//! for (name, value) in system.enum_values().map(|x| x.unwrap()) {
//! println!("{} = {:?}", name, value);
//! }
//!
//! Ok(())
//!}
//!```
//!
cfg_if::cfg_if! {
if #[cfg(not(windows))] {
compile_error!("OS not supported. if your application is multi-platform, use `[target.'cfg(windows)'.dependencies] winreg = \"...\"`");
} else {
pub use crate::reg_key::{EnumKeys, EnumValues, RegKey, HKEY};
pub use crate::reg_key_metadata::RegKeyMetadata;
pub use crate::reg_value::RegValue;
mod common;
#[cfg(feature = "serialization-serde")]
pub mod decoder;
#[cfg(feature = "serialization-serde")]
pub mod encoder;
pub mod enums;
pub mod reg_key;
pub mod reg_key_metadata;
pub mod reg_value;
#[cfg(feature = "transactions")]
pub mod transaction;
pub mod types;
}
}