8

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 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.

Is something like this possible?

coson
  • 8,301
  • 15
  • 59
  • 84

9 Answers9

13

Can you do it at runtime?

if (IntPtr.Size == 4)
  // 32 bit
else if (IntPtr.Size == 8)
  // 64 bit
marcc
  • 12,295
  • 7
  • 49
  • 59
11

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:

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.

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

if( Environment.GetEnvironmentVariable( "PROCESSOR_ARCHITEW6432" ) != null )
    // OS is 64-bit;
else
    // OS is 32-bit
Paul Alexander
  • 31,970
  • 14
  • 96
  • 151
3

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

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

and compile with:

 csc /d:VISTA64 file1.cs 

when compiling a 64 bit build.

Mehrdad Afshari
  • 414,610
  • 91
  • 852
  • 789
2

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
}
Jason Plank
  • 2,336
  • 5
  • 31
  • 40
Maurice Flanagan
  • 5,179
  • 3
  • 30
  • 37
2

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

string framework = (IntPtr.Size == 8) ? "Framework64" : "Framework";
Tamar
  • 2,016
  • 1
  • 15
  • 26
2

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
2

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
Yochai Timmer
  • 48,127
  • 24
  • 147
  • 185
1

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.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • In reality, compiling for "Any CPU" while in 64 bit mode prevents you from deploying to 32 bit mode, unless MS fixed this bug in VS2008... but last I tried, you had to specify 32 bit. – Neil N Jul 02 '09 at 20:14
  • @Neil: I do all my development on a 64-bit machine and never seen this issue. – Paul Alexander Jul 02 '09 at 20:26
  • From an MS MVP: "the problem is deployment. MSI files / Deployment projects need to be targeted at one platform. This means if you want x86/x64 deployment, you compile your DLL's once, then build two deployment projects. This pattern also holds true for Merge Modules and such." http://www.eggheadcafe.com/conversation.aspx?messageid=30991690&threadid=30991686 – Neil N Jul 02 '09 at 20:38
  • I wouls actually say the problem is wider than he states there.. but not everyone will run into these issues. – Neil N Jul 02 '09 at 20:38
  • That still only applies when you explicitly target specific platforms. The assemblies still target Any CPU. The MSI Project might specify 32/64-bit. In practice the MSI can simply be 32-bit and the app will still run 64-bit once installed. – Paul Alexander Jul 02 '09 at 22:12
0

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.

Andrew Hare
  • 344,730
  • 71
  • 640
  • 635