Windows-rpc
Windows RPC client and server library for Rust.
This crate, together with windows_rpc_macros, provides
a way to define Windows RPC interfaces using Rust traits and automatically generate all the
necessary client and server code. The generated code handles NDR (Network Data Representation)
marshalling, format strings, and Windows RPC runtime integration.
Features
- Simple trait-based interface definition - Define RPC interfaces using familiar Rust syntax
- Automatic code generation - Client and server code generated at compile time
- Type safety - Full Rust type system integration
- NDR marshalling - Automatic Network Data Representation encoding/decoding
- String support - Native handling of string parameters and return values
- Integer types - Support for i8, i16, i32, i64, u8, u16, u32, u64
- ALPC protocol - Fast local RPC using Advanced Local Procedure Call
Quick Start
Define an RPC interface as a trait with the [rpc_interface] macro:
use rpc_interface;
use ;
This generates three types:
CalculatorClient- for making RPC callsCalculatorServerImpl- trait to implement for the serverCalculatorServer<T>- generic server wrapper for RPC dispatch
Server Example
Implement the generated ServerImpl trait with static methods:
use rpc_interface;
;
Client Example
Make RPC calls using the generated client:
use rpc_interface;
use ;
Complete Example with String Operations
Here's a more comprehensive example showcasing various string operations:
use ;
;
Supported Types
The following types can be used for parameters and return values:
| Rust Type | Parameters | Return Values | Notes |
|---|---|---|---|
i8, u8 |
✓ | ✓ | 8-bit integers |
i16, u16 |
✓ | ✓ | 16-bit integers |
i32, u32 |
✓ | ✓ | 32-bit integers |
i64, u64 |
✓ | ✓ | 64-bit integers |
&str |
✓ | ✗ | String input parameters |
String |
✗ | ✓ | String return values |
Protocol Support
Currently only ALPC (Advanced Local Procedure Call) is supported via the ncalrpc
protocol sequence. This allows RPC communication between processes on the same machine.
What This Library Does
- Generates all MIDL stub metadata (
MIDL_STUB_DESC,MIDL_SERVER_INFO, etc.) - Handles NDR 2.0 and NDR64 format strings for type marshalling
- Manages RPC binding handles and server lifecycle
- Converts between Rust types and Windows ABI types
- Provides clean async (non-blocking) and sync (blocking) server modes
Limitations
This library is currently limited in scope:
- Protocol: Only local RPC (ALPC/ncalrpc) is supported. TCP, UDP, and named pipes are not yet implemented.
- Parameter direction: Only input (
[in]) parameters and return values ([out]) are supported. Input-output parameters are not available. - Types: Only primitive integers and strings are supported. No pointers, structs, arrays, unions, or other complex types.
- Security: No interface security (authentication, authorization, encryption) is implemented.
- Exceptions: SEH exceptions from the RPC runtime are not caught or handled.
- Callbacks: RPC callbacks from server to client are not supported.
Interoperability
The generated code produces standard Windows RPC interfaces that are compatible with MIDL-generated C/C++ clients and servers. You can use a Rust server with a C++ client (or vice versa) as long as the interface GUID, version, and method signatures match.
Safety
This crate uses unsafe code extensively to interact with the Windows RPC runtime.
The generated client and server code manages memory carefully to ensure:
- RPC metadata structures remain valid for the lifetime of the client/server
- String conversions between Rust and Windows types are handled correctly
- Memory allocated by the server for return values is properly managed
- Static trait methods are called correctly via monomorphization
However, bugs in this crate could lead to memory corruption or undefined behavior.
Implementation Details
The server implementation uses:
- Generic server structs:
{Interface}Server<T>is generic over the implementation type - Static trait methods: Server trait methods don't take
&self, making implementations stateless - Monomorphization: Each instantiation of
Server<ConcreteType>generates type-specific wrapper functions - Extern "C" wrappers: Generated wrapper functions bridge the RPC runtime to Rust static methods