2

This is a reasonably popular question, but all the answers pretty much say: "Use IsWow64Process function". The problem with that is it returns FALSE if the application is 64-bit. I want a solution that works regardless of my app's bitness.

Violet Giraffe
  • 32,368
  • 48
  • 194
  • 335
  • possible duplicate of [How to detect Windows 64-bit platform with .NET?](http://stackoverflow.com/questions/336633/how-to-detect-windows-64-bit-platform-with-net) – Ahmed Masud Jul 28 '13 at 17:43
  • 3
    @AhmedMasud: do you know the difference between .NET and C++ / WinAPI? – Violet Giraffe Jul 28 '13 at 17:50
  • 1
    This does fall in the category of "how can you not know". You make a very specific configuration change in your project to target x64. Making another one to define a macro is not any different and is eminently portable. Using something like sizeof(void*) in your code is not exactly a hack either, the compiler sorts this out at compile time so can optimize away chunks of code. Or use a predefined compiler macro, like _WIN64. – Hans Passant Jul 28 '13 at 17:50
  • did you go read all the answers there?? there is a NON .NET C++/WinAPI answer listed in the replies to that question :P – Ahmed Masud Jul 28 '13 at 17:52
  • @AhmedMasud: I don't like the answer relying on environmental variables either, they might be deleted. – Violet Giraffe Jul 28 '13 at 17:54
  • Okay then I have another reference for you: http://blogs.msdn.com/b/oldnewthing/archive/2005/02/01/364563.aspx and I am retracting the close vote the issues are nicely discussed there. – Ahmed Masud Jul 28 '13 at 17:56
  • 1
    @AhmedMasud: Yep, thanks, figured it out already. Funny how I didn't realize it immediately :) – Violet Giraffe Jul 28 '13 at 18:00
  • Add your answer below I'll vote it. – Ahmed Masud Jul 28 '13 at 18:01

1 Answers1

5

Create a function to call the Win32 API function IsWow64Process() for 32bit process and return true for 64bit process.

bool is_64bit(void)
{
#if defined(_WIN64)
    return true;  // 64-bit programs run only on Win64
#elif defined(_WIN32)
    BOOL f64 = FALSE;
    return IsWow64Process(GetCurrentProcess(), &f64) && f64;
#endif
}
IInspectable
  • 46,945
  • 8
  • 85
  • 181
cdmh
  • 3,294
  • 2
  • 26
  • 41
  • 2
    Your `#if` ... `#elif` ... `#endif` does not contain an `#else` clause. Is this intentional? In this particular case the compiler will issue an error if none of the preprocessor symbols are defined. In general I prefer to be explicit and use an `#error` preprocessor directive in cases like this. – IInspectable Aug 07 '13 at 15:49