feat: wait for the thread loading us to unload

nicer log messages
This commit is contained in:
beerpiss 2024-06-26 23:41:04 +07:00
parent 6b624dc264
commit 275b18ae4f
3 changed files with 45 additions and 6 deletions

View File

@ -1,18 +1,51 @@
use std::ptr;
use snafu::prelude::Snafu; use snafu::prelude::Snafu;
use widestring::U16CString; use widestring::U16CString;
use winapi::{ use winapi::{
ctypes::c_void, ctypes::c_void,
shared::{ shared::{
minwindef::{HINSTANCE, HMODULE, TRUE}, minwindef::{FALSE, HINSTANCE, HMODULE, TRUE}, ntdef::HANDLE, winerror::ERROR_INSUFFICIENT_BUFFER
winerror::ERROR_INSUFFICIENT_BUFFER,
}, },
um::{ um::{
errhandlingapi::GetLastError, errhandlingapi::GetLastError, handleapi::{CloseHandle, DuplicateHandle}, libloaderapi::{FreeLibraryAndExitThread, GetModuleFileNameW}, processthreadsapi::{GetCurrentProcess, GetCurrentThread}, synchapi::WaitForSingleObject, winhttp::{WinHttpQueryOption, HINTERNET}, winnt::SYNCHRONIZE
libloaderapi::{FreeLibraryAndExitThread, GetModuleFileNameW},
winhttp::{WinHttpQueryOption, HINTERNET},
}, },
}; };
pub struct ThreadHandle(HANDLE);
unsafe impl Send for ThreadHandle {}
unsafe impl Sync for ThreadHandle {}
impl ThreadHandle {
pub fn duplicate_thread_handle() -> Result<Self, u32> {
unsafe {
let mut cur_thread = ptr::null_mut();
let result = DuplicateHandle(
GetCurrentProcess(),
GetCurrentThread(),
GetCurrentProcess(),
&mut cur_thread,
SYNCHRONIZE,
FALSE,
0,
);
if result == 0 {
return Err(GetLastError());
}
Ok(ThreadHandle(cur_thread as HANDLE))
}
}
pub fn wait_and_close(self, ms: u32) {
unsafe {
WaitForSingleObject(self.0, ms);
CloseHandle(self.0);
}
}
}
pub struct LibraryHandle(HINSTANCE); pub struct LibraryHandle(HINSTANCE);
unsafe impl Send for LibraryHandle {} unsafe impl Send for LibraryHandle {}

View File

@ -9,6 +9,7 @@ mod updater;
use std::thread; use std::thread;
use helpers::winapi_ext::ThreadHandle;
use log::{error, info, warn}; use log::{error, info, warn};
use winapi::{ use winapi::{
shared::minwindef::{BOOL, DWORD, HINSTANCE, LPVOID, TRUE}, shared::minwindef::{BOOL, DWORD, HINSTANCE, LPVOID, TRUE},
@ -30,8 +31,13 @@ extern "system" fn DllMain(dll_module: HINSTANCE, call_reason: DWORD, reserved:
init_logger(); init_logger();
let library_handle = unsafe { LibraryHandle::new(dll_module) }; let library_handle = unsafe { LibraryHandle::new(dll_module) };
let thread_handle = ThreadHandle::duplicate_thread_handle();
thread::spawn(move || { thread::spawn(move || {
if let Ok(h) = thread_handle {
h.wait_and_close(1000);
}
info!( info!(
"saekawa {} ({}@{}) starting up...", "saekawa {} ({}@{}) starting up...",
env!("CARGO_PKG_VERSION"), env!("CARGO_PKG_VERSION"),

View File

@ -354,7 +354,7 @@ fn setup_network_encryption(info: &GameInformation) -> Result<(), HookError> {
.context(CryptoScanSnafu)? .context(CryptoScanSnafu)?
}; };
info!("Search completed successfully."); debug!("Search completed successfully.");
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
{ {