.NET Framework x86 vs 64 vs anycpu

来源:互联网 发布:淘宝怎么搜假手表 编辑:程序博客网 时间:2024/06/05 14:35

描述:

针对manage code c#, VS 编译时有x86, x64以及anycpu 的变异选项, 而针对unmanaged code c++,则没有anycpu的选项。

那么对于c# 代码,3中 编译条件究竟有什么差别呢:

x86: the assembly只会被作为x86的assembly 加载, 即使.NET framework 或者OS 是64位, 不能被x64 进程加载, 否则抛出BadImageFormatException

x64: assembly 只能作为x64 assembly 加载, x86 进程和os 无法加载。

anycpu: 编译所产生的assembly 不受cpu 类型影响, 如果assembly 类型位exe,  当在x64 os  .net framework上运行时,他就是x64的进程,反之则为x86进程。如果assembly 类型为dll 时,它的类型与加载它的进程类型相同,例如在64 os 系统中, 有一个x86进程加载了一个 anycpu 编译的dll, 此时加载的dll为32位的。

scenarios:

1. On 32 bit os:

a. x86.exe ---- run as a 32 bit process, can load assemblies that compiled by anycpu and x86, will get BadImageFormatException when loads x64 compiled assembly.(dll)

b. x64.exe ---  can not be run and get BadImageFormatException error.

c. anycpu.exe --- same as x86.exe


2. On 64 bit os:

a. x86.exe ---- run as a 32 bit process, can load assemblies that compiled by anycpu and x86, will get BadImageFormatException when loads x64 compiled assembly.(dll)

b. x64.exe ---- run as a 64 bit process, assemblies compiled by anycpu and x64 can be loaded, will get BadImageFormatException when try to load x86 assembly.(dll)

c. anycpu.exe --- same as x64.exe.


PS:

如果exe 在x64 os中运行,且exe 依赖32位的assembly, 那么该exe必须以x86 编译, 否则在64位系统中,用anycpu编译出来的会座位64 bit exe 运行,这样会导致在加载32 bit dependance dll 时 出现异常。


0 0