windows-rs

repository·master·Indexed 11 days ago

https://github.com/microsoft/windows-rs

Rust bindings for the Windows API. The project includes a suite of crates for Windows development, including windows-animation for programmatic animation logic, windows-canvas for 2D drawing via Direct2D and Direct3D 11, windows-bindgen for generating API bindings, windows-composition for the retained-mode composition engine, and windows-collections for Windows-compatible iterable, vector, and map interfaces.

Tokens
111.8K
Snippets
351
Records
546
Agent score
92%

What's inside windows-rs

  1. Overview of windows-canvas

    master

    The windows-canvas crate is a safe, fast 2D graphics library for Rust that wraps the DirectX graphics stack (Direct2D, Direct3D 11, DXGI, DirectWrite, and WIC).

    Core workflow:

    1. Own a GpuDevice which manages Direct3D and Direct2D devices.
    2. Create a SwapChain from the device to present frames.
    3. Call begin_draw to obtain a DrawingSession.
    4. Use the DrawingSession to clear the surface, draw shapes, text, and bitmaps.
  2. Overview of Windows Animation

    master

    The windows-animation crate provides a Rust wrapper around the Windows Animation Manager (IUIAnimationManager). It is designed for handling variable interpolation, transitions, and storyboards.

    Crucially, this crate is decoupled from any specific UI or rendering framework, making it suitable for various application types that require programmatic animation logic.

  3. Introduction to RDL (Rust Definition Language)

    master
    RDL (Rust Definition Language) is a Rust-like source format used to define Windows APIs. The windows-rdl crate is used to parse RDL files and emit ECMA-335 .winmd metadata, which is then consumed by windows-bindgen. Additionally, the tool can perform the reverse operation by writing canonical RDL from existing .winmd files.
  4. Use windows-strings for Windows API string types

    master

    The windows-strings crate provides the specific owned and borrowed string types required by various Windows APIs. It supports:

    • HSTRING: The reference-counted UTF-16 string used throughout WinRT.
    • BSTR: The length-prefixed UTF-16 string used by COM automation.
    • PCSTR: A borrowed, null-terminated ANSI pointer.
    • PCWSTR: A borrowed, null-terminated UTF-16 pointer.

    To simplify creating these types, the crate provides compile-time macros for building null-terminated literals.

  5. Implement Windows services with windows-services

    master

    The windows-services crate provides a safe, builder-based pattern for implementing Windows services in Rust.

    To implement a service:

    1. Use Service::new to define the service's capabilities, such as whether it accepts stop (can_stop) or pause (can_pause) commands.
    2. Call run to hand control over to the Windows Service Control Manager (SCM).
    3. Provide a closure to run that receives the service handle and the incoming control commands to handle them appropriately.
  6. Use windows-collections to bridge Rust containers and WinRT APIs

    master
    The windows-collections crate provides implementations of WinRT collection interfaces (IIterable, IVector, IVectorView, IMap, and IMapView) that are backed by standard Rust containers. This allows you to pass a Rust Vec or BTreeMap directly to a Windows API that expects a WinRT collection interface.
  7. Use the windows crate for Windows API access

    master

    The windows crate is the primary umbrella crate for calling Win32, COM, and WinRT APIs from Rust. It provides broad API coverage and ergonomic wrappers compared to raw bindings.

    Choosing the right crate

    • windows: Use this for high-level, ergonomic wrappers and broad API coverage.
    • windows-sys: Use this if you require raw, low-level bindings.
    • windows-bindgen: Use this to generate a smaller, custom binding set.

    Enabling APIs via Cargo features

    APIs in the windows crate are gated by Cargo features. To use a specific API, you must enable its corresponding feature in your Cargo.toml. For example, to use COM base APIs, you would enable the combaseapi feature. The API reference on docs.rs specifies which feature is required for every individual item.

    # Example Cargo.toml configuration
    [dependencies]
    windows = {
        version = "0.73",
        features = [
            "combaseapi",
            "Foundation_Collections",
        ],
    }
  8. Use the cppwinrt crate for C++/WinRT interop

    master
    The cppwinrt crate provides a bundled version of the C++/WinRT compiler that can be invoked directly from a Rust build process. It acts as a thin wrapper around cppwinrt.exe, allowing you to run the compiler with specific arguments and capture its output. This is primarily used in interop scenarios where you need to generate C++/WinRT projection headers during your Rust build.
  9. Use windows-time for WinRT TimeSpan and DateTime

    master

    The windows-time crate provides idiomatic Rust wrappers for WinRT time primitives. It provides two main types:

    • TimeSpan: Represents a duration, stored as 100-nanosecond ticks.
    • DateTime: Represents an instant on a 1601-based UTC clock.

    Key features:

    • Both types are Copy.
    • Support for standard arithmetic and comparison operators.
    • Support for Display as ISO-8601 strings.
    • Explicit conversions to and from std::time types.
  10. Use windows-registry for safe Windows registry access

    master

    The windows-registry crate provides a safe, efficient wrapper around Win32 registry APIs.

    To use it, follow this general workflow:

    1. Select a root: Start from a predefined root such as CURRENT_USER, LOCAL_MACHINE, or CLASSES_ROOT.
    2. Access keys: Use create or open to interact with registry keys.
    3. Manage values: Read or write typed values within those keys.

    For fine-grained control, use the options() builder to configure:

    • Transactions: For atomic registry operations.
    • Volatile keys: For keys that do not persist to disk.
    • Access rights: Specific permissions for the operation.
    • Registry Redirection (WOW64):
      • wow64_32(): Targets the 32-bit (WOW6432Node) view.
      • wow64_64(): Targets the native 64-bit view.
      • Note: These views are mutually exclusive; the last call to the builder wins.

    For detailed API documentation, visit docs.rs/windows-registry.

  11. Use the windows-threading crate for safe thread pool access

    master

    The windows-threading crate provides a simple, safe interface to the Windows thread pool. It allows you to execute tasks asynchronously or in parallel without managing raw Windows threads manually.

    Key features include:

    • submit: Runs a closure on a thread from the pool.
    • for_each: Runs a closure over an iterator in parallel and waits for all tasks to complete.
    • Pool: A type providing direct control over the thread pool, including setting thread limits and performing scoped submissions.
    // Note: For specific usage patterns, refer to the Getting Started guide or Samples.
    // The crate provides: 
    // - submit()
    // - for_each()
    // - Pool type
  12. Use windows-metadata for ECMA-335 metadata operations

    master

    The windows-metadata crate is a low-level reader and writer for the ECMA-335 metadata format. This format is used by .NET, WinRT, and Win32 metadata. It serves as the foundational layer for windows-bindgen.

    To query namespaces, type definitions, and their members from .winmd files, use the reader::Index type.