使用 ASP.NET 中的本机 DLL 时出现 BadImageFormatException

发布于 2024-09-24 04:00:24 字数 3044 浏览 6 评论 0原文

您好,我在我的 ASP.NET 应用程序中引用了一个托管 C++ 项目,该项目使用名为“libmmd.dll”的本机 dll。如果我使用 Visual Studio 运行 ASP.NET 应用程序,我会收到 BadImageFormatException,它告诉我“该模块预计包含程序集清单”(翻译自德语)。

将本机 dll 包含到 ASP.NET 项目中的首选方法是什么?在哪个目录下搜索?在路径“C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files”中似乎只有托管程序集,而不是本机 dll。

[FileNotFoundException: Das angegebene Modul wurde nicht gefunden. (Ausnahme von HRESULT: 0x8007007E)]
   System.Reflection.Assembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, Assembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection) +0
   System.Reflection.Assembly.nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, Assembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection) +43
   System.Reflection.Assembly.InternalLoad(AssemblyName assemblyRef, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection) +127
   System.Reflection.Assembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection) +142
   System.Reflection.Assembly.Load(String assemblyString) +28
   System.Web.Configuration.CompilationSection.LoadAssemblyHelper(String assemblyName, Boolean starDirective) +46

[ConfigurationErrorsException: Das angegebene Modul wurde nicht gefunden. (Ausnahme von HRESULT: 0x8007007E)]
   System.Web.Configuration.CompilationSection.LoadAssemblyHelper(String assemblyName, Boolean starDirective) +613
   System.Web.Configuration.CompilationSection.LoadAllAssembliesFromAppDomainBinDirectory() +203
   System.Web.Configuration.CompilationSection.LoadAssembly(AssemblyInfo ai) +105
   System.Web.Compilation.BuildManager.GetReferencedAssemblies(CompilationSection compConfig) +178
   System.Web.Compilation.BuildProvidersCompiler..ctor(VirtualPath configPath, Boolean supportLocalization, String outputAssemblyName) +54
   System.Web.Compilation.ApplicationBuildProvider.GetGlobalAsaxBuildResult(Boolean isPrecompiledApp) +227
   System.Web.Compilation.BuildManager.CompileGlobalAsax() +52
   System.Web.Compilation.BuildManager.EnsureTopLevelFilesCompiled() +337

[HttpException (0x80004005): Das angegebene Modul wurde nicht gefunden. (Ausnahme von HRESULT: 0x8007007E)]
   System.Web.Compilation.BuildManager.ReportTopLevelCompilationException() +58
   System.Web.Compilation.BuildManager.EnsureTopLevelFilesCompiled() +512
   System.Web.Hosting.HostingEnvironment.Initialize(ApplicationManager appManager, IApplicationHost appHost, IConfigMapPathFactory configMapPathFactory, HostingEnvironmentParameters hostingParameters) +729

[HttpException (0x80004005): Das angegebene Modul wurde nicht gefunden. (Ausnahme von HRESULT: 0x8007007E)]
   System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +8890751
   System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +85
   System.Web.HttpRuntime.ProcessRequestInternal(HttpWorkerRequest wr) +259

Hi Iam referencing in my ASP.NET application a managed C++ project which makes use of a native dll named "libmmd.dll". If I run the ASP.NET application with visual studio I get an BadImageFormatException which tells me "The module was expected to contain an assembly manifest" (translated from german).

What is the preferred way to include native dlls into asp.net projects? In which directory are the searched for? In the path "C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files" there seem to be only the managed assemblies, not the native dlls.

[FileNotFoundException: Das angegebene Modul wurde nicht gefunden. (Ausnahme von HRESULT: 0x8007007E)]
   System.Reflection.Assembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, Assembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection) +0
   System.Reflection.Assembly.nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, Assembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection) +43
   System.Reflection.Assembly.InternalLoad(AssemblyName assemblyRef, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection) +127
   System.Reflection.Assembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection) +142
   System.Reflection.Assembly.Load(String assemblyString) +28
   System.Web.Configuration.CompilationSection.LoadAssemblyHelper(String assemblyName, Boolean starDirective) +46

[ConfigurationErrorsException: Das angegebene Modul wurde nicht gefunden. (Ausnahme von HRESULT: 0x8007007E)]
   System.Web.Configuration.CompilationSection.LoadAssemblyHelper(String assemblyName, Boolean starDirective) +613
   System.Web.Configuration.CompilationSection.LoadAllAssembliesFromAppDomainBinDirectory() +203
   System.Web.Configuration.CompilationSection.LoadAssembly(AssemblyInfo ai) +105
   System.Web.Compilation.BuildManager.GetReferencedAssemblies(CompilationSection compConfig) +178
   System.Web.Compilation.BuildProvidersCompiler..ctor(VirtualPath configPath, Boolean supportLocalization, String outputAssemblyName) +54
   System.Web.Compilation.ApplicationBuildProvider.GetGlobalAsaxBuildResult(Boolean isPrecompiledApp) +227
   System.Web.Compilation.BuildManager.CompileGlobalAsax() +52
   System.Web.Compilation.BuildManager.EnsureTopLevelFilesCompiled() +337

[HttpException (0x80004005): Das angegebene Modul wurde nicht gefunden. (Ausnahme von HRESULT: 0x8007007E)]
   System.Web.Compilation.BuildManager.ReportTopLevelCompilationException() +58
   System.Web.Compilation.BuildManager.EnsureTopLevelFilesCompiled() +512
   System.Web.Hosting.HostingEnvironment.Initialize(ApplicationManager appManager, IApplicationHost appHost, IConfigMapPathFactory configMapPathFactory, HostingEnvironmentParameters hostingParameters) +729

[HttpException (0x80004005): Das angegebene Modul wurde nicht gefunden. (Ausnahme von HRESULT: 0x8007007E)]
   System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +8890751
   System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +85
   System.Web.HttpRuntime.ProcessRequestInternal(HttpWorkerRequest wr) +259

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沐歌 2024-10-01 04:00:24

确保 ASP.NET 应用程序的位数(由 IIS 应用程序池设置)与本机 dll 的位数相匹配。例如,32 位进程无法加载 64 位 dll。


补充:codymanix 评论道:

我的电脑是32位的,也都是Dll。当我将 dll 放在应用程序文件夹中时,该库在 Windows 窗体应用程序中运行。但在 ASP.NET 中我不知道将 dll 放在哪个文件夹中。

将 dll 放入正确位置的最简单方法是让 VS/ASP.NET 执行以下任一操作:

  • 在项目引用上将“Copy Local”参数设置为 true。
  • 将 dll 添加到项目中,构建操作为 none,并复制到目标文件夹。
  • 将 dll 添加到项目的 bin 文件夹中。

我将从最后一个选项开始,但还要检查 进程监视器查看 Windows 尝试从何处加载 dll(即加载路径中包含哪些 Web 应用程序文件夹)。本机程序集和托管程序集之间的交互可能有点困难(由于加载程序搜索规则不同),ASP.NET 然后添加了自己的差异,这些差异在一定程度上适用于本机 dll。

Ensure that the bitness of your ASP.NET application (this is set by the IIS application pool) matches the bitness of the native dll. E.g. a 32bit process cannot load a 64bit dll.


Additional: codymanix commented:

My Computer is 32 Bit, also are all Dlls. The library worked in an windows forms application when I placed the dll in the application folder. But in ASP.NET I don't know in which folder to place the dll.

The easiest way to get a dll into the right location is to let VS/ASP.NET do it by either:

  • On the project reference set the "Copy Local" parameter to true.
  • Add the dll to the project with a build action of none, and to copy to the destination folder.
  • Add the dll to the project in its bin folder.

I would start with the last option, but also check with Process Monitor to see where Windows is trying to load the dll from (i.e. what web app folders are included in the load path). The interaction between native and managed assemblies can be a little difficult (due to different loader search rules), ASP.NET then adds its own differences which apply to a degree to native dlls.

小鸟爱天空丶 2024-10-01 04:00:24

我没有设置 Windows %PATH% 变量来包含我的本机 DLL 的路径,现在它可以工作了。

我还必须重新启动机器才能使其运行。

I not set the windows %PATH%-variable to include the path to my native DLLs and now it works.

I also had to restart my machine to get it run.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文