CodexBloom - Programming Q&A Platform

implementing dynamically loading shared libraries in Rust using `libloading` on Windows

👀 Views: 195 đŸ’Ŧ Answers: 1 📅 Created: 2025-06-08
rust libloading windows dynamic-libraries Rust

I'm performance testing and Hey everyone, I'm running into an issue that's driving me crazy..... I'm prototyping a solution and Hey everyone, I'm running into an issue that's driving me crazy. I'm trying to dynamically load a shared library in my Rust application using the `libloading` crate, but I'm working with issues specifically on Windows. I've set up my project with the `libloading` dependency and created a simple dynamic library with the following code: ```rust // src/lib.rs #[no_mangle] pub extern "C" fn greet() { println!("Hello from the dynamic library!"); } ``` I compile this as a DLL using `cargo build --release`, and it generates `target/release/my_library.dll`. In my Rust application, I'm attempting to load this DLL and call the `greet` function: ```rust // src/main.rs use libloading::{Library, Symbol}; fn main() { let lib = Library::new("./target/release/my_library.dll").unwrap(); unsafe { let greet: Symbol<unsafe extern fn()> = lib.get(b"greet").unwrap(); greet(); } } ``` When I run the application, I get the following behavior: ``` behavior: Could not load library: The specified module could not be found. (os behavior 126) ``` I've verified the DLL exists in the correct path, and I've tried using the full path to the DLL but still get the same behavior. I've also ensured that the Rust toolchain is up to date (1.64.0). I suspect there might be an scenario with how the DLL is being compiled or how the path is being resolved. I've read that Windows can sometimes have issues with the search path for dynamic libraries. I've also checked if the `libloading` crate version is compatible with my Rust version and it is. If anyone has experience with dynamically loading libraries in Rust on Windows, I would appreciate any insights or suggestions to debug this scenario. Any help would be greatly appreciated! This is for a service running on macOS. I'm on CentOS using the latest version of Rust. Has anyone else encountered this?