#windows #win32 #dll #macro #ffi

windows-dll

Macro for dynamically loading windows dll functions

6 releases

0.2.4 Dec 9, 2020
0.2.3 Dec 9, 2020
0.2.0 Apr 22, 2020
0.1.1 Apr 20, 2020

#16 in Windows APIs

Download history 55/week @ 2020-10-02 79/week @ 2020-10-09 53/week @ 2020-10-16 181/week @ 2020-10-23 353/week @ 2020-10-30 147/week @ 2020-11-06 373/week @ 2020-11-13 292/week @ 2020-11-20 593/week @ 2020-11-27 664/week @ 2020-12-04 1314/week @ 2020-12-11 846/week @ 2020-12-18 584/week @ 2020-12-25 724/week @ 2021-01-01 740/week @ 2021-01-08 1144/week @ 2021-01-15

896 downloads per month

MIT license

9KB
101 lines

windows-dll

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.dll")]
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.dll")]
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.dll")]
extern "system" {
    #[link_name = "RtlGetVersion"]
    fn rtl_get_version(lp_version_information: *mut OSVERSIONINFOW) -> NTSTATUS;
}

Use a dll export without a name

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

Check whether a function exists

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

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

Pass flags to the underlying LoadLibraryExW call

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

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

Dependencies

~0.8–1.4MB
~33K SLoC