Skip to main content

Crate windows_result

Crate windows_result 

Source
Expand description

§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

TypeSuccess
HRESULT, NTSTATUSAny value >= 0
BOOLNonzero
WIN32_ERROR, RPC_STATUSZero

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.

Structs§

BOOL
A 32-bit Windows Boolean value.
Error
A Windows error code with optional COM error information.
HRESULT
An error code value returned by most COM functions.
NTSTATUS
An error or status code value returned by some operating system functions.
RPC_STATUS
An error or status code value returned by some operating system functions.
WIN32_ERROR
An error or status code value returned by some operating system functions.

Type Aliases§

Result
A specialized Result type that provides Windows error information.