Solved

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

Posted on 2013-11-18
3
1,413 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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks again. Very much appreciated!!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

772 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

14 Experts available now in Live!

Get 1:1 Help Now