使用 ASP.NET 中的本机 DLL 时出现 BadImageFormatException
您好,我在我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
确保 ASP.NET 应用程序的位数(由 IIS 应用程序池设置)与本机 dll 的位数相匹配。例如,32 位进程无法加载 64 位 dll。
补充:codymanix 评论道:
将 dll 放入正确位置的最简单方法是让 VS/ASP.NET 执行以下任一操作:
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:
The easiest way to get a dll into the right location is to let VS/ASP.NET do it by either:
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.
我没有设置 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.