summaryrefslogtreecommitdiffstats
path: root/third_party/rust/winreg/examples/installed_apps.rs
blob: a7546847db779a1387641ef4c51589e1bb914a67 (plain)
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
// 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 std::collections::HashMap;
use std::fmt;
use winreg::enums::*;

#[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 v in apps.values() {
        println!("{}", v);
    }
}