Solved

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

Posted on 2013-11-18
3
1,425 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…

930 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now