基于Windows平台判断当前操作系统的位数

检测操作系统位数

首先需要说明的是,win32应用程序可以运行于32位的操作系统,也可以运行于64位的操作系统。但是,win64应用程序只能应用于64位的操作系统。

当我们的win32应用程序需要判断当前的操作系统是32位还是64位时,我们可以采用以下代码进行判断。

示例代码

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

//关键代码:
BOOL IsWow64()
{

    typedef BOOL(WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL);

    LPFN_ISWOW64PROCESS fnIsWow64Process = nullptr;
    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.

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

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

    return bIsWow64;
}


//测试代码:
if (IsWow64())
{
    printf("is win32 on  64 bit os\n");
}
else
{
    printf("is win32 on  32 bit os\n");
}

运行环境:

wind7 64位操作系统 + win32 控制台程序

运行结果:

is win32 on  64 bit os

WOW64介绍

WOW64是x86仿真器,允许32位基于Windows的应用程序在64位Windows上无缝运行,WOW64随操作系统提供,无需明确启用。WOW64将32位应用程序与64位应用程序隔离,其中包括防止文件和注册表冲突。支持控制台,GUI和服务应用程序。32位应用程序可以通过调用IsWow64Process函数来检测它是否在WOW64下运行(如果是Windows 10,则使用IsWow64Process2)。

有关更多信息,请参阅微软官方介绍
https://docs.microsoft.com/zh-cn/windows/desktop/api/wow64apiset/nf-wow64apiset-iswow64process

更多参考资料:
https://blog.csdn.net/c_base_jin/article/details/80947204

猜你喜欢

转载自blog.csdn.net/xiao3404/article/details/81073429
今日推荐