From 64a84b5611e5efbd74f049188191ba0e46e74566 Mon Sep 17 00:00:00 2001 From: Kenny Kerr Date: Fri, 9 Feb 2024 17:22:31 -0600 Subject: [PATCH] samples --- .github/workflows/clippy.yml | 2 + .github/workflows/test.yml | 4 +- .../samples/windows-sys/delay_load/Cargo.toml | 11 +++++ .../windows-sys/delay_load/src/main.rs | 41 +++++++++++++++++++ crates/samples/windows/delay_load/Cargo.toml | 11 +++++ crates/samples/windows/delay_load/src/main.rs | 41 +++++++++++++++++++ 6 files changed, 109 insertions(+), 1 deletion(-) create mode 100644 crates/samples/windows-sys/delay_load/Cargo.toml create mode 100644 crates/samples/windows-sys/delay_load/src/main.rs create mode 100644 crates/samples/windows/delay_load/Cargo.toml create mode 100644 crates/samples/windows/delay_load/src/main.rs diff --git a/.github/workflows/clippy.yml b/.github/workflows/clippy.yml index 452dcea300..8231028c9b 100644 --- a/.github/workflows/clippy.yml +++ b/.github/workflows/clippy.yml @@ -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 && diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 6917c7ba2e..0d22a6f9c7 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -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 && @@ -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 && diff --git a/crates/samples/windows-sys/delay_load/Cargo.toml b/crates/samples/windows-sys/delay_load/Cargo.toml new file mode 100644 index 0000000000..df6b1462ff --- /dev/null +++ b/crates/samples/windows-sys/delay_load/Cargo.toml @@ -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", +] diff --git a/crates/samples/windows-sys/delay_load/src/main.rs b/crates/samples/windows-sys/delay_load/src/main.rs new file mode 100644 index 0000000000..a047d31754 --- /dev/null +++ b/crates/samples/windows-sys/delay_load/src/main.rs @@ -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(library: PCSTR, function: PCSTR) -> Option { + 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::(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; diff --git a/crates/samples/windows/delay_load/Cargo.toml b/crates/samples/windows/delay_load/Cargo.toml new file mode 100644 index 0000000000..5566a95d2f --- /dev/null +++ b/crates/samples/windows/delay_load/Cargo.toml @@ -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", +] diff --git a/crates/samples/windows/delay_load/src/main.rs b/crates/samples/windows/delay_load/src/main.rs new file mode 100644 index 0000000000..5a8c9aeb26 --- /dev/null +++ b/crates/samples/windows/delay_load/src/main.rs @@ -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(library: PCSTR, function: PCSTR) -> Option { + 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::(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;