[wip] use windows crate to run as admin

This commit is contained in:
2025-03-20 20:57:32 +01:00
parent 393f8b5fab
commit 78f7ff2405
4 changed files with 117 additions and 16 deletions

View File

@ -34,3 +34,6 @@ tauri-plugin-shell = { workspace = true }
tauri-plugin-dialog = { workspace = true }
tauri-plugin-store = { workspace = true }
tokio = { workspace = true }
[target.'cfg(windows)'.dependencies]
windows = { workspace = true }

View File

@ -1,3 +1,5 @@
#[cfg(target_os = "windows")]
use std::os::windows::ffi::OsStrExt;
use std::{
collections::HashSet,
fs::File,
@ -54,12 +56,31 @@ fn install_game(id: String, state: tauri::State<LanSpreadState>) -> bool {
true
}
#[cfg(target_os = "windows")]
fn run_as_admin(file: &str, params: &str, dir: &str) -> bool {
todo!();
let file_wide: Vec<u16> = OsStr::new(file).encode_wide().chain(Some(0)).collect();
let params_wide: Vec<u16> = OsStr::new(params).encode_wide().chain(Some(0)).collect();
let dir_wide: Vec<u16> = OsStr::new(dir).encode_wide().chain(Some(0)).collect();
let result = unsafe {
ShellExecuteW(
HWND(0),
Some("runas"),
&file_wide,
Some(&params_wide),
Some(&dir_wide),
windows::Win32::UI::WindowsAndMessaging::SW_SHOWNORMAL,
)
};
result.0 > 32 // Success if greater than 32
}
#[tauri::command]
fn run_game(id: String, state: tauri::State<LanSpreadState>) {
const FIRST_START_DONE_FILE: &str = ".softlan_first_start_done";
log::error!("run_game {id}");
let games_folder =
tauri::async_runtime::block_on(async { state.inner().games_folder.lock().await.clone() });