0

How would I go about checking if the Windows operating system is 32 or 64 bit during runtime? I would like to compile the application once for 32-bit, but have it be used for both versions, so using macros is out of the question.

From what I can tell, I'm supposed to use QSysInfo in order to determine this, but everything in the documentation looks foreign; I have no clue what I'm supposed to check or which value I should be checking for.

If someone could clarify and give an example on how to do this, or provide a more effective alternative, it would be greatly appreciated!

Griffort
  • 1,174
  • 1
  • 10
  • 26
  • 1
    Just curious, why do you want that? – HolyBlackCat Mar 15 '18 at 00:50
  • @HolyBlackCat To boil it down, the program runs a "sub-program", and I would like the user to be able to customize whether the 32 or 64 bit version of the sub-program is used. However, if the user is using a 32 bit computer, I would like the program to detect that and automatically use the 32 bit sub-program without giving them an option. (And as mentioned in the main post, I would like for users that even use the 32 bit main program to have the option on a 64 bit computer.) – Griffort Mar 15 '18 at 00:57
  • @Griffort Why not just let the user configure the actual path to the sub-program executable? That way, they can decide whether they want to use the 32bit or 64bit executable. On a 32bit Windows, there will only be a 32bit executable. Nothing for your program to detect at runtime, just run whichever executable the user chose. – Remy Lebeau Mar 15 '18 at 02:15

2 Answers2

1

You can use IsWow64Process(). It checks if the application is running on a 64-bit Windows.

For Example:

BOOL Is64BitOS()
{
   BOOL bIs64BitOS = FALSE;

   // We check if the OS is 64 Bit
   typedef BOOL (WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL); 

   LPFN_ISWOW64PROCESS
      fnIsWow64Process = (LPFN_ISWOW64PROCESS)GetProcAddress(
      GetModuleHandle("kernel32"),"IsWow64Process");

   if (NULL != fnIsWow64Process)
   {
      if (!fnIsWow64Process(GetCurrentProcess(),&bIs64BitOS))
      {
         //error
      }
   }
   return bIs64BitOS;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 2
    More accurately, `IsWow64Process()` checks if an application is a **32bit process** running on a 64bit Windows. This code will return FALSE if called in a 64bit process. At the very least, there should be an `#ifdef` to return TRUE for a 64-bit compilation and to call `IsWow64Process()` only for a 32-bit compilation. – Remy Lebeau Mar 15 '18 at 02:19
-1

This used to work for C#, your mileage may vary...

[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool IsWow64Process(IntPtr hProcess, out bool wow64Process);

public static bool Is64BitOperatingSystem()
{
    // 64-bit programs run only on Win64
    if (IntPtr.Size == 8)
    {
        return true;
    }
    else
    {
        // 32-bit programs run on both 32-bit and 64-bit Windows
        // Detect whether the current process is a 32-bit process running on a 64-bit system.
        bool flag;
        return ((doesWin32MethodExist("kernel32.dll", "IsWow64Process") &&
                    IsWow64Process(GetCurrentProcess(), out flag)) && flag);
    }
}


 private static bool doesWin32MethodExist(string moduleName, string methodName)
 {
      IntPtr moduleHandle = GetModuleHandle(moduleName);
      if (moduleHandle == IntPtr.Zero)
      {
          return false;
      }
      return (GetProcAddress(moduleHandle, methodName) != IntPtr.Zero);
 }
programmerj
  • 1,634
  • 18
  • 29