An open API service indexing awesome lists of open source software.

https://github.com/player-alex/net-host

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.
https://github.com/player-alex/net-host

application-launcher assembly-loader cpp cpp20 csharp dll-host dotnet dotnet-core dotnet-hosting hostfxr interop native-host native-interop windows wpf

Last synced: about 2 months ago
JSON representation

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.

Awesome Lists containing this project

README

          

# ๐Ÿš€ .NET Generic Host

![Platform](https://img.shields.io/badge/platform-Windows-0078D6?logo=windows&logoColor=white)
![.NET](https://img.shields.io/badge/.NET-8.0-512BD4?logo=.net&logoColor=white)
![C++](https://img.shields.io/badge/C++-20-00599C?logo=cplusplus&logoColor=white)
![License](https://img.shields.io/badge/license-MIT-blue.svg)

**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](https://dotnet.microsoft.com/download/dotnet/8.0)
- โšก **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`:

```json
{
"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:

```csharp
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

```bash
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

```json
{
"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

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

### WPF Application

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

## ๐Ÿ“„ License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

## ๐Ÿ™ Acknowledgments

- [.NET Hosting APIs](https://learn.microsoft.com/en-us/dotnet/core/tutorials/netcore-hosting) - Built with .NET Hosting APIs
- [nlohmann/json](https://github.com/nlohmann/json) - JSON for Modern C++

## ๐Ÿ“š Additional Resources

- [hostfxr API Reference](https://github.com/dotnet/runtime/blob/main/src/native/corehost/hostfxr.h)
- [Write a custom .NET runtime host](https://learn.microsoft.com/en-us/dotnet/core/tutorials/netcore-hosting)

---