71 lines
2.2 KiB
Rust
71 lines
2.2 KiB
Rust
//! The module contains some Windows-specific functions for file associations.
|
|
//! These functions can not be grouped as Windows concept.
|
|
//! So they are placed in there as an independent module.
|
|
|
|
/// Check whether current process has administrative privilege.
|
|
///
|
|
/// It usually means that checking whether current process is running as Administrator.
|
|
/// Return true if it is, otherwise false.
|
|
///
|
|
/// Reference: https://learn.microsoft.com/en-us/windows/win32/api/securitybaseapi/nf-securitybaseapi-checktokenmembership
|
|
pub fn has_privilege() -> bool {
|
|
use windows_sys::Win32::Foundation::HANDLE;
|
|
use windows_sys::Win32::Security::{
|
|
AllocateAndInitializeSid, CheckTokenMembership, FreeSid, PSID, SECURITY_NT_AUTHORITY,
|
|
};
|
|
use windows_sys::Win32::System::SystemServices::{
|
|
DOMAIN_ALIAS_RID_ADMINS, SECURITY_BUILTIN_DOMAIN_RID,
|
|
};
|
|
use windows_sys::core::BOOL;
|
|
|
|
let nt_authority = SECURITY_NT_AUTHORITY.clone();
|
|
let mut administrators_group: PSID = PSID::default();
|
|
let success: BOOL = unsafe {
|
|
AllocateAndInitializeSid(
|
|
&nt_authority,
|
|
2,
|
|
SECURITY_BUILTIN_DOMAIN_RID as u32,
|
|
DOMAIN_ALIAS_RID_ADMINS as u32,
|
|
0,
|
|
0,
|
|
0,
|
|
0,
|
|
0,
|
|
0,
|
|
&mut administrators_group,
|
|
)
|
|
};
|
|
|
|
if success == 0 {
|
|
panic!("Win32 AllocateAndInitializeSid() failed");
|
|
}
|
|
|
|
let mut is_member: BOOL = BOOL::default();
|
|
let success: BOOL =
|
|
unsafe { CheckTokenMembership(HANDLE::default(), administrators_group, &mut is_member) };
|
|
|
|
unsafe {
|
|
FreeSid(administrators_group);
|
|
}
|
|
|
|
if success == 0 {
|
|
panic!("Win32 CheckTokenMembership() failed");
|
|
}
|
|
|
|
is_member != 0
|
|
}
|
|
|
|
/// Notify Windows that some file associations are changed, and should refresh them.
|
|
/// This function must be called once you change any file associations.
|
|
pub fn notify_assoc_changed() -> () {
|
|
use windows_sys::Win32::UI::Shell::{SHCNE_ASSOCCHANGED, SHCNF_IDLIST, SHChangeNotify};
|
|
unsafe {
|
|
SHChangeNotify(
|
|
SHCNE_ASSOCCHANGED as i32,
|
|
SHCNF_IDLIST,
|
|
std::ptr::null(),
|
|
std::ptr::null(),
|
|
)
|
|
}
|
|
}
|