使用 C++ 在 Windows 中获取 OSVersion

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29944745/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 21:15:23  来源:igfitidea点击:

Get OSVersion in Windows using C++

c++windowsversion

提问by Hymanzz

I have to get the OSVersionof my Windows8System (version should be NT 6.2) to use in a C++ application. I tried using GetVersionfunction call. but it returned me a raw value like 602931718. Is there some way by which I can get the versions as listed hereor how can I convert this raw value to a readable form?

我必须得到OSVersion我的Windows8系统(版本应该是NT 6.2)才能在 C++ 应用程序中使用。我尝试使用GetVersion函数调用。但它返回了一个原始值,如602931718. 有什么方法可以获取此处列出的版本,或者如何将此原始值转换为可读形式?

采纳答案by Mateusz Grzejek

Have you looked at GetVersionEx()function and OSVERSIONINFOEXstructure?

你看过GetVersionEx()函数和OSVERSIONINFOEX结构吗?

Possible usage:

可能的用法:

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

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

我不明白,你什么意思NT。根据MSDN

Version table

版本表

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

从 Windows XP 开始,所有版本都是隐式NT版本。如果要针对服务器版本进行测试,请检查以下值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...
}

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

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

回答by Vesa P.

Note that functions GetVersion and GetVersionEx have been deprecated. This is also reflected in what their doc. pages say (this one copied from GetVersion):

请注意,函数 GetVersion 和 GetVersionEx 已被弃用。这也反映在他们的文档中。页面说(这是从 GetVersion 复制的):

"With the release of Windows 8.1, the behavior of the GetVersion API has changed in the value it will return for the operating system version. The value returned by the GetVersion function now depends on how the application is manifested."

“随着 Windows 8.1 的发布,GetVersion API 的行为在它将为操作系统版本返回的值方面发生了变化。GetVersion 函数返回的值现在取决于应用程序的显示方式。”

回答by Mohit Dabas

I have compiled This programmer With Dev-C++ on Windows 7.The Programme is same as the Ist answer on This Question But On Dev-C++ The OSVERSIONINFOEX info requires typecasting when called in GetVersionEx().So Here Is The Code

我已经在 Windows 7 上用 Dev-C++ 编译了这个程序员。 该程序与这个问题的 Ist 答案相同,但在 Dev-C++ 中 OSVERSIONINFOEX 信息在 GetVersionEx() 中调用时需要类型转换。所以这里是代码

#include<windows.h>
#include<stdio.h>
int main()
{
     OSVERSIONINFOEX info;
     ZeroMemory(&info, sizeof(OSVERSIONINFOEX));
     info.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
     GetVersionEx((LPOSVERSIONINFO)&info);//info requires typecasting

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

}

回答by Bill Moore

If you want to make it more convenient, you could try converting to a float instead of checking two integers:

如果你想让它更方便,你可以尝试转换为浮点数而不是检查两个整数:

double GetOsVersion() {
    OSVERSIONINFOEX info;
    ZeroMemory(&info, sizeof(OSVERSIONINFOEX));
    info.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
    GetVersionEx(&info);

    double version;

    version = info.dwMajorVersion 
            + (info.dwMinorVersion / 10.0)
            - (info.wProductType == VER_NT_WORKSTATION) ? 0.5 : 0.0;

    return ver;
}

// Windows 10             : 10.0
// Windows 8.1            : 6.3
// Windows 8.0            : 6.2
// Windows Server 2012    : 6.15
// Windows 7              : 6.1
// Windows Server 2008 R2 : 6.05
// Windows Vista          : 6.0

Regarding last term added to create float version, specifically, "using VER_NT_WORKSTATION", I had some debate about if the server version should be +0.05 or -0.05, or the same as non-server version. I'll leave that one up to you to decide.

关于为创建浮动版本而添加的最后一项,特别是“使用 VER_NT_WORKSTATION”,我对服务器版本应该是 +0.05 还是 -0.05 或与非服务器版本相同有一些争论。我会把那个留给你来决定。