热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

C#指令指示32位或64位构建-C#Directivetoindicate32-bitor64-bitbuild

IstheresomesortofC#directivetousewhenusingadevelopmentmachine(32-bitor64-bit)thats

Is there some sort of C# directive to use when using a development machine (32-bit or 64-bit) that says something to the effect of:

是否有某种C#指令在使用开发机器(32位或64位)时使用,该指令说明了以下内容:

if (32-bit Vista)
    // set a property to true
else if (64-bit Vista)
    // set a property to false

but I want to do this in Visual Studio as I have an application I'm working on that needs to be tested in 32/64 bit versions of Vista.

但我想在Visual Studio中这样做,因为我有一个我正在研究的应用程序需要在32/64位版本的Vista中进行测试。

Is something like this possible?

这样的事情可能吗?

9 个解决方案

#1


Can you do it at runtime?

你能在运行时做到吗?

if (IntPtr.Size == 4)
  // 32 bit
else if (IntPtr.Size == 8)
  // 64 bit

#2


There are two conditions to be aware of with 64-bit. First is the OS 64-bit, and second is the application running in 64-bit. If you're only concerned about the application itself you can use the following:

64位有两个需要注意的条件。第一个是OS 64位,第二个是64位运行的应用程序。如果您只关心应用程序本身,可以使用以下内容:

if( IntPtr.Size == 8 )
   // Do 64-bit stuff
else
   // Do 32-bit

At runtime, the JIT compiler can optimize away the false conditional because the IntPtr.Size property is constant.

在运行时,JIT编译器可以优化false条件,因为IntPtr.Size属性是常量。

Incidentally, to check if the OS is 64-bit we use the following

顺便提一下,要检查操作系统是否为64位,我们使用以下内容

if( Environment.GetEnvironmentVariable( "PROCESSOR_ARCHITEW6432" ) != null )
    // OS is 64-bit;
else
    // OS is 32-bit

#3


You can use a #if directive and set the value as a compiler switch (or in the project settings):

您可以使用#if指令并将值设置为编译器开关(或在项目设置中):

 #if VISTA64
     ...
 #else
     ...
 #endif

and compile with:

并编译:

 csc /d:VISTA64 file1.cs 

when compiling a 64 bit build.

编译64位版本时。

#4


What I use in my C# code is IntPtr.Size, it equals 4 on 32bit and 8 on 64bit:

我在C#代码中使用的是IntPtr.Size,它在32位上等于4,在64位上等于8:

string framework = (IntPtr.Size == 8) ? "Framework64" : "Framework";

#5


You can use Predefined Macros to set the properties on compilation

您可以使用预定义宏在编译时设置属性

    #if (_WIN64) 
  const bool IS_64 = true;
#else
  const bool IS_64 = false;
#endif

#6


I am not sure if this is what you are looking for but I check the IntPtr.Size to detect 32bit versus 64bit runtime. Note that this tells you the runtime environment, you might be running in WOW64

我不确定这是否是您正在寻找的但我检查IntPtr.Size以检测32位与64位运行时。请注意,这可以告诉您运行时环境,您可能正在WOW64中运行

if (IntPtr.Size == 4)
{
    //32 bit
}
else if (IntPtr.Size == 8)
{
    //64 bit
}
else
{
    //the future
}

#7


I know that this is an old topic, but I recently had to achieve the same result (i.e. determine at build time, not run time)

我知道这是一个老话题,但我最近必须达到相同的结果(即在构建时确定,而不是运行时)

I created new build configurations (x86 debug, x86 release, x64 debug, x64 release) and set BUILD64 or BUILD32 in the "Conditional compilation symbols" box in the application properties for each configuration.

我创建了新的构建配置(x86调试,x86版本,x64调试,x64版本),并在每个配置的应用程序属性的“条件编译符号”框中设置BUILD64或BUILD32。

When I needed to do something different between builds (like change the signature on some x86 exported methods from a .dll), I then used standard build directives to achieve what I needed. for instance:

当我需要在构建之间做一些不同的事情时(比如从.dll更改某些x86导出方法的签名),然后我使用标准构建指令来实现我需要的东西。例如:

#if BUILD64
// 64 Bit version
// do stuff here
#endif

#if BUILD32
// 32 Bit version
// do different stuff here
#endif

#8


Open the Configuration Manager from the Build. From there you should be able to set the Active solution platform and create configuration that specifically target x64, x86, or Any CPU. From there you can have code that conditionally compiles based on the current configuration.

从Build打开配置管理器。从那里,您应该能够设置Active解决方案平台并创建专门针对x64,x86或任何CPU的配置。从那里,您可以根据当前配置有条件地编译代码。

Note that this is usually a very bad idea, though. .Net programs are normally distributed as IL rather than native code. This IL is then compiled by the JIT compiler on each local machine the first time the user tries to run it. By leaving the default "Any CPU" selected, you allow the JIT compiler to make that determination for each individual machine.

请注意,这通常是一个非常糟糕的主意。 .Net程序通常以IL而不是本机代码的形式分发。然后,当用户第一次尝试运行它时,JIT编译器会在每台本地计算机上编译此IL。通过选择默认的“任何CPU”,允许JIT编译器为每台机器进行确定。

The main exception for this is when you have a dependency on a 32bit library. In that case, you don't want the JIT compiler to ever compile for x64, because it could break your interop with the library.

这种情况的主要例外是当你依赖32位库时。在这种情况下,您不希望JIT编译器为x64编译,因为它可能会破坏与库的互操作。

#9


There is nothing built in that will do this for you. You could always #define your own symbol and use that for conditional compilation if you wish.

内置任何内容都无法为您完成此任务。您可以随时#define您自己的符号,并根据需要将其用于条件编译。


推荐阅读
author-avatar
以后再试巛_113
这个家伙很懒,什么也没留下!
Tags | 热门标签
RankList | 热门文章
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有