### Building and Installing MinHook using vcpkg Source: https://github.com/veeenu/hudhook/blob/main/vendor/minhook/README.md These commands demonstrate the steps required to download, bootstrap, integrate, and install the MinHook library using the vcpkg command-line tool on Windows. ```Shell git clone https://github.com/microsoft/vcpkg ``` ```Shell .\vcpkg\bootstrap-vcpkg.bat ``` ```Shell .\vcpkg\vcpkg integrate install ``` ```Shell .\vcpkg\vcpkg install minhook ``` -------------------------------- ### Defining State Structure for HUD (Rust) Source: https://github.com/veeenu/hudhook/blob/main/hudbook/src/creating-library/writing-dll.md Defines the `HelloHud` struct to hold the application state, specifically the injection start time. Includes a constructor `new` that initializes the `start_time` to the current moment using `Instant::now()`. ```Rust use std::time::Instant; struct HelloHud { start_time: Instant, } impl HelloHud { fn new() -> Self { Self { start_time: Instant::now() } } } ``` -------------------------------- ### Initialize Rust Library and Add Dependencies (Cargo) Source: https://github.com/veeenu/hudhook/blob/main/hudbook/src/creating-library/project-setup.md Initializes a new Rust library project named `hello-hud`, changes the directory into the new project, and adds `hudhook` (version 0.5) and `imgui` (version 0.11) as dependencies using `cargo add`. ```Shell cargo init --lib hello-hud cd hello-hud cargo add hudhook@0.5 imgui@0.11 ``` -------------------------------- ### Building the Rust Library (Shell) Source: https://github.com/veeenu/hudhook/blob/main/hudbook/src/creating-library/entry-point.md This command builds the Rust project in release mode. It compiles the source code, including the defined DLL entry point, into a release-optimized dynamic link library (`.dll` file) located in the `target/release/` directory. ```Shell cargo build --release ``` -------------------------------- ### Download and Build DirectX 12 Sample (PowerShell) Source: https://github.com/veeenu/hudhook/blob/main/hudbook/src/injecting-library/injecting.md This script downloads the DirectX 12 Hello World samples, extracts them, navigates to the HelloTexture sample directory, and builds the project using msbuild for the x64 platform. ```powershell Invoke-WebRequest ` https://github.com/microsoft/DirectX-Graphics-Samples/releases/download/MicrosoftDocs-Samples/d3d12-hello-world-samples-win32.zip ` -OutFile d3d12-samples.zip Expand-Archive -Path d3d12-samples.zip d3d12-samples cd d3d12-samples\src\HelloTeture msbuild -p:Platform=x64 ``` -------------------------------- ### Define DLL Entry Point (Rust) Source: https://github.com/veeenu/hudhook/blob/main/hudbook/src/advanced/proxy-dll.md Implement the standard `DllMain` function, which serves as the entry point for the DLL. Use the `DLL_PROCESS_ATTACH` event to trigger the initialization of the lazy static state, ensuring the original DLL is loaded and the function pointer is obtained when the proxy DLL is attached to the process. ```Rust #[no_mangle] #[allow(non_snake_case, unused_variables)] extern "system" fn DllMain(dll_module: HINSTANCE, call_reason: u32, reserved: *mut c_void) -> BOOL { match call_reason { DLL_PROCESS_ATTACH => LazyLock::force(&STATE), DLL_PROCESS_DETACH => (), _ => (), } BOOL::from(true) } ``` -------------------------------- ### Configure Rust Project as DLL (Cargo.toml) Source: https://github.com/veeenu/hudhook/blob/main/hudbook/src/creating-library/project-setup.md Configures the Rust project to build as a Windows dynamically-linked library (`cdylib`) and a standard Rust library (`rlib`) by adding the `crate-type` field under the `[lib]` section in the `Cargo.toml` file. It also sets the library name. ```TOML [lib] crate-type = ["cdylib", "rlib"] name = "hello_hud" ``` -------------------------------- ### Defining DLL Entry Point with hudhook! Macro (Rust) Source: https://github.com/veeenu/hudhook/blob/main/hudbook/src/creating-library/entry-point.md This snippet shows how to define the DLL entry point using the `hudhook!` macro. It specifies `ImguiDx12Hooks` as the target hook type and uses an instance of `HelloHud` (which implements `ImguiRenderLoop`) to handle rendering. This is the simplest method provided by `hudhook`. ```Rust use hudhook::hooks::dx12::ImguiDX12Hooks; hudhook::hudhook!(ImguiDx12Hooks, HelloHud::new()); ``` -------------------------------- ### Implementing Custom DllMain Entry Point (Rust) Source: https://github.com/veeenu/hudhook/blob/main/hudbook/src/creating-library/entry-point.md This snippet provides a custom implementation of the `DllMain` function, which serves as the DLL entry point on Windows. It uses the `Hudhook::builder` to configure and apply multiple hooks or perform custom logic during `DLL_PROCESS_ATTACH`. It spawns a new thread to apply the hooks asynchronously and handles potential errors. ```Rust use hudhook::tracing::*; use hudhook::*; #[no_mangle] pub unsafe extern "stdcall" fn DllMain( hmodule: HINSTANCE, reason: u32, _: *mut std::ffi::c_void, ) { if reason == DLL_PROCESS_ATTACH { trace!("DllMain()"); std::thread::spawn(move || { if let Err(e) = Hudhook::builder() .with::(HelloHud::new()) .with_hmodule(hmodule) .build() .apply() { error!("Couldn't apply hooks: {e:?}"); eject(); } }); } } ``` -------------------------------- ### Define DLL Exports (DEF file) Source: https://github.com/veeenu/hudhook/blob/main/hudbook/src/advanced/proxy-dll.md Create a .def file (e.g., exports.def) to explicitly list the functions that should be exported from the proxy DLL. This is necessary to match the exports of the original DLL being proxied. ```Text EXPORTS DirectInput8Create ``` -------------------------------- ### Configure Rust Crate as CDYLIB Source: https://github.com/veeenu/hudhook/blob/main/hudbook/src/advanced/proxy-dll.md Configure the Cargo.toml file to build the Rust crate as a C-compatible dynamic library (cdylib) and set its output name to match the target DLL (e.g., dinput8). ```TOML [lib] name = "dinput8" crate-type = ["cdylib"] ``` -------------------------------- ### Build Rust Proxy DLL Source: https://github.com/veeenu/hudhook/blob/main/hudbook/src/advanced/proxy-dll.md Use the Cargo build command with the `--release` flag to compile the Rust project. This will produce the optimized proxy DLL file in the `target/release/` directory. ```Shell cargo build --release ``` -------------------------------- ### Implementing Imgui Rendering Logic (Rust) Source: https://github.com/veeenu/hudhook/blob/main/hudbook/src/creating-library/writing-dll.md Implements the `ImguiRenderLoop` trait for the `HelloHud` struct, providing the `render` method. This method is called by `hudhook` each frame to draw the UI, displaying a window with 'Hello, world!' and the elapsed time since injection using `imgui::Ui`. ```Rust use hudhook::hooks::ImguiRenderLoop; use imgui::*; impl ImguiRenderLoop for HelloHud { fn render(&mut self, ui: &mut Ui) { ui.window("##hello") .size([320., 200.], Condition::Always) .build(|| { ui.text("Hello, world!"); ui.text(format!("Elapsed: {:?}", self.start_time.elapsed())); }); } } ``` -------------------------------- ### Add Binary Target to Cargo.toml (TOML) Source: https://github.com/veeenu/hudhook/blob/main/hudbook/src/injecting-library/injecting.md This TOML snippet adds a new binary target named "hello_injector" to the project's `Cargo.toml` file, specifying its entry point as `src/main.rs`. This allows compiling the injector code as a separate executable. ```toml [[bin]] name = "hello_injector" path = "src/main.rs" ``` -------------------------------- ### Injecting Hook DLL into Target Process using hudhook (Rust) Source: https://github.com/veeenu/hudhook/blob/main/README.md Shows how to use `hudhook::inject::Process` to find a target application by name and inject a compiled hook DLL into it. Requires the target process to be running and the hook DLL to be built. ```Rust // src/main.rs use hudhook::inject::Process; fn main() { let mut cur_exe = std::env::current_exe().unwrap(); cur_exe.push(".."); cur_exe.push("libmyhook.dll"); let cur_dll = cur_exe.canonicalize().unwrap(); Process::by_name("MyTargetApplication.exe").unwrap().inject(cur_dll).unwrap(); } ``` -------------------------------- ### Initialize Original Function Pointer (Rust) Source: https://github.com/veeenu/hudhook/blob/main/hudbook/src/advanced/proxy-dll.md Define the signature of the original function, create a structure to hold its pointer, and use a LazyLock to safely and lazily load the original DLL from System32 and obtain the address of the function. ```Rust // We define our exported function's signature as a type. type FDirectInput8Create = unsafe extern "stdcall" fn( hinst: HINSTANCE, dwversion: u32, riidltf: *const GUID, ppvout: *mut *mut c_void, punkouter: HINSTANCE, ) -> HRESULT; // We create a structure to hold a pointer to the original function. struct State { directinput8create: FDirectInput8Create, } // These impls are safe because the pointer to the function will be constant // across the entire execution. unsafe impl Send for State {} unsafe impl Sync for State {} // We lazily initialize and statically store our `State` structure. The first // time this is invoked, it will load the actual `dinput8.dll` and get the // pointer to the `DirectInput8Create` function inside of it. static STATE: LazyLock = LazyLock::new(|| unsafe { let dinput8 = LoadLibraryA(PCSTR(b"C:\\Windows\\System32\\dinput8.dll\0".as_ptr())).unwrap(); let directinput8create = std::mem::transmute(GetProcAddress(dinput8, PCSTR(b"DirectInput8Create\0".as_ptr()))); println!("Called!"); State { directinput8create } }); ``` -------------------------------- ### Link DEF file in Rust Build Script Source: https://github.com/veeenu/hudhook/blob/main/hudbook/src/advanced/proxy-dll.md Use a build.rs script to instruct Cargo to pass the .def file to the linker. This ensures that the specified functions are correctly exported from the compiled DLL. ```Rust fn main() { println!("cargo:rustc-cdylib-link-arg=/DEF:exports.def"); } ``` -------------------------------- ### Defining Custom ImGui Render Loop and Hooking with hudhook (Rust) Source: https://github.com/veeenu/hudhook/blob/main/README.md Defines a struct implementing `ImguiRenderLoop` to handle ImGui rendering logic. Demonstrates using the `hudhook!` macro to integrate this render loop with specific graphics API hooks (DirectX 9, 11, 12, OpenGL 3). ```Rust // src/lib.rs use hudhook::*; pub struct MyRenderLoop; impl ImguiRenderLoop for MyRenderLoop { fn render(&mut self, ui: &mut imgui::Ui) { ui.window("My first render loop") .position([0., 0.], imgui::Condition::FirstUseEver) .size([320., 200.], imgui::Condition::FirstUseEver) .build(|| { ui.text("Hello, hello!"); }); } } { // Use this if hooking into a DirectX 9 application. use hudhook::hooks::dx9::ImguiDx9Hooks; hudhook!(ImguiDx9Hooks, MyRenderLoop); } { // Use this if hooking into a DirectX 11 application. use hudhook::hooks::dx11::ImguiDx11Hooks; hudhook!(ImguiDx11Hooks, MyRenderLoop); } { // Use this if hooking into a DirectX 12 application. use hudhook::hooks::dx12::ImguiDx12Hooks; hudhook!(ImguiDx12Hooks, MyRenderLoop); } { // Use this if hooking into an OpenGL 3 application. use hudhook::hooks::opengl3::ImguiOpenGl3Hooks; hudhook!(ImguiOpenGl3Hooks, MyRenderLoop); } ``` -------------------------------- ### Inject DLL by Window Title (Rust) Source: https://github.com/veeenu/hudhook/blob/main/hudbook/src/injecting-library/injecting.md This Rust code uses the `hudhook::inject::Process::by_title` method to find the target process by its window title ("D3D12 Hello Texture") and then injects the specified DLL ("hello_hud.dll") into it. Error handling is done via `.unwrap()`. ```rust use hudhook::inject::Process; fn main() { Process::by_title("D3D12 Hello Texture").unwrap().inject("hello_hud.dll".into()).unwrap(); } ``` -------------------------------- ### Implement Proxied Export Function (Rust) Source: https://github.com/veeenu/hudhook/blob/main/hudbook/src/advanced/proxy-dll.md Implement the function that matches the exported signature. This function should include the custom logic (e.g., calling `patch()`) and then call the original function using the stored pointer, passing along all parameters and returning the original result. ```Rust #[no_mangle] unsafe extern "stdcall" fn DirectInput8Create( hinst: HINSTANCE, dwversion: u32, riidltf: *const GUID, ppvout: *mut *mut c_void, punkouter: HINSTANCE, ) -> HRESULT { patch(); // Perform our custom logic, like setup hudhook or whatever. (STATE.directinput8create)(hinst, dwversion, riidltf, ppvout, punkouter) } ``` -------------------------------- ### Inject DLL by Process Name (Rust) Source: https://github.com/veeenu/hudhook/blob/main/hudbook/src/injecting-library/injecting.md This Rust code uses the `hudhook::inject::Process::by_name` method to find the target process by its executable name ("D3D12HelloTexture.exe") and then injects the specified DLL ("hello_hud.dll") into it. Error handling is done via `.unwrap()`. ```rust use hudhook::inject::Process; fn main() { Process::by_name("D3D12HelloTexture.exe").unwrap().inject("hello_hud.dll".into()).unwrap(); } ``` -------------------------------- ### Add Rust Cross-Compilation Targets (Linux) Source: https://github.com/veeenu/hudhook/blob/main/CONTRIBUTING.md Adds the necessary `x86_64-pc-windows-gnu` and `i686-pc-windows-gnu` targets using `rustup` for cross-compiling Rust code to Windows from Linux. This is required for projects interacting with Windows-specific APIs like DirectX. ```Shell rustup target add x86_64-pc-windows-gnu rustup target add i686-pc-windows-gnu ``` -------------------------------- ### Set WINEPATH for Mingw DLLs (Linux) Source: https://github.com/veeenu/hudhook/blob/main/CONTRIBUTING.md Sets the `WINEPATH` environment variable to include the directory containing Mingw DLLs (e.g., `/usr/x86_64-w64-mingw32/bin`). This is necessary for Wine to find the required libraries when running Windows executables compiled with Mingw, enabling testing on Linux. ```Shell export WINEPATH=/usr/x86_64-w64-mingw32/bin ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.