Say I have 2 Winform projects A, B.
Project A (target .NET 2.0) has to be run on x86 (it is an external library) and for legacy reasons project B (target .NET 4.0) has to be run on Any CPU. Now I need to call A's methods from project B but it throws an error:
"An unhandled exception of type 'System.BadImageFormatException' occurred in System.Windows.Forms.dll"
Additional information: Could not load file or assembly 'CSharpDemo, Version=1.0.5414.18636, Culture=neutral, PublicKeyToken=null' or one of its dependencies. An attempt was made to load a program with an incorrect format."
However, I found if I target project B on .NET 4.5 then this problem doesn't occur. But I still want Project B to be targeting 4.0, how to achieve this?
You may find the answer to your question in following blog post
http://blogs.microsoft.co.il/sasha/2012/04/04/what-anycpu-really-means-as-of-net-45-and-visual-studio-11/
Here is summary
.net 4.0
.net 4.5
If any of your code calls 32-bit x86 libraries, the entire process needs to load as x86. No exceptions.
To do this, set the EXE project to x86, set DLL projects that have native x86 dependencies to x86, and for pure managed DLL projects you can use AnyCPU -- .NET will load them as x86 for your x86 EXE, but they can also load into x64 EXEs.
Remember that x86 code runs just fine on x64 processors running x64 Windows, with the help of the WOW64 compatibility layer.
(with limited exceptions: on Windows Server edition, WOW64 is an optional component, and in the preinstall environment there is no WOW64).