C# Directive to indicate 32-bit or 64-bit build

后端 未结 9 1464
你的背包
你的背包 2020-12-16 04:04

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:

if (32-bit Vista)
    // set a         


        
相关标签:
9条回答
  • 2020-12-16 05:03

    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

    if (IntPtr.Size == 4)
    {
        //32 bit
    }
    else if (IntPtr.Size == 8)
    {
        //64 bit
    }
    else
    {
        //the future
    }
    
    0 讨论(0)
  • 2020-12-16 05:04

    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.

    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:

    #if BUILD64
    // 64 Bit version
    // do stuff here
    #endif
    
    #if BUILD32
    // 32 Bit version
    // do different stuff here
    #endif
    
    0 讨论(0)
  • 2020-12-16 05:05

    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.

    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.

    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.

    0 讨论(0)
提交回复
热议问题