A lightweight native C++ host for running .NET assemblies without modification. Perfect for hosting WPF applications, console apps, or any .NET DLL with a Main method. π―
- π Zero Assembly Modification - Run any .NET DLL without changing source code
- π¨ WPF Support - Full support for WPF applications with proper resource loading
- π§΅ STA Threading - Automatic COM initialization for UI applications
- βοΈ JSON Configuration - Simple configuration via
net-host.json - π¦ Entry Assembly Support - Properly sets entry assembly for resource loading
- π¬ Command-line Arguments - Pass arguments to the hosted application
- π₯οΈ Host WPF applications from native C++ code
- π Run .NET DLLs as standalone applications
- π Create custom .NET application launchers
- π Bridge native and managed code seamlessly
- π§ Visual Studio 2022 or later
- π· .NET 8.0 SDK - Download
- β‘ C++20 support
- Open
net-host.slnin Visual Studio - Build the solution (Release/x64 recommended)
- Output:
net-host.exeinx64/Releaseorx64/Debug
Create net-host.json in the same directory as net-host.exe:
{
"assemblyPath": "YourApp.dll",
"typeName": "YourNamespace.Program",
"methodName": "Main",
"arguments": []
}Ensure your .NET application has a public static void Main() or public static void Main(string[] args) method:
namespace YourNamespace
{
public class Program
{
public static void Main(string[] args)
{
// Your application code
var app = new App();
app.Run();
}
}
}Place in the same directory:
net-host.exenet-host.jsonnethost.dll(from .NET SDK)YourApp.dlland all its dependenciesYourApp.runtimeconfig.json
net-host.exe| Field | Type | Required | Description |
|---|---|---|---|
assemblyPath |
string | β Yes | Path to the .NET DLL (relative or absolute) |
typeName |
string | β Yes | Fully qualified type name (e.g., Namespace.Class) |
methodName |
string | β No | Entry method name (default: Main) |
arguments |
array | β No | Command-line arguments to pass to Main |
{
"assemblyPath": "MyApp.dll",
"typeName": "MyApp.Program",
"methodName": "Main",
"arguments": [
"--config", "production.json",
"--verbose"
]
}- Configuration Loading - Reads
net-host.jsonto get assembly information - hostfxr Initialization - Loads .NET hosting library (
hostfxr.dll) - Runtime Initialization - Initializes .NET runtime using
hostfxr_initialize_for_dotnet_command_line - COM/STA Setup - Initializes COM in STA mode for WPF support
- Application Execution - Calls
hostfxr_run_appto execute the Main method - Entry Assembly Setup - Automatically sets entry assembly for resource loading
For WPF applications, the host automatically:
- β Initializes COM in STA (Single-Threaded Apartment) mode
- β
Sets up entry assembly for
pack://URI resource loading - β
Handles
Application.ResourceAssemblyresolution - β Supports XAML resource loading
No modifications needed to your WPF application!
β Assembly Not Found
- β
Ensure the DLL path in
net-host.jsonis correct - β Check that all dependency DLLs are in the same directory
- β
Verify the assembly path is relative to
net-host.exelocation
β οΈ Runtime Initialization Failed
- β Verify .NET 8.0 Runtime is installed
- β
Check that
runtimeconfig.jsonexists next to the DLL - β
Ensure
rollForwardpolicy inruntimeconfig.jsonallows runtime version - β
Verify
nethost.dllis present in the same directory
π¨ WPF Resource Loading Issues
- β Verify all resource DLLs are present
- β Check that XAML resources are embedded correctly
- β
Ensure
Build Actionfor resources is set toResource - β Verify pack URIs are correct in your XAML files
π§ COM Initialization Failed
- β Close other applications that may conflict
- β Run as administrator if necessary
- β Check if COM is already initialized in a different mode
net-host.exe (Native C++)
β hostfxr API
.NET Runtime
β load_assembly_and_get_function_pointer
YourApp.dll
β Main() execution
WPF Application / Console App
| API | Purpose |
|---|---|
hostfxr_initialize_for_dotnet_command_line |
Simulates dotnet YourApp.dll |
hostfxr_run_app |
Executes the application's Main method |
CoInitializeEx |
Initializes COM for UI threading |
{
"assemblyPath": "ConsoleApp.dll",
"typeName": "ConsoleApp.Program",
"methodName": "Main",
"arguments": ["--help"]
}{
"assemblyPath": "WpfApp.dll",
"typeName": "WpfApp.App",
"methodName": "Main",
"arguments": []
}This project is licensed under the MIT License - see the LICENSE file for details.
- .NET Hosting APIs - Built with .NET Hosting APIs
- nlohmann/json - JSON for Modern C++