【问题标题】:BadImageFormatException when using native DLLs from ASP.NET使用来自 ASP.NET 的本机 DLL 时出现 BadImageFormatException
【发布时间】:2010-09-16 10:38:14
【问题描述】:

嗨,我在我的 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

【问题讨论】:

    标签: .net asp.net dll interop managed-c++


    【解决方案1】:

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


    补充:codymanix 评论:

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

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

    • 在项目参考中将“复制本地”参数设置为 true。
    • 将 dll 添加到项目中,构建操作为 none,然后复制到目标文件夹。
    • 将 dll 添加到项目的 bin 文件夹中。

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

    【讨论】:

    • 我的电脑是32位的,也都是Dll。当我将 dll 放在应用程序文件夹中时,该库在 Windows 窗体应用程序中工作。但在 ASP.NET 中,我不知道将 dll 放在哪个文件夹中。
    • @codymanix 所以,是时候进行进程监视器了。 Win32 试图从哪里加载本机 dll?
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-13
      • 1970-01-01
      • 2018-01-29
      • 2011-06-07
      • 2012-06-19
      • 2012-08-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多