#dll #winapi #macro #windows

windows-dll-codegen

动态加载Windows DLL函数的宏

7 个不稳定版本

0.4.0 2022年4月19日
0.3.0 2021年1月30日
0.2.4 2020年12月9日
0.2.0 2020年4月22日
0.1.1 2020年4月20日

#40 in #win32

Download history 2244/week @ 2024-04-14 2125/week @ 2024-04-21 1914/week @ 2024-04-28 563/week @ 2024-05-05 513/week @ 2024-05-12 708/week @ 2024-05-19 535/week @ 2024-05-26 953/week @ 2024-06-02 601/week @ 2024-06-09 722/week @ 2024-06-16 581/week @ 2024-06-23 807/week @ 2024-06-30 529/week @ 2024-07-07 425/week @ 2024-07-14 486/week @ 2024-07-21 345/week @ 2024-07-28

1,816 下载/月
windows-dll 中使用

MIT 许可证

14KB
263

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.5MB
~75K SLoC