#dll #winapi #macro #windows

macro windows-dll-codegen

Macro for dynamically loading windows dll functions

7 unstable releases

0.4.0 Apr 19, 2022
0.3.0 Jan 30, 2021
0.2.4 Dec 9, 2020
0.2.0 Apr 22, 2020
0.1.1 Apr 20, 2020

#70 in #dll

Download history 1256/week @ 2023-12-02 1284/week @ 2023-12-09 1155/week @ 2023-12-16 685/week @ 2023-12-23 1532/week @ 2023-12-30 1406/week @ 2024-01-06 1893/week @ 2024-01-13 3070/week @ 2024-01-20 2501/week @ 2024-01-27 3110/week @ 2024-02-03 2962/week @ 2024-02-10 2613/week @ 2024-02-17 1608/week @ 2024-02-24 1678/week @ 2024-03-02 2028/week @ 2024-03-09 1360/week @ 2024-03-16

7,085 downloads per month
Used in 8 crates (via windows-dll)

MIT license

14KB
263 lines

windows-dll

Rust

Macro for dynamically loading windows dll functions

Usage

use {
    windows_dll::dll,
    winapi::shared::{
        minwindef::ULONG,
        ntdef::{NTSTATUS, NT_SUCCESS, WCHAR},
    },
};

#[allow(non_snake_case)]
#[repr(C)]
struct OSVERSIONINFOW {
    dwOSVersionInfoSize: ULONG,
    dwMajorVersion: ULONG,
    dwMinorVersion: ULONG,
    dwBuildNumber: ULONG,
    dwPlatformId: ULONG,
    szCSDVersion: [WCHAR; 128],
}

#[dll(ntdll)]
extern "system" {
    #[allow(non_snake_case)]
    fn RtlGetVersion(lpVersionInformation: *mut OSVERSIONINFOW) -> NTSTATUS;
}


fn os_version_info() -> OSVERSIONINFOW {
    unsafe {
        let mut vi = OSVERSIONINFOW {
            dwOSVersionInfoSize: 0,
            dwMajorVersion: 0,
            dwMinorVersion: 0,
            dwBuildNumber: 0,
            dwPlatformId: 0,
            szCSDVersion: [0; 128],
        };

        let status = RtlGetVersion(&mut vi as _);

        if NT_SUCCESS(status) {
            vi
        } else {
            panic!()
        }
    }
}

Return a result to determine whether the function can be retrieved

#[dll(ntdll)]
extern "system" {
    #[allow(non_snake_case)]
    #[fallible]
    fn RtlGetVersion(lpVersionInformation: *mut OSVERSIONINFOW) -> NTSTATUS;
}

fn os_version_info() -> Result<OSVERSIONINFOW, windows_dll::Error> {
    unsafe {
        let mut vi = OSVERSIONINFOW {
            dwOSVersionInfoSize: 0,
            dwMajorVersion: 0,
            dwMinorVersion: 0,
            dwBuildNumber: 0,
            dwPlatformId: 0,
            szCSDVersion: [0; 128],
        };

        let status = RtlGetVersion(&mut vi as _)?;

        if NT_SUCCESS(status) {
            Ok(vi)
        } else {
            panic!()
        }
    }
}

Give the rust wrapper a different name to the dll export

#[dll(ntdll)]
extern "system" {
    #[link_name = "RtlGetVersion"]
    fn rtl_get_version(lp_version_information: *mut OSVERSIONINFOW) -> NTSTATUS;
}

Use a dll export without a name

#[dll(uxtheme)]
extern "system" {
    #[link_ordinal = 133]
    fn allow_dark_mode_for_window(hwnd: HWND, allow: BOOL) -> BOOL;
}

Check whether a function exists

#[dll(ntdll)]
extern "system" {
    #[link_name = "RtlGetVersion"]
    fn rtl_get_version(lp_version_information: *mut OSVERSIONINFOW) -> NTSTATUS;
}

unsafe fn rtl_get_version_exists() -> bool {
    rtl_get_version::exists()
}

Pass flags to the underlying LoadLibraryExW call

use windows_dll::*;
#[dll(ntdll, LOAD_LIBRARY_SEARCH_SYSTEM32)]
extern "system" {
    #[link_name = "RtlGetVersion"]
    fn rtl_get_version(lp_version_information: *mut OSVERSIONINFOW) -> NTSTATUS;
}

Dependencies

~3.5MB
~72K SLoC