views:

1207

answers:

7

Good Day,

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?

TIA, coson

+1  A: 

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
+1  A: 

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
+4  A: 

Can you do it at runtime?

if (IntPtr.Size == 4)
  // 32 bit
else if (IntPtr.Size == 8)
  // 64 bit
marcc
+1  A: 

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.

Paul Alexander
+1  A: 

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
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
@Neil: I do all my development on a 64-bit machine and never seen this issue.
Paul Alexander
Neil N
I wouls actually say the problem is wider than he states there.. but not everyone will run into these issues.
Neil N
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
A: 

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 }

Mo Flanagan
A: 

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