Solved

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

Posted on 2013-11-18
3
1,434 Views
Last Modified: 2013-11-19
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
Comment
Question by:dominicwong
  • 2
3 Comments
 
LVL 21

Accepted Solution

by:
Craig Wagner earned 500 total points
ID: 39659913
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
 

Author Comment

by:dominicwong
ID: 39660934
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
 

Author Closing Comment

by:dominicwong
ID: 39660938
Thanks again. Very much appreciated!!
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question