Ever seen this error message?
Could not load file or assembly 'Microsoft.Xna.Framework, Version=188.8.131.52, Culture=neutral, PublicKeyToken=6d5c3888ef60e27d' or one of its dependencies. An attempt was made to load a program with an incorrect format.
This is right up there in the pantheon of unhelpfulness. What does "incorrect format" mean, anyway? Has my framework installation somehow been corrupted?
Nine times out of ten, what the exception really meant to say was:
Could not load 32 bit assembly 'Microsoft.Xna.Framework' into a 64 bit process. Your game project is set to 'Any CPU' platform, when it should specify 'x86'.
Why does this occur?
64 bit Windows can run both 32 and 64 bit programs. Running a 32 bit program on a 64 bit operating system might sound slow, but actually isn't, because this uses a special CPU mode as opposed to slow emulation.
You can't mix and match, though. A 32 bit program can only load 32 bit libraries, and likewise 64 bit programs can only load 64 bit libs.
In .NET, you have three options:
- If you choose 'x86' as your target platform, the compiler produces a 32 bit program
- If you choose 'x64', you get a 64 bit program
- If you choose 'Any CPU' (the default), you get a program that can work in either mode. If you run it on 32 bit Windows, the jitter produces 32 bit code, but if you run on 64 bit Windows, it jits into 64 bit instructions. Nice!
But here's the thing: the XNA Framework is a 32 bit (x86) assembly. We do not provide a 64 bit version.
If you make an XNA Framework game using the 'Any CPU' configuration, then run this on 64 bit Windows, look what happens:
- The CLR creates a 64 bit process
- The jitter compiles your 'Any CPU' bytecode into 64 bit machine code
- Your game references the XNA Framework assembly
- The CLR looks for a 64 bit version of the XNA Framework, but cannot find one
- It tries to load the 32 bit version, but cannot load a 32 bit assembly into a 64 bit process
- This produces a confusing and poorly worded exception message 🙂
Why doesn't this happen all the time?
If you use our standard project templates, these are set up to target the 'x86' platform, not 'Any CPU'. This produces a 32 bit program, which will always run in 32 bit mode, even on 64 bit Windows.
The error only occurs if you create a project using some other template, which defaults to 'Any CPU', and then manually reference the XNA Framework. This often happens when people try to use the XNA Framework inside a WinForms application, for instance.
How do I fix it?
In your Visual Studio toolbar, there should be a combo box saying 'Any CPU'.
If you are using C# Express, this toolbar entry may be grayed out. To enable it, go to 'Tools / Options', check 'Show all settings', select the 'Projects and Solutions' tab, and check the 'Show advanced build configurations' box.
Pull down the 'Any CPU' toolbar combo, and choose 'Configuration Manager'. Open the 'Active solution platform' combo, choose '<New...>', and create an 'x86' configuration.