0

Despite reading a number of articles online, I am still confused about which platform target assemblies can be mixed with each other.

To test this, I created a Visual Studio Solution with three Console App projects and three .Net Framework class libraries.

The console app projects are AnyCPUApp, X86App, and X64App with the corresponding platform targets set in Configuration Manager and Project Properties > Build. The library projects are AnyCPULib, X86Lib, and X64Lib with the corresponding platform targets set in Configuration Manager and Project Properties > Build.

X86App will build and run with AnyCPULib and X86Lib. The compiler gives a "processor architecture mismatch" warning when X86App references X64Lib and it crashes with System.badImageFormatException if you try to run it.

X64App will build and run with AnyCPULib and X64Lib. The compiler gives a "processor architecture mismatch" warning when X64App references X86Lib and it crashes with System.badImageFormatException if you try to run it.

My questions:

  1. Why does AnyCPUApp run (with a "processor architecture mismatch" warning) against an X86Lib, but not against an X64Lib?
  2. Are there real-world cases where the processor architecture mismatch will cause a run-time failure?
  3. Is AnyCPU just meant for class libraries?

My xxxApp code looks like (change xxx to AnyCPU|X86|X64, uncomment clauses to test and update project references:

    namespace xxxApp {
    class Program {
        static void Main(string[] args) {
            //var anyCPULib = new AnyCPULib.AnyCPULib();
            //Console.WriteLine(anyCPULib.Hello);
            //System.Diagnostics.Debug.WriteLine(anyCPULib.Hello);

            //var x86Lib = new X86Lib.X86Lib();
            //Console.WriteLine(x86Lib.Hello);
            //System.Diagnostics.Debug.WriteLine(x86Lib.Hello);

            //var x64Lib = new X64Lib.X64Lib();
            //Console.WriteLine(x64Lib.Hello);
            //System.Diagnostics.Debug.WriteLine(x64Lib.Hello);
        }
    }
}

My xxxLib code looks like (change xxx to AnyCPU|X86|X64)

namespace AnyCPULib
{
    public class AnyCPULib
    {
        public string Hello { get { return "Hello from AnyCPULib"; } }
    }
}

I'm using Visual Studio 2019 Version 16.8.0 and .NET Framework 4.8

d ei
  • 493
  • 4
  • 16
  • That's a lot of question. Too many for an acceptable Stack Overflow post. That said: the underlying rule is that all the assemblies in a given process have to have the same architecture. You can mix AnyCPU with any other type of assembly, as long as the process is consistent. So to answer your first question: if your app is AnyCPU and it winds up running as x86, you can't load x64 assembly, and by default that's exactly what happens. If you change the default behavior on a 64-bit machine it will work with the x64 library but not the x86. See duplicate. – Peter Duniho Nov 15 '20 at 21:39
  • @Peter Duniho But that's not what my testing indicates. AnyCPUApp will not run with X64Lib, although X64App will run with AnyCPULib. Why is this? – d ei Nov 15 '20 at 22:11
  • @Peter Duniho Oh, wait, the reverse is true if I uncheck 'Prefer 32-bit'. Then, AnyCPUApp will run with X64Lib but no X86Lib. – d ei Nov 15 '20 at 22:21

0 Answers0