Skip to content

Commit

Permalink
Add delay load samples (#2839)
Browse files Browse the repository at this point in the history
  • Loading branch information
kennykerr authored Feb 10, 2024
1 parent 0df3676 commit 40f671b
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .github/workflows/clippy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ jobs:
cargo clippy -p sample_credentials &&
cargo clippy -p sample_data_protection &&
cargo clippy -p sample_dcomp &&
cargo clippy -p sample_delay_load &&
cargo clippy -p sample_delay_load_sys &&
cargo clippy -p sample_device_watcher &&
cargo clippy -p sample_direct2d &&
cargo clippy -p sample_direct3d12 &&
Expand Down
4 changes: 3 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ jobs:
cargo test -p sample_credentials &&
cargo test -p sample_data_protection &&
cargo test -p sample_dcomp &&
cargo test -p sample_delay_load &&
cargo test -p sample_delay_load_sys &&
cargo test -p sample_device_watcher &&
cargo test -p sample_direct2d &&
cargo test -p sample_direct3d12 &&
Expand Down Expand Up @@ -103,8 +105,8 @@ jobs:
cargo test -p test_debugger_visualizer &&
cargo test -p test_deprecated &&
cargo test -p test_dispatch &&
cargo test -p test_does_not_return &&
cargo clean &&
cargo test -p test_does_not_return &&
cargo test -p test_enums &&
cargo test -p test_error &&
cargo test -p test_event &&
Expand Down
11 changes: 11 additions & 0 deletions crates/samples/windows-sys/delay_load/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "sample_delay_load_sys"
version = "0.0.0"
edition = "2021"
publish = false

[dependencies.windows-sys]
path = "../../../libs/sys"
features = [
"Win32_System_LibraryLoader",
]
41 changes: 41 additions & 0 deletions crates/samples/windows-sys/delay_load/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use windows_sys::{core::*, Win32::Foundation::*, Win32::System::LibraryLoader::*};

/// # Safety
///
/// The `PCSTR` parameters need to be valid for reads up until and including the next `\0`.
pub unsafe fn delay_load<T>(library: PCSTR, function: PCSTR) -> Option<T> {
let library = LoadLibraryExA(library, 0, LOAD_LIBRARY_SEARCH_DEFAULT_DIRS);

if library == 0 {
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!("Message"), w!("Sample"), 1);
} else {
println!("Can't find API");
}
}
}

type ShellMessageBoxW = unsafe extern "cdecl" fn(
happinst: usize,
hwnd: usize,
lpctext: PCWSTR,
lpctitle: PCWSTR,
fustyle: u32,
...
) -> i32;
11 changes: 11 additions & 0 deletions crates/samples/windows/delay_load/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "sample_delay_load"
version = "0.0.0"
edition = "2021"
publish = false

[dependencies.windows]
path = "../../../libs/windows"
features = [
"Win32_System_LibraryLoader",
]
41 changes: 41 additions & 0 deletions crates/samples/windows/delay_load/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use windows::{core::*, Win32::Foundation::*, Win32::System::LibraryLoader::*};

/// # Safety
///
/// The `PCSTR` parameters need to be valid for reads up until and including the next `\0`.
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!("Message"), w!("Sample"), 1);
} else {
println!("Can't find API");
}
}
}

type ShellMessageBoxW = unsafe extern "cdecl" fn(
happinst: usize,
hwnd: usize,
lpctext: PCWSTR,
lpctitle: PCWSTR,
fustyle: u32,
...
) -> i32;

0 comments on commit 40f671b

Please sign in to comment.