8

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?

Steve
  • 11,763
  • 15
  • 70
  • 103

9 Answers9

7

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 PROCESSOR_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
  • 339,232
  • 124
  • 596
  • 636
3

Are you aware of Environment.Is64BitProcess? It determines whether the current running process is a 64-bit process.

Ilya Serbis
  • 21,149
  • 6
  • 87
  • 74
2

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
  • 21
  • 1
2

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

Walden Leverich
  • 4,416
  • 2
  • 21
  • 30
  • how corflags command line tool will help to identify an assembly is built for 32 bit/64 bit or *MSIL* ? – manu Sep 17 '14 at 11:46
  • I found the answer. http://stackoverflow.com/questions/270531/how-to-determine-if-a-net-assembly-was-built-for-x86-or-x64. Thanks for your contribution @Walden Leverich – manu Sep 17 '14 at 12:03
2

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
  • 43,293
  • 10
  • 75
  • 117
  • 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 Mar 11 '09 at 19:08
  • 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 Mar 11 '09 at 19:13
  • Hmmm, _WIN64 is apparently not working. The code is grayed out even though I'm compiling in 64 bit – Steve Mar 11 '09 at 19:15
  • _WIN64 is a c++ define, I don't think it gets defined for c# – Brian R. Bondy Mar 11 '09 at 19:25
  • _WIN64 gets defined by the compiler. But I thought you're using C++ not C# - my mistake. – Stefan Mar 11 '09 at 19:54
1

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

Community
  • 1
  • 1
Chris Hynes
  • 9,999
  • 2
  • 44
  • 54
1

Take a look at conditional methods.

MarkusQ
  • 21,814
  • 3
  • 56
  • 68
1

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.

Powerlord
  • 87,612
  • 17
  • 125
  • 175
0

You can use the Predefined Macros to check compilation type

   #if (_WIN64) 
        const bool IS_64 = true;
    #else
       const bool IS_64 = false;
    #endif
Yochai Timmer
  • 48,127
  • 24
  • 147
  • 185