use std::ffi::CString;
use windows::Win32::Foundation::{FARPROC, HMODULE};
use windows::Win32::System::LibraryLoader::{GetModuleHandleA, GetProcAddress};
use windows::core::PCSTR;
pub fn get_module_from_name(module_name: &str) -> Result<HMODULE, String> {
let c_module_name = CString::new(module_name)
.map_err(|e| format!("Failed to create CString for module name: {}", e))?;
let module = unsafe { GetModuleHandleA(PCSTR::from_raw(c_module_name.as_ptr() as *const u8)) }
.map_err(|e| format!("Failed to load library {}: {}", module_name, e))?;
if module.is_invalid() {
return Err(format!("Failed to get module: {}", module_name));
}
Ok(module)
}
pub fn resolve_function_ptr_from_name(
module: HMODULE,
name: &str,
) -> Result<Option<FARPROC>, String> {
if module.is_invalid() {
return Err(format!("Invalid module handle"));
}
let c_name =
CString::new(name).map_err(|e| format!("Failed to create CString for name: {}", e))?;
let proc_address = unsafe {
GetProcAddress(module, PCSTR::from_raw(c_name.as_ptr() as *const u8))
};
Ok(Some(proc_address))
}