C++ 检测 32 位或 64 位的 Windows

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7011071/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 16:20:53  来源:igfitidea点击:

Detect 32-bit or 64-bit of Windows

c++windows32bit-64bit

提问by Rahul

I want to detect whether the current Windows OS is 32-bit or 64-bit. How to achieve it using C++? I don't want processor type I want OS's bit type. This is because you can install 32-bit OS on 64-bit processor.

我想检测当前的 Windows 操作系统是 32 位还是 64 位。如何使用 C++ 实现它?我不想要处理器类型我想要操作系统的位类型。这是因为您可以在 64 位处理器上安装 32 位操作系统。

采纳答案by Bo Persson

The function to call is IsWow64Processor IsWow64Process2. It tells your 32-bit application if it is running on a 64 bit Windows.

要调用的函数是IsWow64Processor IsWow64Process2。它会告诉您的 32 位应用程序是否在 64 位 Windows 上运行。

If the program is compiled for 64 bits, it will already know.

如果程序是为 64 位编译的,它就已经知道了。

回答by sharptooth

If your code is 64-bit and running, then Windows is 64-bit - nothing to check. If your process is 32-bit call IsWow64Process()- 32-bit processes run in WOW64 on 64-bit Windows and without WOW64 otherwise.

如果您的代码是 64 位并且正在运行,则 Windows 是 64 位 - 无需检查。如果您的进程是 32 位调用IsWow64Process()- 32 位进程在 64 位 Windows 上以 WOW64 运行,否则不使用 WOW64。

回答by Survarium

bool getWindowsBit(bool & isWindows64bit)
{
#if _WIN64

    isWindows64bit =  true;
    return true;

#elif _WIN32

    BOOL isWow64 = FALSE;

    //IsWow64Process is not available on all supported versions of Windows.
    //Use GetModuleHandle to get a handle to the DLL that contains the function
    //and GetProcAddress to get a pointer to the function if available.

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

    if(fnIsWow64Process)
    {
        if (!fnIsWow64Process(GetCurrentProcess(), &isWow64))
            return false;

        if(isWow64)
            isWindows64bit =  true;
        else
            isWindows64bit =  false;

        return true;
    }
    else
        return false;

#else

    assert(0);
    return false;

#endif
}

回答by Necrolis

you can use IsWow64Processif your app is a 32 bit app, if it's true you are running on an x64 OS, else it's 32bit

如果您的应用程序是 32 位应用程序,您可以使用IsWow64Process,如果您在 x64 操作系统上运行是真的,否则它是 32 位

回答by Hans Passant

You need to use GetNativeSystemInfo. Given that you expect this to work on a 32-bit operating system, you need to use LoadLibrary+ GetProcAddressso that you can deal with this function not being available. So if that fails, you know it is a 32-bit operating system. If not, SYSTEM_INFO.wProcessorArchitecturegives you the real processor type instead of the emulated one.

您需要使用GetNativeSystemInfo. 鉴于您希望它在 32 位操作系统上运行,您需要使用LoadLibrary+GetProcAddress以便您可以处理此功能不可用的情况。因此,如果失败,您就知道它是一个 32 位操作系统。如果不是,SYSTEM_INFO.wProcessorArchitecture则为您提供真实的处理器类型而不是模拟的处理器类型。

回答by Hayri U?ur Koltuk

Use GetNativeSystemInfofunction. It gets a LPSYSTEM_INFOparameter to get what you want.

使用GetNativeSystemInfo功能。它得到一个LPSYSTEM_INFO参数来获得你想要的东西。

SYSTEM_INFOstructure:

SYSTEM_INFO结构体:

wProcessorArchitecture
The processor architecture of the installed operating system.

wProcessorArchitecture
已安装操作系统的处理器架构。

回答by sealz

You can run the windows command systeminfoas a process in your program.

您可以将 windows 命令systeminfo作为程序中的一个进程运行。

#include <stdlib.h>

system("systeminfo");

One of the returning categories is System Type.

返回类别之一是系统类型。

Its output reads: System Type: x86-based PC, or System Type: x64-based PC

其输出为:System Type: x86-based PC, 或System Type: x64-based PC

This may be a more complicated solution than the one provided by others but I thought I would add it as a possibility. (Maybe you are after additional info as well.)

这可能是一个比其他人提供的解决方案更复杂的解决方案,但我想我会添加它作为一种可能性。(也许您也在寻找其他信息。)

回答by user3759036

Here is another way: GetSystemWow64Directory- "Retrieves the path of the system directory used by WOW64. This directory is not present on 32-bit Windows." and "On 32-bit Windows, the function always fails, and the extended error is set to ERROR_CALL_NOT_IMPLEMENTED."

这是另一种方式:GetSystemWow64Directory- “检索 WOW64 使用的系统目录的路径。此目录在 32 位 Windows 上不存在。” 和“在 32 位 Windows 上,该函数总是失败,并且扩展错误设置为ERROR_CALL_NOT_IMPLEMENTED。”

I personally am not sure about the usage of IsWow64Processsince in MSDN in the description of the IsWow64Processthere is the text "Note that this technique is not a reliable way to detect whether the operating system is a 64-bit version of Windows because the Kernel32.dll in current versions of 32-bit Windows also contains this function."

我个人不太确定IsWow64Process在 MSDN 的描述中的使用,因为IsWow64Process有文本“请注意,此技术不是检测操作系统是否为 64 位版本的 Windows 的可靠方法,因为 Kernel32.dll在当前版本的 32 位 Windows 中也包含此功能。”

回答by TheMaster Moh

bool IsX64win()
{
    UINT x64test = GetSystemWow64DirectoryA(NULL, 0);
    if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)  return FALSE;
    else return TRUE;
}

回答by Xitalogy

Answer for Newer Versions of Windows

较新版本的 Windows 的答案

While several people, and the Microsoft Docs, have suggested IsWow64Process2for this, I have not seen any code examples in my research. That is why I wanted to contribute this answer to the community.

虽然有几个人和 Microsoft Docs为此建议使用IsWow64Process2,但我在研究中没有看到任何代码示例。这就是为什么我想将这个答案贡献给社区。

According to the running 32 bit applications documentation page, Microsoft recommends using the IsWow64Process2for Windows 10 instead of IsWow64Process:

根据正在运行的 32 位应用程序文档页面,Microsoft 建议使用IsWow64Process2for Windows 10 而不是IsWow64Process

A 32-bit application can detect whether it is running under WOW64 by calling the IsWow64Process function (use IsWow64Process2 if targeting Windows 10).

32 位应用程序可以通过调用 IsWow64Process 函数来检测它是否在 WOW64 下运行(如果面向 Windows 10,则使用 IsWow64Process2)。

This function works on Windows 10 version 1511 (client) and Windows Server 2016 and above.

此功能适用于 Windows 10 版本 1511(客户端)和 Windows Server 2016 及更高版本。

This has two parameters via which information is returned: pProcessMachine and pNativeMachine. Both return Image File Machine Constants

这有两个返回信息的参数:pProcessMachine 和 pNativeMachine。两者都返回图像文件机器常量

pProcessMachine returns information about whether the target process is running under the WOW64 emulator or not, and if it is, what kind of process it is.

pProcessMachine 返回有关目标进程是否在 WOW64 模拟器下运行的信息,如果是,它是哪种进程。

pNativeMachine returns information about the architecture of the Windows host.

pNativeMachine 返回有关 Windows 主机架构的信息。

Using both of these return values, one can determine if Windows is 32 bit or 64 bit (which is what the OP asked), and whether the process is running under WOW64 as well as whether the process is 32 or 64 bit.

使用这两个返回值,可以确定 Windows 是 32 位还是 64 位(这是 OP 要求的),以及该进程是否在 WOW64 下运行以及该进程是 32 位还是 64 位。

Here is a function I wrote for these purposes:

这是我为这些目的编写的函数:

BOOL getBits(BOOL& windowsIs32Bit, BOOL& isWOW64, BOOL& processIs32Bit)
{
  USHORT ProcessMachine;
  USHORT NativeMachine;

  if (!IsWow64Process2(GetCurrentProcess(), &ProcessMachine, &NativeMachine)) {
    std::cerr << "IsWOW64Process2 returned FALSE (failed). GetLastError returned: " << GetLastError() << std::endl;
    return FALSE;
  }

  if (ProcessMachine == IMAGE_FILE_MACHINE_UNKNOWN) {
    isWOW64 = FALSE;

    if (NativeMachine == IMAGE_FILE_MACHINE_IA64 || NativeMachine == IMAGE_FILE_MACHINE_AMD64 || NativeMachine == IMAGE_FILE_MACHINE_ARM64) {
      windowsIs32Bit = FALSE;
      processIs32Bit = FALSE;

      return TRUE;
    }

    if (NativeMachine == IMAGE_FILE_MACHINE_I386 || NativeMachine == IMAGE_FILE_MACHINE_ARM) {
      windowsIs32Bit = TRUE;
      processIs32Bit = TRUE;

      return TRUE;
    }

    std::cerr << "Unknown Windows Architecture." << std::endl;
    return FALSE;
  }

  windowsIs32Bit = FALSE;
  isWOW64 = TRUE;
  processIs32Bit = TRUE;

  return TRUE;
}

Here is an example usage of the above function:

以下是上述函数的示例用法:

int main() {

  BOOL windowsIs32Bit;
  BOOL isWOW64;
  BOOL processIs32Bit;

  if (!getBits(windowsIs32Bit, isWOW64, processIs32Bit)) {
    return -1;
  }

  std::cout << (windowsIs32Bit ? "Windows is 32 bit" : "Windows is 64 bit") << std::endl;
  std::cout << (isWOW64 ? "This process *is* running under WOW64" : "This process is *not* running under WOW64") << std::endl;
  std::cout << (processIs32Bit ? "This process is 32 bit" : "This process is 64 bit") << std::endl;

  return 0;
}

I could only test two scenarios of the above code because I only have 64 bit Windows on a 64 bit machine. I do not have a 32 bit machine nor 32 bit Windows, nor do I have any ARM machines. If someone can test other scenarios, I would appreciate some feedback about whether the design I have done works for them.

我只能测试上述代码的两个场景,因为我在 64 位机器上只有 64 位 Windows。我没有 32 位机器,也没有 32 位 Windows,也没有任何 ARM 机器。如果有人可以测试其他场景,我会很感激一些关于我所做的设计是否适合他们的反馈。

I wrote an articlethat goes into greater depth explaining how the above code works.

我写了一篇文章,更深入地解释了上述代码的工作原理。