Fork me on GitHub

Visual Studio 64位应用程序编译

Visual Studio的编译选项 build下的platform有X64、Any CPU和x86。X86表示只能在32位环境下运行,X64表示只能在64位环境下运行,Any CPU表示你的程序集可以根据环境变化适应32位还是64位,但是如果你的程序集依赖于一个x86选项编译的程序集,哪么你的程序集只能选择X86进行编译,而不能选择Any CPU编译,如果使用Any CPU编译,运行就会出现如下的错误:

   Unhandled Exception: System.BadImageFormatException: Could not load file or asse
mbly 'SourceCode.HostClientAPI, Version=4.0.0.0, Culture=neutral, PublicKeyToken
=16a2c5aaaa1b130d' or one of its dependencies. An attempt was made to load a pro
gram with an incorrect format.
File name: 'SourceCode.HostClientAPI, Version=4.0.0.0, Culture=neutral, PublicKe
yToken=16a2c5aaaa1b130d'

原因是如果用Any CPU编译,那么在64位机器默认的运行就是64位的,通常这情况十assembly需要调用一个32的COM库,只要用x86编译,那么程序就是以32位的程序,依靠wow64来运行的,就可以在64位系统上运行。所以,如果assembly需要使用win32库的话,那么编译选项就不能使用默认的Any CPU就需要注意了。

如果你的程序集还有调用VC++编写的程序集,如果没有安装Microsoft Visual C++ 2005/2008/SP1 Redistributable Package (x86)更新包,还会出现类似的错误:

 

Unhandled Exception: System.IO.FileLoadException: Could not load file or assembl

y 'SourceCode.HostClientAPI, Version=4.0.0.0, Culture=neutral, PublicKeyToken=16

a2c5aaaa1b130d' or one of its dependencies. The application has failed to start

because its side-by-side configuration is incorrect. Please see the application

event log or use the command-line sxstrace.exe tool for more detail. (Exception

from HRESULT: 0x800736B1)

File name: 'SourceCode.HostClientAPI, Version=4.0.0.0, Culture=neutral, PublicKe

yToken=16a2c5aaaa1b130d' ---> System.Runtime.InteropServices.COMException (0x800

736B1): The application has failed to start because its side-by-side configurati

on is incorrect. Please see the application event log or use the command-line sx

strace.exe tool for more detail. (Exception from HRESULT: 0x800736B1)

具体使用Microsoft Visual C++ Redistributable Package 的2005,2008,2008 SP1版本,取决于你开发的应用程序所使用Visual Studio的版本。

Microsoft Visual C++ 2008 SP1 Redistributable Package (x86)

上述问题发生在我准备在Windows Server 2008 R2 Server Core上部署应用程序所碰到的问题的总结,折腾了将近一天的时间,希望对今后碰到类似问题的同学有帮助,可以节省更多的时间。

posted @ 2010-04-08 16:37  张善友  阅读(13802)  评论(15编辑  收藏  举报