feat(launcher): warn if the launcher is not run as admin
This commit is contained in:
@@ -4,16 +4,17 @@ use std::ffi::CString;
|
|||||||
|
|
||||||
use std::ptr::null_mut;
|
use std::ptr::null_mut;
|
||||||
use windows::Win32::Foundation::{CloseHandle, GetLastError, HANDLE};
|
use windows::Win32::Foundation::{CloseHandle, GetLastError, HANDLE};
|
||||||
|
use windows::Win32::Security::{GetTokenInformation, TOKEN_ELEVATION, TOKEN_QUERY, TokenElevation};
|
||||||
use windows::Win32::System::Diagnostics::Debug::WriteProcessMemory;
|
use windows::Win32::System::Diagnostics::Debug::WriteProcessMemory;
|
||||||
use windows::Win32::System::LibraryLoader::{GetModuleHandleA, GetProcAddress};
|
use windows::Win32::System::LibraryLoader::{GetModuleHandleA, GetProcAddress};
|
||||||
use windows::Win32::System::Memory::{
|
use windows::Win32::System::Memory::{
|
||||||
MEM_COMMIT, MEM_RELEASE, MEM_RESERVE, PAGE_READWRITE, VirtualAllocEx, VirtualFreeEx,
|
MEM_COMMIT, MEM_RELEASE, MEM_RESERVE, PAGE_READWRITE, VirtualAllocEx, VirtualFreeEx,
|
||||||
};
|
};
|
||||||
use windows::Win32::System::Threading::{
|
use windows::Win32::System::Threading::{
|
||||||
CREATE_SUSPENDED, CreateProcessA, CreateRemoteThread, PROCESS_INFORMATION, ResumeThread,
|
CREATE_SUSPENDED, CreateProcessA, CreateRemoteThread, OpenProcessToken, PROCESS_INFORMATION,
|
||||||
STARTUPINFOA, WaitForSingleObject,
|
ResumeThread, STARTUPINFOA, WaitForSingleObject,
|
||||||
};
|
};
|
||||||
use windows::core::{PSTR, s};
|
use windows::core::{Error, PSTR, s};
|
||||||
|
|
||||||
fn inject_standard(h_target: HANDLE, dll_path: &str) -> bool {
|
fn inject_standard(h_target: HANDLE, dll_path: &str) -> bool {
|
||||||
unsafe {
|
unsafe {
|
||||||
@@ -70,7 +71,48 @@ fn inject_standard(h_target: HANDLE, dll_path: &str) -> bool {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn is_running_as_admin() -> Result<bool, Error> {
|
||||||
|
unsafe {
|
||||||
|
let mut token_handle = HANDLE::default();
|
||||||
|
let current_process = windows::Win32::System::Threading::GetCurrentProcess();
|
||||||
|
|
||||||
|
if OpenProcessToken(current_process, TOKEN_QUERY, &mut token_handle).is_err() {
|
||||||
|
return Err(windows::core::Error::from_win32());
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut elevation = TOKEN_ELEVATION::default();
|
||||||
|
let mut size = std::mem::size_of::<TOKEN_ELEVATION>() as u32;
|
||||||
|
|
||||||
|
let success = GetTokenInformation(
|
||||||
|
token_handle,
|
||||||
|
TokenElevation,
|
||||||
|
Some(&mut elevation as *mut _ as *mut _),
|
||||||
|
size,
|
||||||
|
&mut size,
|
||||||
|
);
|
||||||
|
|
||||||
|
let _ = CloseHandle(token_handle);
|
||||||
|
|
||||||
|
if success.is_ok() {
|
||||||
|
Ok(elevation.TokenIsElevated != 0)
|
||||||
|
} else {
|
||||||
|
Err(windows::core::Error::from_win32())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn wait_exit() {
|
||||||
|
println!("Press any key to exit...");
|
||||||
|
let mut input = String::new();
|
||||||
|
std::io::stdin().read_line(&mut input).unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
if !is_running_as_admin().unwrap_or_default() {
|
||||||
|
println!("launcher need to be launched as admin");
|
||||||
|
wait_exit();
|
||||||
|
}
|
||||||
|
|
||||||
let current_dir = std::env::current_dir().unwrap();
|
let current_dir = std::env::current_dir().unwrap();
|
||||||
let dll_path = current_dir.join("hkrpg.dll");
|
let dll_path = current_dir.join("hkrpg.dll");
|
||||||
if !dll_path.is_file() {
|
if !dll_path.is_file() {
|
||||||
|
|||||||
Reference in New Issue
Block a user