summaryrefslogtreecommitdiff
path: root/crates/windows-kernel-rs/src/symbolic_link.rs
blob: 8c30f6bbbf30292aa5cc30fae5d230f2c9811d5b (plain) (blame)
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
use widestring::U16CString;
use windows_kernel_sys::ntoskrnl::{IoCreateSymbolicLink, IoDeleteSymbolicLink};

use crate::{
  error::{Error, IntoResult},
  string::create_unicode_string,
};

pub struct SymbolicLink {
  name: U16CString,
}

impl SymbolicLink {
  pub fn new(name: &str, target: &str) -> Result<Self, Error> {
    // Convert the name to UTF-16 and then create a UNICODE_STRING.
    let name = U16CString::from_str(name).unwrap();
    let mut name_ptr = create_unicode_string(name.as_slice());

    // Convert the target to UTF-16 and then create a UNICODE_STRING.
    let target = U16CString::from_str(target).unwrap();
    let mut target_ptr = create_unicode_string(target.as_slice());

    unsafe { IoCreateSymbolicLink(&mut name_ptr, &mut target_ptr) }.into_result()?;

    Ok(Self {
      name,
    })
  }
}

impl Drop for SymbolicLink {
  fn drop(&mut self) {
    let mut name_ptr = create_unicode_string(self.name.as_slice());

    unsafe {
      IoDeleteSymbolicLink(&mut name_ptr);
    }
  }
}