热门标签 | 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您自己的符号,并根据需要将其用于条件编译。


推荐阅读
  • XNA 3.0 游戏编程:从 XML 文件加载数据
    本文介绍如何在 XNA 3.0 游戏项目中从 XML 文件加载数据。我们将探讨如何将 XML 数据序列化为二进制文件,并通过内容管道加载到游戏中。此外,还会涉及自定义类型读取器和写入器的实现。 ... [详细]
  • 本文介绍了如何在C#中启动一个应用程序,并通过枚举窗口来获取其主窗口句柄。当使用Process类启动程序时,我们通常只能获得进程的句柄,而主窗口句柄可能为0。因此,我们需要使用API函数和回调机制来准确获取主窗口句柄。 ... [详细]
  • 本文介绍如何在Linux Mint系统上搭建Rust开发环境,包括安装IntelliJ IDEA、Rust工具链及必要的插件。通过详细步骤,帮助开发者快速上手。 ... [详细]
  • Explore how Matterverse is redefining the metaverse experience, creating immersive and meaningful virtual environments that foster genuine connections and economic opportunities. ... [详细]
  • 本文详细介绍了如何在Linux系统上安装和配置Smokeping,以实现对网络链路质量的实时监控。通过详细的步骤和必要的依赖包安装,确保用户能够顺利完成部署并优化其网络性能监控。 ... [详细]
  • 实体映射最强工具类:MapStruct真香 ... [详细]
  • 深入解析 Spring Security 用户认证机制
    本文将详细介绍 Spring Security 中用户登录认证的核心流程,重点分析 AbstractAuthenticationProcessingFilter 和 AuthenticationManager 的工作原理。通过理解这些组件的实现,读者可以更好地掌握 Spring Security 的认证机制。 ... [详细]
  • 本文详细介绍了如何在 Windows 环境下使用 node-gyp 工具进行 Node.js 本地扩展的编译和配置,涵盖从环境搭建到代码实现的全过程。 ... [详细]
  • 本文将介绍如何编写一些有趣的VBScript脚本,这些脚本可以在朋友之间进行无害的恶作剧。通过简单的代码示例,帮助您了解VBScript的基本语法和功能。 ... [详细]
  • 本文详细解析了Python中的os和sys模块,介绍了它们的功能、常用方法及其在实际编程中的应用。 ... [详细]
  • 本文介绍了如何通过 Maven 依赖引入 SQLiteJDBC 和 HikariCP 包,从而在 Java 应用中高效地连接和操作 SQLite 数据库。文章提供了详细的代码示例,并解释了每个步骤的实现细节。 ... [详细]
  • C++构造函数与初始化列表详解
    本文深入探讨了C++中构造函数的初始化列表,包括赋值与初始化的区别、初始化列表的使用规则、静态成员初始化等内容。通过实例和调试证明,详细解释了初始化列表在对象创建时的重要性。 ... [详细]
  • Scala 实现 UTF-8 编码属性文件读取与克隆
    本文介绍如何使用 Scala 以 UTF-8 编码方式读取属性文件,并实现属性文件的克隆功能。通过这种方式,可以确保配置文件在多线程环境下的一致性和高效性。 ... [详细]
  • 本题通过将每个矩形视为一个节点,根据其相对位置构建拓扑图,并利用深度优先搜索(DFS)或状态压缩动态规划(DP)求解最小涂色次数。本文详细解析了该问题的建模思路与算法实现。 ... [详细]
  • 微软Exchange服务器遭遇2022年版“千年虫”漏洞
    微软Exchange服务器在新年伊始遭遇了一个类似于‘千年虫’的日期处理漏洞,导致邮件传输受阻。该问题主要影响配置了FIP-FS恶意软件引擎的Exchange 2016和2019版本。 ... [详细]
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社区 版权所有