如何在C ++上检测Windows 32或64位?

时间:2014-05-16 13:44:27

标签: c++ windows winapi visual-c++

如何在C ++上检测Windows 32或64位? 我在.Net中看到很多例子,但我需要C ++。 IsWow64Process()也不适用于我,因为“如果进程在32位Windows下运行,则该值设置为FALSE。如果进程是在64位Windows下运行的64位应用程序,则值为也设为FALSE“

如果我在32位操作系统下有32位进程,那我就错了 如果我在64位操作系统下有64位进程,我就有错误

但我不关心进程位我需要OS位

4 个答案:

答案 0 :(得分:13)

用于检测有关底层系统的信息的Win32 API函数是GetNativeSystemInfo。调用该函数并读取该函数填充的SYSTEM_INFO结构的wProcessorArchitecture成员。

虽然实际上可以使用IsWow64Process来检测这一点。如果您调用IsWow64Process并返回TRUE,则表示您正在使用64位系统运行。否则,返回FALSE。然后你只需要测试一个指针的大小。 32位指针表示32位系统,64位指针表示64位系统。实际上,您可以从编译器提供的条件中获取信息,具体取决于您使用的编译器,因为指针的大小在编译时是已知的。

Raymond Chen在blog article中描述了这种方法。他帮助我包含了我在这里重现的代码:

BOOL Is64BitWindows()
{
#if defined(_WIN64)
 return TRUE;  // 64-bit programs run only on Win64
#elif defined(_WIN32)
 // 32-bit programs run on both 32-bit and 64-bit Windows
 // so must sniff
 BOOL f64 = FALSE;
 return IsWow64Process(GetCurrentProcess(), &f64) && f64;
#else
 return FALSE; // Win64 does not support Win16
#endif
}

答案 1 :(得分:0)

在Windows,C ++上,您可以使用此代码 - 它确定正在运行的窗口类型

我已经在windows xp,windows 7 64& 32位,Windows 8 64& 32,windows 10

(抱歉我的英语不好)

#include <windows.h>
#include <iostream>
#include <tchar.h>

BOOL IsWow64()
{
    BOOL bIsWow64 = 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.

    typedef BOOL (WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL);
    LPFN_ISWOW64PROCESS fnIsWow64Process;
    fnIsWow64Process = (LPFN_ISWOW64PROCESS) GetProcAddress( GetModuleHandle(TEXT("kernel32")),"IsWow64Process");

    if(NULL != fnIsWow64Process)
    {
        if (!fnIsWow64Process(GetCurrentProcess(),&bIsWow64))
        {
            //handle error
        }
    }
    return bIsWow64;
}

int main( void )
{
    if(IsWow64())
        printf(TEXT("The process is running under Windows 64.\n"));
    else
        printf(TEXT("The process is NOT running under Windows 64.\n"));

    return 0;
}

答案 2 :(得分:0)

另一种方法是检查系统分区上是否存在“程序文件(x86)”根文件夹。它会出现在x64 Windows安装中,而不是出现在x86安装中。

答案 3 :(得分:0)

GetSystemWow64DirectoryW

检索 WOW64 使用的系统目录的路径。此目录在 32 位 Windows 上不存在。