.= 'tag.htm'; break; case 'flag': $pre .= $default_pre .= 'flag.htm'; break; case 'my': $pre .= $default_pre .= 'my.htm'; break; case 'my_password': $pre .= $default_pre .= 'my_password.htm'; break; case 'my_bind': $pre .= $default_pre .= 'my_bind.htm'; break; case 'my_avatar': $pre .= $default_pre .= 'my_avatar.htm'; break; case 'home_article': $pre .= $default_pre .= 'home_article.htm'; break; case 'home_comment': $pre .= $default_pre .= 'home_comment.htm'; break; case 'user': $pre .= $default_pre .= 'user.htm'; break; case 'user_login': $pre .= $default_pre .= 'user_login.htm'; break; case 'user_create': $pre .= $default_pre .= 'user_create.htm'; break; case 'user_resetpw': $pre .= $default_pre .= 'user_resetpw.htm'; break; case 'user_resetpw_complete': $pre .= $default_pre .= 'user_resetpw_complete.htm'; break; case 'user_comment': $pre .= $default_pre .= 'user_comment.htm'; break; case 'single_page': $pre .= $default_pre .= 'single_page.htm'; break; case 'search': $pre .= $default_pre .= 'search.htm'; break; case 'operate_sticky': $pre .= $default_pre .= 'operate_sticky.htm'; break; case 'operate_close': $pre .= $default_pre .= 'operate_close.htm'; break; case 'operate_delete': $pre .= $default_pre .= 'operate_delete.htm'; break; case 'operate_move': $pre .= $default_pre .= 'operate_move.htm'; break; case '404': $pre .= $default_pre .= '404.htm'; break; case 'read_404': $pre .= $default_pre .= 'read_404.htm'; break; case 'list_404': $pre .= $default_pre .= 'list_404.htm'; break; default: $pre .= $default_pre .= theme_mode_pre(); break; } if ($config['theme']) { $conffile = APP_PATH . 'view/template/' . $config['theme'] . '/conf.json'; $json = is_file($conffile) ? xn_json_decode(file_get_contents($conffile)) : array(); } !empty($json['installed']) and $path_file = APP_PATH . 'view/template/' . $config['theme'] . '/htm/' . ($id ? $id . '_' : '') . $pre; (empty($path_file) || !is_file($path_file)) and $path_file = APP_PATH . 'view/template/' . $config['theme'] . '/htm/' . $pre; if (!empty($config['theme_child']) && is_array($config['theme_child'])) { foreach ($config['theme_child'] as $theme) { if (empty($theme) || is_array($theme)) continue; $path_file = APP_PATH . 'view/template/' . $theme . '/htm/' . ($id ? $id . '_' : '') . $pre; !is_file($path_file) and $path_file = APP_PATH . 'view/template/' . $theme . '/htm/' . $pre; } } !is_file($path_file) and $path_file = APP_PATH . ($dir ? 'plugin/' . $dir . '/view/htm/' : 'view/htm/') . $default_pre; return $path_file; } function theme_mode_pre($type = 0) { global $config; $mode = $config['setting']['website_mode']; $pre = ''; if (1 == $mode) { $pre .= 2 == $type ? 'portal_category.htm' : 'portal.htm'; } elseif (2 == $mode) { $pre .= 2 == $type ? 'flat_category.htm' : 'flat.htm'; } else { $pre .= 2 == $type ? 'index_category.htm' : 'index.htm'; } return $pre; } ?>Programmatically creating a .NET Core AppHost for a DLL in C# - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Programmatically creating a .NET Core AppHost for a DLL in C# - Stack Overflow

programmeradmin1浏览0评论

Starting with .NET Core, when we build an executable project in C# (such as a Console App or a desktop application like Windows Forms, WPF, etc), it generates more than just one .exe file - the actual IL code is stored in the DLL file, and the .exe is an unmanaged stub that locates .NET and passes the DLL. There's also the .deps.json & .runtimeconfig.json files. Example:

ConsoleApp1.deps.json - Dependencies
ConsoleApp1.dll - IL Code
ConsoleApp1.exe - Unmanaged Stub
ConsoleApp1.pdb - Program Debug Database, e.g. variable names
ConsoleApp1.runtimeconfig.json - .NET Runtime Configuration

Now, here's the problem. I have a DLL file generated using Cecil that contains the IL code, including an entry point. So I need the .exe file that I talked about - the unmanaged stub, ConsoleApp1.exe, to ensure that I can execute the only DLL file I have.

My question is, is it possible to generate the AppHost - that unmanaged executable serving as a .NET stub, ConsoleApp1.exe - programmatically in C#, for my own generated DLL containing IL code?

Starting with .NET Core, when we build an executable project in C# (such as a Console App or a desktop application like Windows Forms, WPF, etc), it generates more than just one .exe file - the actual IL code is stored in the DLL file, and the .exe is an unmanaged stub that locates .NET and passes the DLL. There's also the .deps.json & .runtimeconfig.json files. Example:

ConsoleApp1.deps.json - Dependencies
ConsoleApp1.dll - IL Code
ConsoleApp1.exe - Unmanaged Stub
ConsoleApp1.pdb - Program Debug Database, e.g. variable names
ConsoleApp1.runtimeconfig.json - .NET Runtime Configuration

Now, here's the problem. I have a DLL file generated using Cecil that contains the IL code, including an entry point. So I need the .exe file that I talked about - the unmanaged stub, ConsoleApp1.exe, to ensure that I can execute the only DLL file I have.

My question is, is it possible to generate the AppHost - that unmanaged executable serving as a .NET stub, ConsoleApp1.exe - programmatically in C#, for my own generated DLL containing IL code?

Share Improve this question asked Jan 30 at 13:02 winscripterwinscripter 7381 gold badge7 silver badges37 bronze badges 4
  • maybe have a "generic" apphost.exe file compiled normally which you use as base to replace the dll name with your dll via hexedit. But sounds too hacky. – Ivan Petrov Commented Jan 30 at 16:59
  • 1 Don't build it. Your ConsoleApp1.exe host is a simple copy of the apphost.exe file included with the install. Just copy it yourself from, say, C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Host.win-x64\8.0.11\runtimes\win-x64\native. Or use dotnet.exe to execute your code. – Hans Passant Commented Jan 30 at 20:47
  • @HansPassant what about getting "The application to execute does not exist:" when you do this? – Ivan Petrov Commented Jan 31 at 7:05
  • 1 Not a "simple copy". The apphost's inner metadata is overwritten by Microsoft.NET.HostModel (see github/dotnet/runtime/tree/main/src/installer/managed/…). – winscripter Commented Jan 31 at 8:25
Add a comment  | 

2 Answers 2

Reset to default 2

Thanks to the comment posted by @HansPassant, I figured it out.

Here are the steps to do it.

  1. Copy the apphost.exe file from C:\Program Files\dotnet\packs\Microsoft.NET.App.Host.win-x64\<SDK VERSION>\runtimes\win-x64\native (replace <SDK VERSION> with the version of the .NET sdk like 8.0.12. Optionally replace win-x64 with the target OS-processor)
  2. In our C# program to generate the AppHost, install the package Microsoft.NET.HostModel, version 3.0.0, via NuGet.
  3. Add this single method invocation:
using Microsoft.NET.HostModel.AppHost;

HostWriter.CreateAppHost(
    appHostSourceFilePath: "apphost.exe",
    // ^^^
    // Simple copy of:
    // C:\Program Files\dotnet\Microsoft.NETCore.App.Host.win-x64\<SDK VERSION>\runtimes\win-x64\native\apphost.exe
    appHostDestinationFilePath: "ConsoleApp1.exe",
    // ^^^
    // Save apphost.exe as <appHostDestinationFilePath>
    appBinaryFilePath: "ConsoleApp1.dll",
    // ^^^
    // Path to the managed DLL, in my case, the one generated with Cecil
    assemblyToCopyResorcesFrom: "ConsoleApp1.dll",
    // ^^^
    // Can be used to copy assembly info like title, version, company, etc
    windowsGraphicalUserInterface: false
    // ^^^
    // false - console window is shown
    // true - console window is hidden, typically for desktop apps like windows forms or WPF
);
  1. Add the necessary JSON files:
    • ConsoleApp1.deps.json:
{
  "runtimeTarget": {
    "name": ".NETCoreApp,Version=v8.0",
    "signature": ""
  },
  "compilationOptions": {},
  "targets": {
    ".NETCoreApp,Version=v8.0": {
      "ConsoleApp1/1.0.0": {
        "runtime": {
          "ConsoleApp1.dll": {}
        }
      }
    }
  },
  "libraries": {
    "ConsoleApp1/1.0.0": {
      "type": "project",
      "serviceable": false,
      "sha512": ""
    }
  }
}
  • ConsoleApp1.runtimeconfig.json
{
  "runtimeOptions": {
    "tfm": "net8.0",
    "framework": {
      "name": "Microsoft.NETCore.App",
      "version": "8.0.0"
    },
    "configProperties": {
      "System.Runtime.Serialization.EnableUnsafeBinaryFormatterSerialization": false
    }
  }
}

The apphost is generated, and it works as tested.

You can always run the .dll code with this command:

dotnet ConsoleApp1.dll

This is in fact how you launch it on Linux without .exe file.

发布评论

评论列表(0)

  1. 暂无评论