How to set platform architecture for csi.exe

这一生的挚爱 提交于 2020-01-23 13:23:46

问题


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:

  1. 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"
  2. 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"
  3. Open a commandline window as administrator.
  4. 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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!