AnyCPU x86 x64

转自http://blogs.microsoft.co.il/sasha/2012/04/04/what-anycpu-really-means-as-of-net-45-and-visual-studio-11/

 

What AnyCPU Really Means As Of .NET 4.5 and Visual Studio 11

The 32-bit and 64-bit development story on Windows seemingly never stops causing problems for developers. It’s been a decade since 64-bit processors have started popping up in the Windows consumer environment, but we just can’t get it right. If you forget some of the gory details, here are a couple of reminders:

  • 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.
  • When a 32-bit process opens a file in C:\Program Files, it actually reads/writes to C:\Program Files (x86).
  • 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.

These differences are hardly elegant as they are, but they allow 32-bit applications to run successfully on a 64-bit Windows system. While unmanaged applications always had to choose the native target—x86, x64, or ia64 in the Visual Studio case—managed code has the additional choice of AnyCPU.  (也就是说只有托管代码有AnyCPU选项,普通的C++代码编译时候只能选X86、X64、ia64)

What AnyCPU used to mean up to .NET 4.0 (and Visual Studio 2010) is the following:

  • If the process runs on a 32-bit Windows system, it runs as a 32-bit process. IL is compiled to x86 machine code.
  • If the process runs on a 64-bit Windows system, it runs as a 64-bit process. IL is compiled to x64 machine code.
  • If the process runs on an Itanium Windows system (has anyone got one? ;-)), it runs as a 64-bit process. IL is compiled to Itanium machine code.

Prior to Visual Studio 2010, AnyCPU was the default for most .NET projects, which was confusing to some developers: when they ran the application on a 64-bit Windows system, the process was a 64-bit process, which may cause unexpected results. For example, if the application relies on an unmanaged DLL of which only a 32-bit version is available, its 64-bit version won’t be able to load that component.

In Visual Studio 2010, x86 (and not AnyCPU) became the default for most .NET projects—but otherwise the semantics haven’t changed.

In .NET 4.5 and Visual Studio 11 the cheese has been moved. The default for most .NET projects is again AnyCPU, but there is more than one meaning to AnyCPU now. There is an additional sub-type of AnyCPU, “Any CPU 32-bit preferred”, which is the new default (overall, there are now five options for the /platform C# compiler switch: x86, Itanium, x64, anycpu, and anycpu32bitpreferred). When using that flavor of AnyCPU, the semantics are the following:

  • If the process runs on a 32-bit Windows system, it runs as a 32-bit process. IL is compiled to x86 machine code.
  • If the process runs on a 64-bit Windows system, it runs as a 32-bit process. IL is compiled to x86 machine code.
  • If the process runs on an ARM Windows system, it runs as a 32-bit process. IL is compiled to ARM machine code.

The difference, then, between “Any CPU 32-bit preferred” and “x86” is only this: a .NET application compiled to x86 will fail to run on an ARM Windows system, but an “Any CPU 32-bit preferred” application will run successfully.

To inspect these changes, I created a new C# console application in Visual Studio 11 that prints the values of Environment.Is64BitOperatingSystem and Environment.Is64BitProcess. When I ran it on my 64-bit Windows system, the result was as follows:

Is64BitOperatingSystem = True 
Is64BitProcess         = False

Inspecting the project’s properties shows the following (in the current Visual Studio UI “Prefer 32-bit” is grayed out and unchecked, where in actuality it is enabled…):

image

Inspecting the executable with CorFlags.exe shows the following:

Version   : v4.0.30319 
CLR Header: 2.5 
PE        : PE32 
CorFlags  : 131075 
ILONLY    : 1 
32BITREQ  : 0 
32BITPREF : 1 
Signed    : 0

After changing the 32BITPREF setting with CorFlags.exe (using the /32bitpref- option), the output was as follows:

Is64BitOperatingSystem = True 
Is64BitProcess         = True

 

在X86操作系统上:

目标平台程序类型运行结果
X86应用程序exe在32位CLR上运行
X86类库dll在32位CLR上运行
X64应用程序exe无法运行
X64类库dll无法运行
AnyCPU应用程序exe在32位CLR上运行
AnyCPU类库dll

在32位CLR上运行

 

 

在X64操作系统上:

目标平台程序类型运行结果
X86应用程序exe在WOW下的32位CLR上运行
X86类库dll在WOW下的32位CLR上运行
X64应用程序exe在64位CLR上运行
X64类库dll在64位CLR上运行
AnyCPU应用程序exe在64为CLR上运行
AnyCPU类库dll在与加载它的主程序相同的CLR上运行

举几个例子: 

主程序DllX86操作系统X64操作系统
目标平台X86目标平台X86在32位CLR上运行在WOW下的32位CLR上运行
目标平台X86目标平台X64XX
目标平台X86目标平台AnyCPU在32位CLR上运行在WOW下的32位CLR上运行
目标平台X64目标平台X64X在64位CLR上运行
目标平台X64目标平台X86XX
目标平台X64目标平台AnyCPUX在64位CLR上运行
目标平台AnyCPU目标平台X64X在64为CLR上运行
目标平台AnyCPU目标平台X86在32位CLR上运行X
目标平台AnyCPU目标平台AnyCPU在32位CLR上运行在64位CLR上运行

一般来讲,我们可以把主程序的目标平台根据实际需求设置为跟部署的操作系统一致,类库最好是AnyCPU。当然特殊的部署环境还要特殊考虑。

 

对于托管DLL或EXE,我们可以使用CorFlags.exe工具来查看它的目标平台,

找到corflags.exe + dll位置    "C:\Program Files (x86)\Microsoft SDKs\Windows\v8.1A\bin\NETFX 4.5.1 Tools\CorFlags.exe" dll_name
·         anycpu: PE = PE32    and  32BIT = 0
·         x86:      PE = PE32    and  32BIT = 1
·         64-bit:  PE = PE32+  and  32BIT = 0
例如:

 

若使用CorFlags.exe查看非托管DLL或EXE,则会报错,如下:

 

 使用dumpbin.exe也可以查DLL或者EXE是32位或者64位的,这个对托管和非托管都管用。

"D:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\dumpbin.exe" /headers dll_name,例如:

 

使用dumpbin还可以查看dll暴露出的接口:

"D:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\dumpbin.exe" /exports dll_name 

例如:

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值