Visual Studio .NET Platform Target Explained

原文:

 

The Platform Target is an often overlooked option in the build configuration for Visual Studio 2005 and 2008 projects.  For C# projects, you will find this option on the Project Properties dialog under the build tab.  For VB.net projects, the setting is found on the Advanced Compiler Settings dialog which is made available via the Advanced Compile Options button on the Compile tab of the Project Properties.  The list of available options are:

  1. Any CPU
  2. x86
  3. x64
  4. Itanium

The meaning of these options is often misunderstood.  Based on their names, one might think that the compiler will generate code differently based upon the setting.  However, the C# and VB.net compilers only generate IL code that is taken to native code by the CLR at runtime using the just-in-time compiler (unless ngen is used but that is another discussion).  The fact is that this setting actually does not affect the build of the assembly in any way except to set the platform status information on the assembly’s CLR header.  In other words, the Platform Target setting is meant to communicate the platform that the developer intends to be compatible with.

 

The default setting, Any CPU, means that the assembly will run natively on the CPU is it currently running on.  Meaning, it will run as 64-bit on a 64-bit machine and 32-bit on a 32-bit machine.  If the assembly is called from a 64-bit application, it will perform as a 64-bit assembly and so on.

 

If the project is set to x86, this means the project is intended to run only as a 32-bit process.  A 64-bit process will be unable to call into an assembly set as X86.  Reasons to set your project as x86 include dependencies upon native DLLs that are only available in 32-bit or making native calls assuming 32-bit .  Applications and assemblies marked for x86 can still run on 64-bit Windows.  However they run under WOW64 .  Visual Studio itself runs under this emulation mode since it is a 32-bit application.

 

Setting the project to x64 will specify that the assembly must run under 64-bit Windows.  Attempting to run the assembly on 32-bit Windows or call the assembly from a 32-bit process will result in a runtime error.

 

The final, and likely least common, setting is Itanium.  This setting specifies that the assembly will only run on an Itanium processor.  The only reason to set your project for Itanium is if it has dependencies on native DLLs compiled specifically for the Itanium processor.

 

Developers should be aware that simply marking Platform Target of an assembly as Any CPU does not guarantee that it will run on both 32-bit and 64-bit Windows.  In fact, since Any CPU is the default setting, there are likely many applications that actually depend upon 32-bit processing but are not marked as such.  These assemblies will fail under 64-bit Windows when run from a 64-bit process so it is important to perform testing for your applications before users are migrated to 64-bit.  The somewhat increasing popularity of Vista x64 will soon elevate the importance of this type of testing.

 

Another point to be aware of when attempting to migrate to 64-bit processing for .Net applications is the dependency upon assemblies originally built using Visual Studio 2002 and 2003.  All assemblies targeting .Net 1.0 and 1.1 will be 32-bit only (x86) so these assemblies will need to be upgraded to newer versions, if available, or the functionality will need to be replaced in order for the process to execute as 64-bit.

 

应用程序的平台标志, 操作系统的版本 和 CPU的系统架构

 

一个标记为X86的Assembly能否在64位的环境下运行,包含有两层意思:

能否在64位的操作系统下运行/或被调用执行

能否被64位的进程调用

 

 

仅仅改变Platform的类型,并不会影响assembly中的IL代码,只不过改变了assembly头部中的标志位而已。不过,也就是说Assembly的内容是完全一样的。不过,JIT生成的native code应该会不一样。

 

标记为Any Cpu 可以在X86 或者 X64下使用

不过,标记为Any CPU 并不一定能保证它确实可以在X86或者X64的环境下顺利运行。因为Any CPU是标准配置。很可能某些库实际上只能在X86或者X64下运行,但却被标记为Any Cpu。 当某些别的程序适用它的时候,就可能发生问题。

 

最根本的原因,这个设置只改变头部标志位,并不强制改变任何内容

 

标记为X86的只能被X86应用程序调用,不能被64的进程调用。

但是,标记为X86的应用程序可以在x64的操作系统下运行(WOW64 模拟器)。标记为X86的库也可以被X86的应用程序在x64的操作系统下调用。

至于为什么不标记为Any Cpu, 而要标记为限制比较多的X86,是因为很可能应用程序或者库依赖于某些只能在X86 32位环境下运行的库

 

标记为X64只能在64位进程中使用

 

在Framework1.0 或者Framework1.1 环境下编译的库只能在X86下运行。

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值