使用C ++在Windows中获取OSVersion [英] Get OSVersion in Windows using C++

查看:407
本文介绍了使用C ++在Windows中获取OSVersion的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须获取我的Windows8系统的OSVersion(版本应为NT 6.2)才能在C ++应用程序中使用.我尝试使用 GetVersion 函数调用.但是它返回了像602931718这样的原始值.是否可以通过某种方式获取此处列出的版本,或者如何转换此原始版本值变成可读的形式?

I have to get the OSVersion of my Windows8 System (version should be NT 6.2) to use in a C++ application. I tried using GetVersion function call. but it returned me a raw value like 602931718. Is there some way by which I can get the versions as listed here or how can I convert this raw value to a readable form?

推荐答案

您是否看过 OSVERSIONINFOEX 结构?

Have you looked at GetVersionEx() function and OSVERSIONINFOEX structure?

可能的用法:

void print_os_info()
{
    OSVERSIONINFOEX info;
    ZeroMemory(&info, sizeof(OSVERSIONINFOEX));
    info.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);

    GetVersionEx(&info);

    printf("Windows version: %u.%u\n", info.dwMajorVersion, info.dwMinorVersion);
}

我不明白,NT是什么意思.根据 MSDN :

I don't understand, what do you mean by NT. According to MSDN:

从Windows XP开始,所有版本都是隐式的NT版本.如果要针对服务器版本进行测试,请检查info.wProductType:

Since Windows XP, all versions are implicitly NT versions. If you want to test against Server versions, check value of info.wProductType:

if(info.dwMajorVersion == 6)
{
    if (info.dwMinorVersion == 0)
    {
        if (info.wProductType == VER_NT_WORKSTATION)
            //Windows Vista;
        else
            //Windows Server 2008
    }
    else if (info.dwMinorVersion == 1)
    {
        if (info.wProductType == VER_NT_WORKSTATION)
            //Windows 7
        else
            //Windows Server 2008 R2
    }
    else if (...) //etc...
}

还有另一件事:您还可以检查info.dwBuildNumber的值.允许的值之一是VER_PLATFORM_WIN32_NT.

And one more thing: you can also check value of info.dwBuildNumber. One of allowed values is VER_PLATFORM_WIN32_NT.

这篇关于使用C ++在Windows中获取OSVersion的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆