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
use crate::utilities::buf_from_os;
use crate::{LocalBox, Sid};
use std::ffi::OsStr;
use std::io;
use std::ptr::{null_mut, NonNull};
/// Wraps [`ConvertStringSidToSidW`](https://docs.microsoft.com/en-us/windows/win32/api/sddl/nf-sddl-convertstringsidtosidw).
///
/// ```
/// use windows_permissions::{Sid, wrappers::ConvertStringSidToSid};
///
/// let string_sid = "S-1-5-123-456-789";
/// let sid = ConvertStringSidToSid(string_sid).unwrap();
/// assert_eq!(string_sid, &sid.to_string());
/// ```
///
/// # Panics
///
/// Panics if the underlying WinAPI call reports success but returns a null
/// pointer. This should never happen.
#[allow(non_snake_case)]
pub fn ConvertStringSidToSid<S: AsRef<OsStr> + ?Sized>(string: &S) -> io::Result<LocalBox<Sid>> {
let buf = buf_from_os(string);
let mut ptr = null_mut();
let result = unsafe { winapi::shared::sddl::ConvertStringSidToSidW(buf.as_ptr(), &mut ptr) };
if result != 0 {
// Success
Ok(unsafe {
LocalBox::from_raw(
NonNull::new(ptr as *mut _)
.expect("ConvertStringSidToSidW reported success but returned null"),
)
})
} else {
// Failure
Err(io::Error::last_os_error())
}
}