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
use crate::FaitheError;
use std::mem::size_of;
use windows::Win32::{
    Foundation::{HANDLE, HINSTANCE},
    System::Diagnostics::ToolHelp::{
        CreateToolhelp32Snapshot, Module32FirstW, Module32NextW, MODULEENTRY32W, TH32CS_SNAPMODULE,
        TH32CS_SNAPMODULE32,
    },
};

/// Represents a single module in a running process.
#[derive(Debug, Clone)]
pub struct ModuleEntry {
    /// Id of the process.
    pub process_id: u32,
    /// Base address of the module.
    pub base_address: usize,
    /// Size of the module in bytes.
    pub size: usize,
    /// Handle to the module.
    pub handle: HINSTANCE,
    /// Name of the module.
    pub name: String,
    /// Full path to the module.
    pub path: String,
}

impl From<MODULEENTRY32W> for ModuleEntry {
    fn from(me: MODULEENTRY32W) -> Self {
        Self {
            process_id: me.th32ProcessID,
            base_address: me.modBaseAddr as _,
            size: me.modBaseSize as _,
            handle: me.hModule,
            name: String::from_utf16_lossy(
                &me.szModule[..me.szModule.iter().position(|b| *b == 0).unwrap_or(0)],
            ),
            path: String::from_utf16_lossy(
                &me.szExePath[..me.szExePath.iter().position(|b| *b == 0).unwrap_or(0)],
            ),
        }
    }
}

/// Iterator over process's loaded modules.
pub struct ModuleIterator {
    snap: HANDLE,
    entry: MODULEENTRY32W,
    ret: bool,
}

impl ModuleIterator {
    /// Creates new iterator over modules of process with id `process_id`
    pub fn new(process_id: u32) -> crate::Result<Self> {
        unsafe {
            let snap =
                CreateToolhelp32Snapshot(TH32CS_SNAPMODULE | TH32CS_SNAPMODULE32, process_id)
                    .map_err(|_| FaitheError::last_error())?;

            let entry = MODULEENTRY32W {
                dwSize: size_of::<MODULEENTRY32W>() as _,
                ..Default::default()
            };

            let mut this = Self {
                snap,
                entry,
                ret: true,
            };

            if Module32FirstW(snap, &mut this.entry) == false {
                Err(FaitheError::last_error())
            } else {
                Ok(this)
            }
        }
    }
}

impl Iterator for ModuleIterator {
    type Item = ModuleEntry;

    fn next(&mut self) -> Option<Self::Item> {
        if !self.ret {
            None
        } else {
            let this = self.entry.into();

            unsafe {
                self.ret = Module32NextW(self.snap, &mut self.entry) == true;
            }

            Some(this)
        }
    }
}