Expand description
Host the .NET runtime in a Rust process and call into managed code.
Two backends are available behind features:
coreclr(default): .NET 8+, loadinglibcoreclrdirectly.mono: the Mono embedding API, as used by Unity.
Managed methods are called through native function pointers, so the
methods have to be callable from native code: [UnmanagedCallersOnly]
static methods on CoreCLR, or any static method on Mono. Bindings generated
by hao-runtime-macros produce such methods for you.
use hao_runtime::Runtime;
let runtime = Runtime::coreclr()?;
runtime.load_assembly("MyLib.dll")?;
// A static `[UnmanagedCallersOnly] int Add(int, int)` in `MyLib.Exports`.
let add = runtime.function_pointer("MyLib", "MyLib.Exports", "Add")?;
let add: extern "C" fn(i32, i32) -> i32 = unsafe { std::mem::transmute(add) };
assert_eq!(add(40, 2), 42);
// Any other static method, JIT-compiled first.
let twice = runtime.method_pointer("MyLib", "MyLib.Exports", "Twice")?;
let twice: extern "C" fn(i32) -> i32 = unsafe { std::mem::transmute(twice) };
assert_eq!(twice(21), 42);Re-exports§
pub use bindings::BoundAssembly;pub use bindings::Object;pub use hao;hao
Modules§
- attach
x86-64 and coreclr - Attach to a CoreCLR game’s already-running runtime from a native mod loaded
before the runtime starts (via
LD_PRELOADon Linux, or an injected DLL on Windows). - bindings
- Support code for bindings generated by
hao-runtime-macros. - console
- Give an injected mod somewhere to see its logs.
- jit
- CoreCLR JIT helpers — resolve precode indirections and call JIT’d methods.
- profiler
coreclr - A minimal CoreCLR profiler, implemented as COM objects by hand.
- reflect
reflect - Reflection roots for the
reflectfeature. Seehao::reflectfor the pattern. The runtime handles (Runtime,ClrHost, the backends) hold raw pointers and library handles and are not reflected; the configuration and descriptor types are. - rewrite
hao - Rewrite a managed assembly’s IL with hao before the runtime JITs it.
Structs§
- Core
ClrOptions coreclr - Where to find .NET and which framework version to run.
- Mono
Options mono - Where to find Mono.
- Runtime
- A handle to a running .NET runtime. Cheap to clone.
Enums§
- Backend
- Which .NET runtime a
Runtimeis running on, which decides how native pointers to managed methods are called. - Error
- What went wrong starting a runtime, loading assemblies or calling into them.
Functions§
- find_
dotnet_ root coreclr - The .NET install to run:
DOTNET_ROOTif set, else the first standard location with ashared/Microsoft.NETCore.Appframework folder.