检测 32 位或 64 位 Windows

发布于 2024-11-28 21:19:40 字数 96 浏览 2 评论 0原文

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(14

能怎样 2024-12-05 21:19:40

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

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

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

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

凡尘雨 2024-12-05 21:19:40

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

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.

吃兔兔 2024-12-05 21:19:40
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
}
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
}
他不在意 2024-12-05 21:19:40

如果您的app 是一个 32 位应用程序,如果您确实在 x64 操作系统上运行,否则它就是 32 位

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

π浅易 2024-12-05 21:19:40

使用 GetNativeSystemInfo函数。它获取 LPSYSTEM_INFO 参数来获取您想要的内容。

SYSTEM_INFO 结构:

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

Use GetNativeSystemInfo function. It gets a LPSYSTEM_INFO parameter to get what you want.

SYSTEM_INFO structure:

wProcessorArchitecture
The processor architecture of the installed operating system.

极致的悲 2024-12-05 21:19:40

You need to use GetNativeSystemInfo. Given that you expect this to work on a 32-bit operating system, you need to use LoadLibrary + GetProcAddress so 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.wProcessorArchitecture gives you the real processor type instead of the emulated one.

要走就滚别墨迹 2024-12-05 21:19:40

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

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

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

I personally am not sure about the usage of IsWow64Process since in MSDN in the description of the IsWow64Process there 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."

孤凫 2024-12-05 21:19:40
bool IsX64win()
{
    UINT x64test = GetSystemWow64DirectoryA(NULL, 0);
    if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)  return FALSE;
    else return TRUE;
}
bool IsX64win()
{
    UINT x64test = GetSystemWow64DirectoryA(NULL, 0);
    if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)  return FALSE;
    else return TRUE;
}
醉南桥 2024-12-05 21:19:40

或者,最简单的方法是使用 sizeof(int *) 检查整数指针的大小。

如果为 4,则为 32 位
如果是 8 那么它是 64 位

Alternatively the easiest way is to check the size of integer pointer with sizeof(int *).

If 4 then its 32 bit
If 8 then its 64 bit

漆黑的白昼 2024-12-05 21:19:40

您可以将 Windows 命令 systeminfo 作为程序中的进程来运行。

#include <stdlib.h>

system("systeminfo");

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

其输出为:系统类型:基于 x86 的 PC系统类型:基于 x64 的 PC

这可能是比其他人提供的解决方案更复杂的解决方案,但我认为我会将其添加为一种可能性。 (也许您还需要更多信息。)

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

#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

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

流心雨 2024-12-05 21:19:40

针对较新版本的 Windows 的解答

虽然有几个人和 Microsoft 文档建议IsWow64Process2 为此,我在我的文件中没有看到任何代码示例 研究。这就是为什么我想向社区贡献这个答案。

根据运行32位应用程序文档页面< /a>,微软建议使用IsWow64Process2(适用于 Windows 10)而不是 IsWow64Process

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

该功能适用​​于Windows 10版本1511(客户端)和Windows Server 2016及以上版本。

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

pProcessMachine 返回有关目标进程是否在 WOW64 模拟器下运行的信息,如果是,则它是什么类型的进程。

pNativeMachine 返回有关 Windows 主机体系结构的信息。

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

这是我为此目的编写的一个函数:

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;
}

这是上述函数的示例用法:

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;
}

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

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

Answer for Newer Versions of Windows

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

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

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

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

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

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.

pNativeMachine returns information about the architecture of the Windows host.

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.

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.

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

慈悲佛祖 2024-12-05 21:19:40

这是一个未记录的方法...

bool _Is64BitOS(void);


bool   _Is64BitOS(void) {
    unsigned int version = *((unsigned int)*)0x7FFE026C;
    unsigned int address = version == 10 ? 0x7FFE0308 : 0x7FFE0300;
//printf("Running %u-bit system",*((void*)*)address ? 32 : 64);

    return (*((void*)*)address ? false,true);
};

Here is an undocumented method ...

bool _Is64BitOS(void);


bool   _Is64BitOS(void) {
    unsigned int version = *((unsigned int)*)0x7FFE026C;
    unsigned int address = version == 10 ? 0x7FFE0308 : 0x7FFE0300;
//printf("Running %u-bit system",*((void*)*)address ? 32 : 64);

    return (*((void*)*)address ? false,true);
};
野侃 2024-12-05 21:19:40
 static bool is64bitOS()
   {
      SYSTEM_INFO si;
      GetSystemInfo(&si);

      if((si.wProcessorArchitecture & PROCESSOR_ARCHITECTURE_IA64)||(si.wProcessorArchitecture & PROCESSOR_ARCHITECTURE_AMD64)==64)
      {
         return true;
      }
      else
      {
         return false;
      }

   }
 static bool is64bitOS()
   {
      SYSTEM_INFO si;
      GetSystemInfo(&si);

      if((si.wProcessorArchitecture & PROCESSOR_ARCHITECTURE_IA64)||(si.wProcessorArchitecture & PROCESSOR_ARCHITECTURE_AMD64)==64)
      {
         return true;
      }
      else
      {
         return false;
      }

   }
给不了的爱 2024-12-05 21:19:40

一个简单的检查是,如果 EXE 未运行,则它是在 32 位计算机上运行的 64 位可执行文件。 64 位机器将始终运行 32 位可执行文件。

来自微软,

大多数为 32 位版本的 Windows 设计的程序都可以在
64 位版本的 Windows。值得注意的例外是许多防病毒软件
程序。

为 32 位版本的 Windows 设计的设备驱动程序不起作用
在运行 64 位版本 Windows 的计算机上。如果你想尝试
安装仅具有 32 位驱动程序的打印机或其他设备
可用,但它无法在 64 位版本的 Windows 上正常工作。

但是,在 Windows 中,您还可以检查 Program Files (x86) 文件夹是否存在,作为另一种简单的检查。无需花哨。

A simple check is if the EXE does not run, then it is a 64-bit executable running on a 32-bit machine. A 64-bit machine will always run a 32-bit executable.

From Microsoft,

Most programs designed for the 32-bit version of Windows will work on
the 64-bit version of Windows. Notable exceptions are many antivirus
programs.

Device drivers designed for the 32-bit version of Windows don't work
on computers running a 64-bit version of Windows. If you're trying to
install a printer or other device that only has 32-bit drivers
available, it won't work correctly on a 64-bit version of Windows.

In Windows, however, you can also check for the existence of the Program Files (x86) folder as another simple check. No need to get fancy.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文