How do I create a TOKEN_PRIVILEGES with more than one Privileges (LUID_AND_ATTRIBUTES).
let token_privileges = TOKEN_PRIVILEGES {
PrivilegeCount: 3,
Privileges: /* Need to create a "C" style array of 3 elements of type LUID_AND_ATTRIBUTES */
}
Do I need to create a Vec<LUID_AND_ATTRIBUTES> and somehow get a pointer to the contents? Maybe into_boxed_slice? How about for an arbitrary number of elements?
btw, you can work around the problem without creating a wrapper, e.g. you can just "emulate" C code with raw pointers in unsafe rust.
ultimately, it's no different than creating a wrapper type, it's just you do all the pointer arithemtic manually (and unsafe-ly) instead of letting the compiler do it for you.
below is a sketch using the alloc API
// the data
let privileges = todo!("initialize the privilage array");
let n = privileges.len();
// allocate memory to be used as arguments for the API
let header_layout = Layout::new::<TOKEN_PRIVILEGES>();
let extra_layout = Layout::array::<LUID_AND_ATTRIBUTES>(n-1).unwrap();
let (layout, _) = header_layout.extend(extra_layout).unwrap();
let layout = layout.pad_to_align();
let ptr = unsafe { alloc(layout) } as *mut TOKEN_PRIVILEGES;
assert!(!ptr.is_null());
// initialize the allocated struct
unsafe {
(*ptr).PrivilegeCount = n as u32;
std::ptr::copy_non_overlapping(privileges.as_ptr(), &raw mut (*ptr).Privileges, n);
}
// call the API
let result = unsafe {
AdjustTokenPrivileges(
...,
Some(ptr),
...,
)
};
// free the memory
unsafe {
dealloc(ptr, layout);
}
that is correct. for FAM structs, ffi -> rust is relatively straight forward, while rust -> ffi is complicated.
creating a FAM in rust needs to deal with allocation, pointer arithmetics, uninitialized memory, etc, but accessing a FAM in rust only needs dereferencing raw pointers.