8
votes

I am trying to add a reference to a .Net 2.0 DLL in a WPF application that is targeted to the .Net 4 Framework.

I added <startup useLegacyV2RuntimeActivationPolicy="true"> to the app.config file. The WPF app builds fine, but gets a BadImageFormatException at Runtime when trying to access the .Net 2.0 DLL.

"An attempt was made to load a program with an incorrect format"

This works with a new test WPF project, but does not work on my app. My app uses Entity Framework and MEF. Could these technologies be causing the issue?

Any ideas?

Edit: Resolved

According to the comment by Alois below, I had my main app targeted to 'Any CPU' and the DLL was targeted to 32-bit.

<startup useLegacyV2RuntimeActivationPolicy="true"> was not required

1
Could it be that the assembly is compiled for 32 bit and you are loading it into a 64 bit process or vice versa? Use corflags from the .NET Framework SDK to check.Alois Kraus
@Alois: Thanks. I hadn't thought of that. My app was targeted to 'Any CPU'. Changing it to x86 lets it load correctly.Mike

1 Answers

2
votes

When you have to use the useLegacyV2RuntimeActivationPolicy attribute then you are working with a mixed-mode assembly that was written in C++/CLI and targeting version 2.0.50727 of the CLR. Such an assembly contains both managed code and native machine code. That machine code is 32-bit in your case, you can't run it in a 64-bit process. Which is what the exception means. Setting the Platform target to x86 in your EXE project is required.