tags:

views:

2069

answers:

7

Hi I need for my program to determine if it was compiled for 32 bit windows or 64 bit/any CPU. I need to choose a different COM server flag depending on the compilation options. Any ideas?

+1  A: 

You want the CORFLAGS command-line app from Microsoft. http://msdn.microsoft.com/en-us/library/ms164699(VS.80).aspx

WaldenL
+1  A: 

You could do this based on setting compile time constants and doing #ifs based on those to set a value that you can check to see which platform the application was compiled for. See http://stackoverflow.com/questions/524196/-net-target-platform-processor-at-compile-time.

The easiest other way is to check the IntPtr.Size property to see if it's 4 (32 bit), or 8 (64 bit).

Chris Hynes
+1  A: 

Take a look at conditional methods.

MarkusQ
+1  A: 

You can use the IsWow64Process function to determine at runtime whethehr your COM server is a x64 or win32 process.

If you just want to know this during compile time: the compiler sets the _WIN64 macro.

Stefan
From the docs: "If the application is a 64-bit application running under 64-bit Windows, the Wow64Process parameter is set to FALSE."... how does that help?
Chris Hynes
IsWow64Process will not tell you if you are running a 32-bit or a 64bit application. On 32-bit machines it will tell you FALSE when run, and on a 64-bit machine it will tell you TRUE for a 32-bit application.
Brian R. Bondy
Hmmm, _WIN64 is apparently not working. The code is grayed out even though I'm compiling in 64 bit
Steve
_WIN64 is a c++ define, I don't think it gets defined for c#
Brian R. Bondy
_WIN64 gets defined by the compiler. But I thought you're using C++ not C# - my mistake.
Stefan
+4  A: 

Checking at run time if you are running a 64-bit application:

To see whether your process is 64-bit or 32-bit you can simply check the IntPtr.Size or any other pointer type. If you get 4 then you are running on 32-bit. If you get 8 then you are running on 64-bit.

What type of computer is your process running as:

You can check the ARCHITECTURE environment variable to see if you are running on an x64, ia64 or x86 machine.

Is your process running under emulation:

The Win32 API IsWow64Process will tell you if you are running a 32-bit application under x64. Wow64 means windows32 on windows64.

Contrary to what other people have posted: IsWow64Process will not tell you if you are running a 32-bit or a 64bit application. On 32-bit machines it will tell you FALSE for 32-bit applications when run, and on a 64-bit machine it will tell you TRUE for a 32-bit application. It only tells you if your application is running under emulation.

Brian R. Bondy
Hi Brian, _WIN64 seems to not be defined in vs2008. Any ideas?
Steve
I updated my answer, sorry at first I thought you said c++. I updated it with C# information.
Brian R. Bondy
+1  A: 

While it seems like an odd route to go, you can tell whether you're running in 32-bit (or 64-bit in WOW64) or 64-bit .NET code by checking IntPtr.Size.

On 32-bit/WOW64, IntPtr.Size is 4.

On 64-bit, IntPtr.Size is 8.

Source: Migrating 32-bit Managed Code to 64-bit Managed Code on MSDN.

R. Bemrose
A: 

use GetBinaryType api function and you will have the type of the file

this is the link to the api on msdn http://msdn.microsoft.com/en-us/library/aa364819(VS.85).aspx

the results are:

SCS_32BIT_BINARY = 32 bit exe

SCS_64BIT_BINARY = 64 bit exe

SCS_DOS_BINARY = DOS

SCS_OS216_BINARY = OS/2

SCS_WOW_BINARY = 16 bit

SCS_POSIX_BINARY = POSIX based

SCS_PIF_BINARY = PIF file that execute on DOS

Farmer