问题
I see the following error when running csi.exe:
System.BadImageFormatException: Could not load file or assembly 'xyz.dll' or one of its dependencies. is not a valid Win32 application. (Exception from HRESULT: 0x800700C1)
Compiling in Visual Studio raises a warning but compiles and runs without problems:
There was a mismatch between the processor architecture of the project being built "MSIL" and the processor architecture of the reference "xyz", "x86". This mismatch may cause runtime failures. Please consider changing the targeted processor architecture of your project through the Configuration Manager so as to align the processor architectures between your project and references, or take a dependency on references with a processor architecture that matches the targeted processor architecture of your project.
So it seems I have to specify processor architecture when running csi.exe? How to do that?
回答1:
Your assembly xyz.dll is compiled to only run in a 32bit process. This might be for a good reason (such as for example loading another native dll that is available with 32bit code only). In this case, use the solution presented below. It may also be for no good reason. In this case, change the target platform of xyz.dll.
The program CSI.exe (C# Interactive Compiler) is built in a way so that it will run as a 32bit executable on a 32bit version of Windows and it will run as a 64bit executable in a 64bit version of Windows.
When running as a 64bit executable, it will not be able to load an assembly that is built to only run in a 32bit process (such as your xyz.dll).
In order to change this, you can create a version of CSI.exe that runs as a 32bit process even on a 64bit version of Windows. Follow these steps:
- Locate the file csi.exe, make a copy of it in the same directory and rename the copy to csi32.exe. You can find csi.exe in a path similar to "C:\Program Files (x86)\MSBuild\14.0\Bin\csi.exe"
- Locate the file CorFlags.exe. You can find CorFlags.exe in a path similar to "C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6.2 Tools\CorFlags.exe"
- Open a commandline window as administrator.
Execute the following command using the correct paths for the two executable files
C:\Path\To\CorFlags.exe /32bit+ /force C:\Path\To\csi32.exe
You can now use csi32.exe in the place of csi.exe to run scripts that require assemblies/dlls which are only available in 32bit versions.
来源:https://stackoverflow.com/questions/42601462/how-to-set-platform-architecture-for-csi-exe