#ffi #dlopen #load #shared #lazy


Run-time dynamic linking loader utilities

4 releases (2 breaking)

new 0.9.1 Nov 18, 2023
0.8.0 Oct 30, 2023
0.7.0 Jul 12, 2023

#4 in #load

Download history 1/week @ 2023-07-29 22/week @ 2023-08-05 11/week @ 2023-08-12 11/week @ 2023-08-26 21/week @ 2023-09-02 2/week @ 2023-09-09 2/week @ 2023-09-16 2/week @ 2023-09-23 2/week @ 2023-09-30 3/week @ 2023-10-07 1/week @ 2023-10-14 1/week @ 2023-10-21 41/week @ 2023-10-28 2/week @ 2023-11-04 28/week @ 2023-11-11

73 downloads per month


881 lines


Crates.io Crates.io Crates.io docs.rs dylink-rs

Dylink provides a run-time dynamic linking framework for loading dynamic libraries.

This crate may be useful if the dynamic library you are loading is not always guarenteed to exist, which may enable you to provide fallbacks in your code.

Related links:


  • Thread-safe library loading.
  • Macro attribute

Supported platforms

Implemented for all major platforms.

Windows Linux MacOS


Add this to your Cargo.toml

dylink = "0.9"


Below is an example of opening a library manually through Library on Windows.

use dylink::*;
use std::mem;

// Open the Kernel32.dll library.
let lib = Library::open("Kernel32.dll").expect("Failed to open library");

// Get the symbol for the GetLastError function.
let sym = lib.symbol("GetLastError").unwrap();

// Cast the symbol to the appropriate function signature.
let get_last_error: unsafe extern "system" fn() -> u32 = unsafe {mem::transmute(sym.cast::<()>())};

let result = unsafe {get_last_error()};

// Call the function and assert its return value.
assert_eq!(result, 0);

Below is an example on how to use the dylink attribute on Windows. This example demonstrates the lazy loading capability of the dylink crate by interacting with functions from the Kernel32.dll library.

use dylink::*;

// Define a static LibLock for the Kernel32.dll library.
static KERNEL32: sync::LibLock = sync::LibLock::new(&["Kernel32.dll"]);

// Use the `dylink` attribute to declare functions from the Kernel32.dll.
extern "system-unwind" {
    fn GetLastError() -> u32;
    fn SetLastError(_: u32);

// Use the declared functions, which will be loaded lazily when called.
unsafe {
   assert_eq!(52, GetLastError());


Licensed under either of

at your option.


Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.


~19K SLoC