19

Possible Duplicate:
How do I tell if my application is running as a 32 or 64 bit application?

Hi

I have an app in c# (Framework 3.5 SP1) and I need to load an unmanaged assembly at run time but there are two different versions, one for x86 and another for x64, so I need to know at run time in which mode is the app running

I have seen this POST but it's for C++, is there an easier way to do it in C#? or how can I do this in C#?

Thanks

Community
  • 1
  • 1
DkAngelito
  • 1,147
  • 1
  • 13
  • 30
  • 5
    See: [c# - How to know a process is 32-bit or 64-bit programmatically](http://stackoverflow.com/questions/1953377/how-to-know-a-process-is-32-bit-or-64-bit-programmatically). If you upgrade to .NET 4.0, you can also use the [`Environment.Is64BitProcess`](http://msdn.microsoft.com/en-us/library/system.environment.is64bitprocess.aspx) property – Ani Feb 20 '11 at 02:44
  • 2
    @Ani: That's not a dupe, because it asks about detecting bitness of some *other* application. But this is a dupe of http://stackoverflow.com/questions/266082/how-do-i-tell-if-my-application-is-running-as-a-32-or-64-bit-application – Ben Voigt Feb 20 '11 at 02:54
  • 1
    @Ben Voigt: I don't understand; that questions covers this too. The top answer on that question starts off by mentioning the `IntPtr.Size` technique. – Ani Feb 20 '11 at 03:22
  • @Ani: You're right that the answers to that question include answers to this one. – Ben Voigt Feb 20 '11 at 03:40
  • Fun fact: carefully-crafted x86 machine code can branch or not depending on whether it's being decoded as x86-64 or x86-32. See [this polyglot machine-code Q&A](http://stackoverflow.com/questions/38063529/x86-32-x86-64-polyglot-machine-code-fragment-that-detects-64bit-mode-at-run-ti). – Peter Cordes Dec 05 '16 at 18:10

4 Answers4

28

You can check whether IntPtr.Size is 4 or 8.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
14

Use the System.Environment.Is64BitOperatingSystem property, introduced in .NET 4.0. According to MSDN:

Determines whether the current operating system is a 64-bit operating system, it returns true if the operating system is 64-bit; otherwise, false.

Abel
  • 56,041
  • 24
  • 146
  • 247
Alan Turing
  • 2,482
  • 17
  • 20
8

You can simply check the size of an IntPtr via IntPtr.Size to see what your process is running as:

  • If it is 8 bytes, then you are running as an x64 process.
  • If it is 4 bytes, then you are running as an x86 process..

In .NET you can set your Platform Target in your project properties to Any CPU to automatically run your application as x64 on an x64 OS and x86 on an x86 OS with the same binary.


Your application could run in x86 even on an x64 OS since a process that starts it could be running on WOW64 emulation and it starts your process. Here are some additional ways to run your x64 process as an x86 process on an x64 OS.

Brian R. Bondy
  • 339,232
  • 124
  • 596
  • 636
  • Although your explanation is wrong (it doesn't tell you the architecture you're on), this will work. Note that changing the CPU property when building a C# DLL will just cause crashes if there's a mismatch, it won't control the bitness. The main application could be loading in 32-bit mode even on a 64-bit OS if it needs other 32-bit only components. – Ben Voigt Feb 20 '11 at 02:57
  • Replaced the word architecture with "as an x__ process" and fixed to explain about wow64 emulation a bit, thx. – Brian R. Bondy Feb 20 '11 at 11:51
-4

This MSDN blog post is a good start. GetEnvironmentVariable("PROCESSOR_ARCHITECTURE").ToString() also see the GetPlatform code in Mono Paint.

chx
  • 11,270
  • 7
  • 55
  • 129
  • This tells you if the underlying platform is 64-bit capable, not whether your program is running in 64-bit mode. – Ben Voigt Feb 20 '11 at 02:55
  • It is not save to rely upon environment variables, PROCESSOR_ARCHITECTURE can be changed or be other than expected in the future OS'es – Alan Turing Feb 20 '11 at 02:56