Skip to content

A lightweight native C++ host for running .NET assemblies without modification. Supports WPF applications, console apps, and any .NET DLL with a Main method via simple JSON configuration.

License

Notifications You must be signed in to change notification settings

player-alex/net-host

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

6 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸš€ .NET Generic Host

Platform .NET C++ License

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. 🎯


✨ Features

  • πŸ”„ 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

🎯 Use Cases

  • πŸ–₯️ Host WPF applications from native C++ code
  • πŸ“š Run .NET DLLs as standalone applications
  • πŸš€ Create custom .NET application launchers
  • πŸ”— Bridge native and managed code seamlessly

πŸ› οΈ Building

Prerequisites

  • πŸ”§ Visual Studio 2022 or later
  • πŸ”· .NET 8.0 SDK - Download
  • ⚑ C++20 support

Build Steps

  1. Open net-host.sln in Visual Studio
  2. Build the solution (Release/x64 recommended)
  3. Output: net-host.exe in x64/Release or x64/Debug

πŸ“– Usage

1️⃣ Create Configuration File

Create net-host.json in the same directory as net-host.exe:

{
  "assemblyPath": "YourApp.dll",
  "typeName": "YourNamespace.Program",
  "methodName": "Main",
  "arguments": []
}

2️⃣ Prepare Your .NET Assembly

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();
        }
    }
}

3️⃣ Deploy Files

Place in the same directory:

  • net-host.exe
  • net-host.json
  • nethost.dll (from .NET SDK)
  • YourApp.dll and all its dependencies
  • YourApp.runtimeconfig.json

4️⃣ Run

net-host.exe

βš™οΈ Configuration Reference

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

Example with Arguments

{
  "assemblyPath": "MyApp.dll",
  "typeName": "MyApp.Program",
  "methodName": "Main",
  "arguments": [
    "--config", "production.json",
    "--verbose"
  ]
}

πŸ—οΈ How It Works

  1. Configuration Loading - Reads net-host.json to get assembly information
  2. hostfxr Initialization - Loads .NET hosting library (hostfxr.dll)
  3. Runtime Initialization - Initializes .NET runtime using hostfxr_initialize_for_dotnet_command_line
  4. COM/STA Setup - Initializes COM in STA mode for WPF support
  5. Application Execution - Calls hostfxr_run_app to execute the Main method
  6. Entry Assembly Setup - Automatically sets entry assembly for resource loading

🎨 WPF Applications

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.ResourceAssembly resolution
  • βœ… Supports XAML resource loading

No modifications needed to your WPF application!

πŸ› Troubleshooting

❌ Assembly Not Found
  • βœ… Ensure the DLL path in net-host.json is correct
  • βœ… Check that all dependency DLLs are in the same directory
  • βœ… Verify the assembly path is relative to net-host.exe location
⚠️ Runtime Initialization Failed
  • βœ… Verify .NET 8.0 Runtime is installed
  • βœ… Check that runtimeconfig.json exists next to the DLL
  • βœ… Ensure rollForward policy in runtimeconfig.json allows runtime version
  • βœ… Verify nethost.dll is present in the same directory
🎨 WPF Resource Loading Issues
  • βœ… Verify all resource DLLs are present
  • βœ… Check that XAML resources are embedded correctly
  • βœ… Ensure Build Action for resources is set to Resource
  • βœ… 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

πŸ”§ Technical Details

Architecture

net-host.exe (Native C++)
    ↓ hostfxr API
.NET Runtime
    ↓ load_assembly_and_get_function_pointer
YourApp.dll
    ↓ Main() execution
WPF Application / Console App

Key APIs Used

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

πŸ“¦ Example Projects

Console Application

{
  "assemblyPath": "ConsoleApp.dll",
  "typeName": "ConsoleApp.Program",
  "methodName": "Main",
  "arguments": ["--help"]
}

WPF Application

{
  "assemblyPath": "WpfApp.dll",
  "typeName": "WpfApp.App",
  "methodName": "Main",
  "arguments": []
}

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ™ Acknowledgments

πŸ“š Additional Resources


About

A lightweight native C++ host for running .NET assemblies without modification. Supports WPF applications, console apps, and any .NET DLL with a Main method via simple JSON configuration.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages