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
| 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.
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.