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.
- Host: GitHub
- URL: https://github.com/player-alex/net-host
- Owner: player-alex
- License: mit
- Created: 2025-10-08T07:21:56.000Z (9 months ago)
- Default Branch: main
- Last Pushed: 2025-10-10T06:20:58.000Z (8 months ago)
- Last Synced: 2026-05-03T10:46:46.564Z (about 2 months ago)
- Topics: application-launcher, assembly-loader, cpp, cpp20, csharp, dll-host, dotnet, dotnet-core, dotnet-hosting, hostfxr, interop, native-host, native-interop, windows, wpf
- Language: C++
- Homepage:
- Size: 152 KB
- Stars: 1
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ๐ .NET Generic Host




**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)
---