?
Solved

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

Posted on 2013-11-18
3
Medium Priority
?
1,487 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 21

Accepted Solution

by:
Craig Wagner earned 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

765 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