#smart-pointers #winapi #handle #api #raw #windows #generate

windows_safe_handle

Generate smart pointers for https://crates.io/crates/windows raw handles with ergonomic APIs

4 releases

0.2.0 Jul 17, 2024
0.1.2 Sep 2, 2023
0.1.1 Sep 2, 2023
0.1.0 Sep 2, 2023

#82 in Windows APIs

Download history 1/week @ 2024-07-23 2/week @ 2024-07-30 5/week @ 2024-09-10 2/week @ 2024-09-17 15/week @ 2024-09-24 7/week @ 2024-10-01

156 downloads per month

MIT/Apache

8KB
76 lines

windows_safe_handle

Generate smart pointers for windows raw handles with ergonomic APIs.

safe_handle!

This crate doesn't offer pre-defined smart pointers. Instead, it provides a single safe_handle! macro for generation:

Simple Smart Pointer, calling an unsafe Function on Drop

use windows_safe_handle::safe_handle;
use windows::Win32::Foundation::{HANDLE, CloseHandle};

safe_handle!(pub Handle(HANDLE), CloseHandle);

If you do not need to export the Handle type, simply omit the pub keyword.

Smart Pointer with additional Drop logic

You can use a closure-based syntax:

use windows_safe_handle::safe_handle;
use windows::Win32::Foundation::{HANDLE, CloseHandle};

safe_handle!(pub Handle(HANDLE), |h| {
    // Place your code here
    unsafe { CloseHandle(h) }
});

Note that in this case you have to explicitly use unsafe block.

Versions

  • v0.1.*: Compatible with windows v0.48.0.
  • v0.2.*: Compatible with windows v0.58.0.

Example

Refer to tests/bcrypt_hash.rs to see how to safely wrap Windows Cryptography Next Generation (CNG) APIs for calculating MD5 hashes.

Dependencies

~127MB
~2M SLoC