Calling "C" FFI with variable sized structure

⚓ Rust    📅 2025-07-17    👤 surdeus    👁️ 2      

surdeus

windows-rs has the following struct

#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct TOKEN_PRIVILEGES {
    pub PrivilegeCount: u32,
    pub Privileges: [LUID_AND_ATTRIBUTES; 1]
}

I want to call the following function with newstate having three privileges

pub unsafe fn AdjustTokenPrivileges(tokenhandle: HANDLE, disableallprivileges: bool, newstate: Option<*const TOKEN_PRIVILEGES>, bufferlength: u32, previousstate: Option<*mut TOKEN_PRIVILEGES>, returnlength: Option<*mut u32>) -> windows_core::Result<()>

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?

7 posts - 2 participants

Read full topic

🏷️ rust_feed