windows-result 0.101.0

Windows error handling
Documentation

windows-result

Windows status types, errors, and Result for Win32, COM, and WinRT APIs. Projected APIs often re-export these types; use this crate directly for low-level error handling.

Get started

[dependencies]
windows-result = "0.101"
use windows_result::*;

const ERROR_CANCELLED: WIN32_ERROR = WIN32_ERROR(1223);

fn main() -> Result<()> {
    HRESULT(0).ok()?;
    let error = Error::new(ERROR_CANCELLED.to_hresult(), "operation cancelled");
    assert_eq!(error.code(), ERROR_CANCELLED.to_hresult());
    assert_eq!(error.message(), "operation cancelled");
    Ok(())
}

Check the API's success convention

Type Success
HRESULT, NTSTATUS Any value >= 0
BOOL Nonzero
WIN32_ERROR, RPC_STATUS Zero

Use the convention documented for the API, then propagate failures with .ok()?. HRESULT(1) is success, and not every zero returned by a Win32 function means failure.

For last-error APIs, call BOOL::ok or Error::from_thread immediately after failure. Another Windows call can overwrite the thread's error.

Error implements core::error::Error; the default std feature adds I/O conversions.