• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1526
  • Last Modified:

Mysterious exception "BadImageFormatException" from an un-used DLL in C# project

Hi experts

My C# project imports an external DLL generated by C code. But after I'd removed all references to the DLL in my C# project. I am still getting this exception:
"BadImageFormatException" that complaint about a dll: "Could not load file or assembly file .... TestLib.dll or in one of its dependencies. The module was expected to contain an assembly manifest."

But my program had already removed all references to the DLL.
I'd done a Clean and Rebuilt Solution.
I'd rebooted my PC.
I'd manually deleted all the files in the bin/Debug folder (except the DLL file)

My question is where does the program remember looking for the DLL while I'd already removed it from everywhere I could think of.

Eventually to get rid of the exception, I need to remove the DLL from the folder. But it shouldn't be the case.

Also, my PC is 32-bit Windows XP. So why I am getting this exception that normally happens when 32-bit meets 64-bit??

Thanks in advance.
0
dominicwong
Asked:
dominicwong
  • 2
1 Solution
 
Craig WagnerSoftware ArchitectCommented:
You can also get this exception if you have code that dynamically loads assemblies in your code. For example, I have the following code that loops through all the files in a folder with a ".dll" extension and reports the .NET assembly version number. If I happen to encounter a non-assembly file I need to catch and handle the exception (in this case I ignore it because I don't care).

foreach( string fileName in Directory.GetFiles( Path.Combine( directory, "bin" ), "*.dll" ) )
{
    try
    {
        AssemblyName assemblyName = AssemblyName.GetAssemblyName( fileName );
        FileVersionInfo fileVersionInfo = FileVersionInfo.GetVersionInfo( fileName );
        // stuff left out for brevity...
    }
    catch( BadImageFormatException )
    {
        // Do nothing, it's not a .NET assembly so we don't care
    }
}

Open in new window


A good first step (which is really true of any exception you encounter) is to look at the stack trace you're getting along with the exception and find which class/method/line number is throwing and then figure out why the code is trying to load/reference that dll.
0
 
dominicwongAuthor Commented:
Thanks CraigWagner. I really thank you for your help!!
That was exactly where my problem was. In some part of my code, I was using reflection to load the assemblies:
            string[] assemblies = Directory.GetFiles(path, "*.dll");
            foreach (string assemblyFile in assemblies)
            {
                    Assembly asm = Assembly.LoadFrom(assemblyFile);
                    foreach (Type t in asm.GetExportedTypes())
                    {
                          :

Open in new window


Now I did as per your illustration and everything works fine. :)
0
 
dominicwongAuthor Commented:
Thanks again. Very much appreciated!!
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now