Skip to main content

Module attach

Module attach 

Source
Available on x86-64 and crate feature coreclr only.
Expand description

Attach to a CoreCLR game’s already-running runtime from a native mod loaded before the runtime starts (via LD_PRELOAD on Linux, or an injected DLL on Windows).

CoreCLR has no public “attach to the running runtime” C API: coreclr_create_delegate needs the host handle and domain id the game’s host created, and the runtime initialises only once per process. So we capture them as the game creates them — inline-hooking the loader to see coreclr arrive (it is loaded late and resolved dynamically, so symbol interposition misses it), then inline-hooking coreclr_initialize to read its output.

The loader differs per platform (dlopen / LoadLibraryExW); everything else, including the coreclr_initialize hook, is shared. Requires x86_64, where the inline-hook trampoline works. With the captured ClrHost, ClrHost::attach builds a Runtime bound to that CLR.

// In the mod's entry point, which runs before the game starts .NET.
hao_runtime::attach::on_clr_init(|host| {
    // A .NET 8 game. This runs on a background thread, so call only
    // thread-agnostic methods here.
    let runtime = host.attach(8).expect("attach to the game's CoreCLR");
    let main = runtime.method_pointer("MyMod", "MyMod.Entry", "Run");
});

Structs§

ClrHost
A handle to the game’s running CoreCLR, captured from coreclr_initialize.

Functions§

diag
Logs an early-attach diagnostic at debug level (target hao_runtime::attach), for tracing a mod’s startup.
ensure_loader_hook
Installs the loader hook (dlopen / LoadLibraryExW) so that the runtime’s library load is detected as early as possible.
on_clr_init
Runs callback once the game’s CoreCLR host has been captured. Call it from a mod’s entry point (which the loader runs before the game starts the runtime).