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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
#![cfg(windows)]
//! Windows WinAPI
//!
//! Some windows hacking library with utilities to find windows and access them.
//!

use std::default;
use std::mem;
use std::os;
use std::io;
use std::ptr;
use std::ffi;
use std::convert;

#[path="raw/mod.rs"]
mod inner_raw;
mod utils;

pub mod raw {
    //! Provides direct bindings to WinAPI functions of crate.
    pub use inner_raw::process;
    pub use inner_raw::window;
    pub use inner_raw::message;
    pub use inner_raw::file;
    pub use inner_raw::memory;
    pub use inner_raw::module;
}

use os::windows::raw::HANDLE;
use inner_raw::winapi::{
    HWND,
    UINT,
    WPARAM,
    LPARAM,
    LRESULT,
    MSG,
    c_uint
};

///Windows process representation
pub struct Process {
    pid: u32,
    inner: HANDLE,
}

impl Process {
    ///Creates handle to a new process by opening it through pid.
    ///
    ///# Note:
    ///See information about access rights:
    ///https://msdn.microsoft.com/en-us/library/windows/desktop/ms684880%28v=vs.85%29.aspx
    ///
    ///# Parameters:
    ///
    ///* ```pid``` - Pid of the process.
    ///* ```access_rights``` - Bit mask that specifies desired access rights.
    ///
    ///# Return:
    ///
    ///* ```Ok``` - Process struct.
    ///* ```Err``` - Error reason.
    pub fn open(pid: u32, access_rights: u32) -> io::Result<Process> {
        match raw::process::open(pid, access_rights) {
            Ok(handle) => Ok(Process {
                pid: pid,
                inner: handle,
            }),
            Err(error) => Err(error),
        }
    }

    ///Creates instance from existing handle
    pub fn from_raw(handle: HANDLE) -> Self {
        Process {
            pid: raw::process::get_id(handle),
            inner: handle
        }
    }

    #[inline]
    ///Retrieves underlying handle.
    pub fn inner(&self) -> HANDLE {
        self.inner
    }

    #[inline]
    ///Retrieves underlying handle and consumes self.
    ///
    ///Basically you're responsible to close handle now.
    pub fn into_inner(self) -> HANDLE {
        let result = self.inner;
        mem::forget(self);
        result
    }

    #[inline]
    ///Gets full path to process's exectuable.
    ///
    ///# Note
    ///
    /// The process MUST be opened with either PROCESS_QUERY_INFORMATION or PROCESS_QUERY_LIMITED_INFORMATION flag.
    ///
    ///# Return
    ///
    ///* ```Ok``` - Success.
    ///* ```Err``` - Error reason.
    pub fn exe_path(&self) -> io::Result<String> {
        raw::process::get_exe_path(self.inner)
    }

    #[inline]
    ///Retrieves handle to process's window
    ///
    ///# Note
    ///
    ///It can return ```None``` if process hasn't created window.
    pub fn window(&self) -> io::Result<Option<HWND>> {
        raw::window::get_by_pid(self.pid)
    }

    #[inline]
    ///Reads memory from process.
    ///
    ///# Parameters:
    ///
    ///* ```base_addr``` - Address from where to start reading.
    ///* ```storage``` - Storage to hold memory. Its `len` determines amount of bytes to read.
    pub fn read_memory(&self, base_addr: usize, storage: &mut [u8]) -> io::Result<()> {
        raw::process::read_memory(self.inner, base_addr, storage)
    }

    #[inline]
    ///Writes into process memory.
    ///
    ///# Parameters:
    ///
    ///* ```base_addr``` - Address from where to start writing.
    ///* ```data``` - Slice with write data.
    ///
    ///# Return:
    ///
    ///* ```Ok``` - Success.
    ///* ```Err``` - Error reason.
    pub fn write_memory(&self, base_addr: usize, data: &[u8]) -> io::Result<()> {
        raw::process::write_memory(self.inner, base_addr, data)
    }

    ///Closes process
    ///
    ///# Note:
    ///
    ///There is no need to explicitly close the process.
    ///
    ///It shall be closed automatically when being dropped.
    pub fn close(&mut self) {
        if !self.inner.is_null() {
            raw::process::close(self.inner).expect("Unable to close process");
            self.inner = ptr::null_mut();
        }
    }

    ///Forces termination of process and consumes itself.
    ///
    ///For details see [raw::process::terminate()](raw/process/fn.terminate.html).
    pub fn terminate(self, exit_code: c_uint) -> io::Result<()> {
        raw::process::terminate(self.inner, exit_code).map(|_| {
            let _ = self.into_inner();
        })
    }
}

impl Drop for Process {
    fn drop(&mut self) {
        self.close()
    }
}

///Wrapper over Windows messages.
///
///On drop it translates and dispatches message.
///You can do it yourself though.
pub struct Msg {
    inner: MSG
}

impl Msg {
    pub fn new(message: MSG) -> Msg {
        Msg {
            inner: message
        }
    }

    #[inline]
    ///Message identifier.
    pub fn id(&self) -> UINT {
        self.inner.message
    }

    #[inline]
    ///Pointer to inner message.
    pub fn as_ptr(&self) -> *const MSG {
        &self.inner as *const MSG
    }

    #[inline]
    ///Mutable pointer to inner message.
    pub fn as_mut_ptr(&mut self) -> *mut MSG {
        &mut self.inner as *mut MSG
    }

    #[inline]
    ///Retrieves raw Windows Message.
    ///
    ///Ownership is not passed so do not manually dispatch it.
    pub fn inner(&self) -> MSG {
        self.inner
    }

    #[inline]
    ///Retrieves raw Windows Message and transfers ownership.
    ///
    ///After that user is responsible to dispatch message.
    pub fn into_inner(self) -> MSG {
        let result = self.inner;
        mem::forget(self);
        result
    }

    #[inline]
    ///Drops and Dispatches underlying Windows Message.
    ///You cannot use it after that.
    pub fn dispatch(self) {
        drop(self);
    }
}

impl Drop for Msg {
    fn drop(&mut self) {
        raw::message::translate(self.as_mut_ptr());
        raw::message::dispatch(self.as_mut_ptr());
    }
}

///Iterator over Windows messages
///
///Under hood it uses [get()](raw/message/fn.get.html).
///
///Similarly to this function you can configure:
///
///* window - For which window to received messages.
///* range - Range of message identifiers to receive.
pub struct Messages {
    window: Option<HWND>,
    range: (Option<UINT>, Option<UINT>),
    is_block: bool
}

impl Messages {
    ///Initializes new iterator with default no filtering.
    pub fn new() -> Messages {
        Messages {
            window: None,
            range: (None, None),
            is_block: true
        }
    }

    ///Sets window for which to receive messages.
    pub fn window(&mut self, window: Option<HWND>) -> &mut Messages {
        self.window = window;
        self
    }

    ///Sets low range of message identifiers.
    pub fn low(&mut self, low: Option<UINT>) -> &mut Messages {
        self.range.0 = low;
        self
    }

    ///Sets high range of message identifiers.
    pub fn high(&mut self, high: Option<UINT>) -> &mut Messages {
        self.range.1 = high;
        self
    }

    ///Sets blocking mode.
    pub fn blocking(&mut self) -> &mut Messages {
        self.is_block = true;
        self
    }

    ///Sets non blocking mode.
    ///
    ///You can provide how to handle retrieved messages as in [peek()](raw/message/fn.peek.html).
    ///It sets `PM_REMOVE` to remove message, but not that it is not always guaranteed.
    ///See docs on `PeekMessage`
    pub fn non_blocking(&mut self) -> &mut Messages {
        self.is_block = false;
        self
    }
}

impl Iterator for Messages {
    type Item = io::Result<Msg>;

    ///Retrieves next message in queue.
    ///
    ///Blocking call.
    fn next(&mut self) -> Option<Self::Item> {
        if self.is_block {
            Some(raw::message::get(self.window, self.range.0, self.range.1).map(|msg| Msg::new(msg)))
        }
        else {
            match raw::message::peek(self.window, self.range.0, self.range.1, Some(0x0001)) {
                Ok(Some(msg)) => Some(Ok(Msg::new(msg))),
                Ok(None) => None,
                Err(error) => Some(Err(error))
            }
        }
    }
}

///Convenient wrapper over Window.
///
///Note that while you can use it with any window.
///It makes no sense in taking ownership of not created by you windows.
///
///This struct destroys window on drop and it is bad idea to do it for not your own window.
///If lucky, it fails but still not great idea.
pub struct Window {
    inner: HWND
}

impl Window {
    #[inline]
    ///Creates new instance by taking ownership over provided window.
    pub fn from_hwnd(window: HWND) -> Self {
        Window { inner: window }
    }

    #[inline]
    ///Creates window from instance of window builder.
    pub fn from_builder(builder: &mut raw::window::Builder) -> io::Result<Self> {
        builder.create().map(|win| Window::from_hwnd(win))
    }

    #[inline]
    ///Returns underlying window.
    ///
    ///Ownership is not passed.
    pub fn inner(&self) -> HWND {
        self.inner
    }

    #[inline]
    ///Transfers ownership of underlying window.
    pub fn into_inner(self) -> HWND {
        let result = self.inner;
        mem::forget(self);
        result
    }

    #[inline]
    ///Returns whether window is visible.
    pub fn is_visible(&self) -> bool {
        raw::window::is_visible(self.inner)
    }

    #[inline]
    ///Retrieves window's class.
    pub fn class(&self) -> io::Result<String> {
        raw::window::get_class(self.inner)
    }

    #[inline]
    ///Retrieves window's title.
    pub fn title(&self) -> io::Result<String> {
        raw::window::get_text(self.inner)
    }

    #[inline]
    ///Retrieves tuple of thread and process ids.
    pub fn thread_pid(&self) -> (u32, u32) {
        raw::window::get_thread_process_id(self.inner)
    }

    #[inline]
    ///Sends message to underlying window.
    ///
    ///For more information refer to [send_message()](raw/window/fn.send_message.html)
    pub fn send_message(&self, msg_type: UINT, w_param: WPARAM, l_param: LPARAM, timeout: Option<UINT>) -> io::Result<LRESULT> {
        raw::window::send_message(self.inner, msg_type, w_param, l_param, timeout)
    }

    #[inline]
    ///Sends `BM_CLICK` message to underlying window.
    ///
    ///For mores information refer to [send_push_button()](raw/window/fn.send_push_button.html)
    pub fn send_push_button(&self, timeout: Option<UINT>) -> io::Result<LRESULT> {
        raw::window::send_push_button(self.inner, timeout)
    }

    #[inline]
    ///Sends `WM_SETTEXT` message to underlying window with new text.
    ///
    ///For more information refer to [send_set_text()](raw/window/fn.send_set_text.html)
    pub fn send_set_text<T: AsRef<ffi::OsStr>>(&self, text: T) -> bool {
        raw::window::send_set_text(self.inner, text)
    }

    #[inline]
    ///Sends `WM_GETTEXT` message to underlying window and returns, if possible, corresponding text.
    ///
    ///For more information refer to [send_get_text()](raw/window/fn.send_get_text.html)
    pub fn send_get_text(&self) -> Option<String> {
        raw::window::send_get_text(self.inner)
    }

    #[inline]
    ///Sends `WM_SYSCOMMAND` message to underlying window and returns, if possible, corresponding text.
    ///
    ///For more information refer to [send_sys_command()](raw/window/fn.send_sys_command.html)
    pub fn send_sys_command(&self, cmd_type: WPARAM, l_param: LPARAM) -> bool {
        raw::window::send_sys_command(self.inner, cmd_type, l_param)
    }

    #[inline]
    ///Destroys underlying window and drops self.
    pub fn destroy(self) {
        drop(self);
    }
}

impl convert::From<HWND> for Window {
    fn from(window: HWND) -> Window {
        Window { inner: window }
    }
}

impl convert::Into<HWND> for Window {
    fn into(self) -> HWND {
        self.into_inner()
    }
}

impl Drop for Window {
    fn drop(&mut self) {
        raw::window::destroy(self.inner);
    }
}