use std::io;
use windows::Win32::Foundation::{BOOL, HANDLE, LUID};
use windows::Win32::Security::{
LookupPrivilegeValueW, PrivilegeCheck, LUID_AND_ATTRIBUTES, PRIVILEGE_SET, SE_CREATE_PERMANENT_NAME,
TOKEN_PRIVILEGES_ATTRIBUTES, TOKEN_QUERY,
};
use windows::Win32::System::SystemServices::PRIVILEGE_SET_ALL_NECESSARY;
use windows::Win32::System::Threading::{GetCurrentProcess, OpenProcessToken};
pub fn can_create_permanent_shared_objects() -> io::Result<bool> {
let process_handle = unsafe { GetCurrentProcess() };
let mut token_handle = HANDLE::default();
unsafe { OpenProcessToken(process_handle, TOKEN_QUERY, &mut token_handle) }?;
let mut privilege_luid = LUID::default();
unsafe { LookupPrivilegeValueW(None, SE_CREATE_PERMANENT_NAME, &mut privilege_luid) }?;
let mut privilege_set = PRIVILEGE_SET {
PrivilegeCount: 1,
Control: PRIVILEGE_SET_ALL_NECESSARY,
Privilege: [LUID_AND_ATTRIBUTES {
Luid: privilege_luid,
Attributes: TOKEN_PRIVILEGES_ATTRIBUTES::default(),
}],
};
let mut privilege_enabled = BOOL::default();
unsafe { PrivilegeCheck(token_handle, &mut privilege_set, &mut privilege_enabled) }?;
Ok(privilege_enabled.into())
}