9个版本

0.4.1 2022年6月23日
0.4.0 2022年4月19日
0.3.0 2021年1月30日
0.2.4 2020年12月9日
0.1.1 2020年4月20日

Windows API 中排名第 66

Download history 1641/week @ 2024-03-13 1948/week @ 2024-03-20 1518/week @ 2024-03-27 1491/week @ 2024-04-03 2325/week @ 2024-04-10 1831/week @ 2024-04-17 2475/week @ 2024-04-24 1203/week @ 2024-05-01 485/week @ 2024-05-08 497/week @ 2024-05-15 694/week @ 2024-05-22 629/week @ 2024-05-29 891/week @ 2024-06-05 562/week @ 2024-06-12 759/week @ 2024-06-19 551/week @ 2024-06-26

每月下载量 2,902

MIT 许可证

20KB
405 行代码

windows-dll

Rust

动态加载Windows DLL函数的宏

使用方法

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!()
        }
    }
}

返回一个结果以确定是否可以检索函数

#[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!()
        }
    }
}

给Rust包装器一个与DLL导出不同的名称

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

使用不带名称的DLL导出

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

检查函数是否存在

#[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()
}

将标志传递给底层的LoadLibraryExW调用

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

依赖关系

~3–41MB
~602K SLoC