【.NET 4.5】新增的 Prefer 32-bit target platform

这本来是一个很小的feature并且也没有什么模糊的地方,关键是VS把这个设置成了默认值,当默认为Any CPU的时候,application会被编译成32-bit mode.

下边是我遇到的问题,在64-bit OS上,使用VS2013创建基于.net 4.5的console 工程,代码如下:

            // Software\Wow6432Node\mytest default value is 0
            var x86path =  @"Software\Wow6432Node\mytest";
            // Software\mytest default value is 1
            var x64path = @"Software\mytest";
            Console.WriteLine(Registry.LocalMachine.OpenSubKey(x86path).GetValue("").ToString()); // output is 0
            Console.WriteLine(Registry.LocalMachine.OpenSubKey(x64path).GetValue("").ToString()); // output is 0

Console.WriteLine(Registry.LocalMachine.OpenSubKey(x64path).GetValue("").ToString()); 希望的输出是1,最终输出0. 为什么呢?答案在MSDN中:

  • anycpu (default) compiles your assembly to run on any platform. Your application runs as a 64-bit process whenever possible and falls back to 32-bit when only that mode is available.

  • anycpu32bitpreferred compiles your assembly to run on any platform. Your application runs in 32-bit mode on systems that support both 64-bit and 32-bit applications. You can specify this option only for projects that target the .NET Framework 4.5.

  • x86 compiles your assembly to be run by the 32-bit, x86-compatible common language runtime.

http://msdn.microsoft.com/en-us/library/zekwfyz4.aspx

很显然,anycpu32bitpreferred 就是把application编译成了一个可以在ARM/X64/X86/Itanium下运行的x86 application。这一点算是一个增强,但不应该是一个默认值。

  • ------------------------
  • 64-bit and 32-bit tips:
  1. On a 64-bit Windows system, both the 32-bit and 64-bit versions of system DLLs are stored. The 64-bit DLLs are in C:\Windows\System32, and the 32-bit DLLs are in C:\Windows\SysWOW64.
  2. When a 32-bit process opens a file in C:\Program Files, it actually reads/writes to C:\Program Files (x86).
  3. There are separate views of (most of) the registry for 32-bit and 64-bit applications. You can change the 64-bit registry location and it wouldn’t be visible to 32-bit applications.

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。