检测 32 位或 64 位 Windows
我想检测当前的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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
要调用的函数是
IsWow64Process
或IsWow64Process2
。它告诉您的 32 位应用程序是否在 64 位 Windows 上运行。如果程序是为 64 位编译的,它就已经知道了。
The function to call is
IsWow64Process
orIsWow64Process2
. 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.
如果您的代码是 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.如果您的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
使用
GetNativeSystemInfo
函数。它获取 LPSYSTEM_INFO 参数来获取您想要的内容。
SYSTEM_INFO
结构:Use
GetNativeSystemInfo
function. It gets aLPSYSTEM_INFO
parameter to get what you want.SYSTEM_INFO
structure:您需要使用
获取NativeSystemInfo
。鉴于您希望它在 32 位操作系统上运行,您需要使用LoadLibrary
+GetProcAddress
< /a> 这样你就可以处理这个功能不可用的情况。因此,如果失败,您就知道它是 32 位操作系统。如果没有,SYSTEM_INFO.wProcessorArchitecture
将为您提供真实的处理器类型,而不是模拟的处理器类型。You need to use
GetNativeSystemInfo
. Given that you expect this to work on a 32-bit operating system, you need to useLoadLibrary
+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.这是另一种方法: 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 theIsWow64Process
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."或者,最简单的方法是使用
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
您可以将 Windows 命令
systeminfo
作为程序中的进程来运行。返回的类别之一是系统类型。
其输出为:
系统类型:基于 x86 的 PC
或系统类型:基于 x64 的 PC
这可能是比其他人提供的解决方案更复杂的解决方案,但我认为我会将其添加为一种可能性。 (也许您还需要更多信息。)
You can run the windows command
systeminfo
as a process in your program.One of the returning categories is System Type.
Its output reads:
System Type: x86-based PC
, orSystem 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.)
针对较新版本的 Windows 的解答
虽然有几个人和 Microsoft 文档建议IsWow64Process2 为此,我在我的文件中没有看到任何代码示例 研究。这就是为什么我想向社区贡献这个答案。
根据运行32位应用程序文档页面< /a>,微软建议使用IsWow64Process2(适用于 Windows 10)而不是 IsWow64Process:
该功能适用于Windows 10版本1511(客户端)和Windows Server 2016及以上版本。
它有两个返回信息的参数:pProcessMachine 和 pNativeMachine。两者都返回图像文件机器常量
pProcessMachine 返回有关目标进程是否在 WOW64 模拟器下运行的信息,如果是,则它是什么类型的进程。
pNativeMachine 返回有关 Windows 主机体系结构的信息。
使用这两个返回值,可以确定 Windows 是 32 位还是 64 位(这是 OP 所要求的),以及进程是否在 WOW64 下运行以及进程是 32 位还是 64 位。
这是我为此目的编写的一个函数:
这是上述函数的示例用法:
我只能测试上述代码的两个场景,因为我在 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:
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:
Here is an example usage of the above function:
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.
这是一个未记录的方法...
Here is an undocumented method ...
一个简单的检查是,如果 EXE 未运行,则它是在 32 位计算机上运行的 64 位可执行文件。 64 位机器将始终运行 32 位可执行文件。
来自微软,
但是,在 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,
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.