简体   繁体   English

VS2012在x64 OS中使用“任何CPU”编译为x86

[英]VS2012 compiles to x86 with “Any CPU” in a x64 OS

I have a solution with a C# project and a C DLL. 我有一个C#项目和一个C DLL的解决方案。 The solution is set to "Mixed Platforms" and the C# project to "Any CPU". 该解决方案设置为“混合平台”,C#项目设置为“任何CPU”。

When the DLL is set to compile for x64, I get a crash. 当DLL设置为针对x64编译时,出现崩溃。 But when the DLL is set to compile for x86, everything works fine. 但是,当DLL设置为针对x86编译时,一切正常。

The OS is Windows7 x64. 操作系统是Windows7 x64。

Environment.Is64BitProcess gives false no matter which I try. 无论我尝试哪种,Environment.Is64BitProcess都为false。

My understanding is that "Any CPU" would compile to x64 by default since the OS is x64. 我的理解是,由于操作系统为x64,默认情况下,“任何CPU”都将编译为x64。 Clearly, I am wrong. 显然,我错了。

Thank you guys. 感谢大伙们。

My understanding is that "Any CPU" would compile to x64 by default 我的理解是,默认情况下,“任何CPU”都会编译为x64

Nope, it'll compile to "Any CPU". 不,它将编译为“任何CPU”。 What it runs as depends on what machine is executing it . 它的运行方式取决于执行它的机器 So if you are running it on a 64-bit CPU, then yes, it will run as 64-bit. 因此,如果您在64位CPU上运行它,则可以,它将以64位运行。 However, from the crash, it sounds like your C dll is 32-bit, or you are using 32-bit API calls. 但是,从崩溃中听起来,您的C dll是32位的,或者您正在使用32位的API调用。 In which case you will need to compile it for 32-bit. 在这种情况下,您需要将其编译为32位。 Note that in VS2013 there is also a "Prefer 32-bit" option for "Any CPU", which will run as 32-bit unless 32-bit simply isn't available. 请注意,在VS2013中,“任何CPU”也有一个“首选32位”选项,除非32位根本不可用,否则它将以32位运行。 Most people want to turn this off ;p 大多数人都希望关闭此功能 ,P

Note also that if this is a library (from the dll tag), it will also depend on whether the process is 32-bit vs 64-bit. 还要注意,如果这是一个 (来自dll标签),则还取决于该进程是32位还是64位。

First we should now what is Platform target : 首先,我们现在应该是什么平台目标

anycpu (default) compiles your assembly to run on any platform. anycpu (默认)可编译您的程序集以在任何平台上运行。 Your application runs as a 64-bit process whenever possible and falls back to 32-bit when only that mode is available. 您的应用程序将尽可能以64位进程的形式运行,只有该模式可用时,应用程序才会回退至32位。

anycpu32bitpreferred compiles your assembly to run on any platform. anycpu32bitpreferred编译您的程序集以在任何平台上运行。 Your application runs in 32-bit mode on systems that support both 64-bit and 32-bit applications. 您的应用程序在支持64位和32位应用程序的系统上以32位模式运行。 You can specify this option only for projects that target the .NET Framework 4.5. 您只能为面向.NET Framework 4.5的项目指定此选项。

ARM compiles your assembly to run on a computer that has an Advanced RISC Machine (ARM) processor. ARM将您的程序集编译为在具有高级RISC计算机(ARM)处理器的计算机上运行。

x64 compiles your assembly to be run by the 64-bit common language runtime on a computer that supports the AMD64 or EM64T instruction set. x64会在支持AMD64或EM64T指令集的计算机上编译要由64位公共语言运行时运行的程序集。

x86 compiles your assembly to be run by the 32-bit, x86-compatible common language runtime. x86编译您的程序集,使其由32位,兼容x86的公共语言运行时运行。 Itanium compiles your assembly to be run by the 64-bit common language runtime on a computer with an Itanium processor. Itanium可以在具有Itanium处理器的计算机上编译要由64位公共语言运行时运行的程序集。

So anyCpu means run 32bit and 64bit depend on your cpu.I think your dll is 32bit so yo can use x86 or anycpu . 所以anyCpu意味着运行32位和64位取决于您的cpu。我认为您的dll是32位的,因此您可以使用x86anycpu

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM