libobs_wrapper\utils/
initialization.rs

1//! This is derived from the frontend/obs-main.cpp.
2
3use windows::{
4    core::PCWSTR,
5    Win32::{Foundation::{CloseHandle, HANDLE, LUID}, Security::{
6        AdjustTokenPrivileges, LookupPrivilegeValueW,
7        SE_DEBUG_NAME, SE_INC_BASE_PRIORITY_NAME, SE_PRIVILEGE_ENABLED, TOKEN_ADJUST_PRIVILEGES,
8        TOKEN_PRIVILEGES, TOKEN_QUERY,
9    }, System::Threading::{GetCurrentProcess, OpenProcessToken}},
10};
11
12pub fn load_debug_privilege() {
13    unsafe {
14        let flags = TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY;
15        let mut tp = TOKEN_PRIVILEGES::default();
16        let mut token = HANDLE::default();
17        let mut val = LUID::default();
18
19        if OpenProcessToken(GetCurrentProcess(), flags, &mut token).is_err() {
20            return;
21        }
22
23        if LookupPrivilegeValueW(PCWSTR::null(), SE_DEBUG_NAME, &mut val).is_ok() {
24            tp.PrivilegeCount = 1;
25            tp.Privileges[0].Luid = val;
26            tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
27
28            let res = AdjustTokenPrivileges(
29                token,
30                false,
31                Some(&tp),
32                std::mem::size_of::<TOKEN_PRIVILEGES>() as u32,
33                None,
34                None,
35            );
36            if let Err(e) = res {
37                // Use a logging mechanism compatible with your Rust application
38                eprintln!("Could not set privilege to debug process: {e:?}");
39            }
40        }
41
42        if LookupPrivilegeValueW(PCWSTR::null(), SE_INC_BASE_PRIORITY_NAME, &mut val).is_ok() {
43            tp.PrivilegeCount = 1;
44            tp.Privileges[0].Luid = val;
45            tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
46
47            let res = AdjustTokenPrivileges(
48                token,
49                false,
50                Some(&tp),
51                std::mem::size_of::<TOKEN_PRIVILEGES>() as u32,
52                None,
53                None,
54            );
55
56            if let Err(e) = res {
57                // Use a logging mechanism compatible with your Rust application
58                eprintln!("Could not set privilege to increase GPU priority {e:?}");
59            }
60        }
61
62        let _ = CloseHandle(token);
63    }
64}