BadImageFormatException when using native DLLs from ASP.NET

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:WINDOWSMicrosoft.NETFrameworkv2.0.50727Temporary 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

Ensure that the bitness of your ASP.NET application (this is set by the IIS application pool) matches the bitness of the native dll. Eg 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 (ie 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.


    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.

    链接地址: http://www.djcxy.com/p/44534.html

    上一篇: 如何在运行时指定[DllImport]路径?

    下一篇: BadImageFormatException从ASP.NET使用本机DLL时