Skip to content

Instantly share code, notes, and snippets.

@kennykerr
Created February 9, 2024 18:15
Show Gist options
  • Save kennykerr/5c953c88d59257c6cbaeeb5cd5fb3f0d to your computer and use it in GitHub Desktop.
Save kennykerr/5c953c88d59257c6cbaeeb5cd5fb3f0d to your computer and use it in GitHub Desktop.
[dependencies.windows]
version = "0.52"
features = [
"Win32_Foundation",
"Win32_System_LibraryLoader",
]
use windows::{core::*, Win32::Foundation::*, Win32::System::LibraryLoader::*};
pub unsafe fn delay_load<T>(library: PCSTR, function: PCSTR) -> Option<T> {
let library = LoadLibraryExA(library, None, LOAD_LIBRARY_SEARCH_DEFAULT_DIRS);
let Ok(library) = library else {
return None;
};
let address = GetProcAddress(library, function);
if address.is_some() {
return Some(std::mem::transmute_copy(&address));
}
_ = FreeLibrary(library);
None
}
fn main() {
unsafe {
if let Some(api) = delay_load::<ShellMessageBoxW>(s!("shlwapi.dll"), s!("ShellMessageBoxW"))
{
api(0, 0, w!("text"), w!("title"), 16384);
} else {
println!("Can't find API");
}
}
}
type ShellMessageBoxW = unsafe extern "cdecl" fn(
happinst: usize,
hwnd: usize,
lpctext: PCWSTR,
lpctitle: PCWSTR,
fustyle: u32,
...
) -> i32;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment