C++ 检查是否是 Windows 10

2022-01-11 00:00:00 operating-system windows windows-10 c++

我正在针对特定操作系统制作应用程序,但我似乎无法缩小 Windows 10 的范围,它以 Windows 8 的形式出现.我已经在 Window 10 Pro 上对其进行了测试,结果是 Major: 6 Min:2.还有其他方法可以更有效地检查它是否是 Windows 10?

I am making an app to OS specific but I can't seem to narrow down Windows 10, it comes up as Windows 8. I have tested it on Window 10 Pro and the outcome is Major: 6 Min:2. Is there another way to check if it's windows 10 more efficiently?

发现一个正常工作的 API RtlGetVersion() 适用于所有操作系统!

Found a properly working API RtlGetVersion() works for all OS's properly!

#include "windows.h"
#include <iostream>
using namespace std;
BOOL EqualsMajorVersion(DWORD majorVersion)
{
OSVERSIONINFOEX osVersionInfo;
::ZeroMemory(&osVersionInfo, sizeof(OSVERSIONINFOEX));
osVersionInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
osVersionInfo.dwMajorVersion = majorVersion;
ULONGLONG maskCondition = ::VerSetConditionMask(0, VER_MAJORVERSION, VER_EQUAL);
return ::VerifyVersionInfo(&osVersionInfo, VER_MAJORVERSION, maskCondition);
}
BOOL EqualsMinorVersion(DWORD minorVersion)
{
OSVERSIONINFOEX osVersionInfo;
::ZeroMemory(&osVersionInfo, sizeof(OSVERSIONINFOEX));
osVersionInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
osVersionInfo.dwMinorVersion = minorVersion;
ULONGLONG maskCondition = ::VerSetConditionMask(0, VER_MINORVERSION, VER_EQUAL);
return ::VerifyVersionInfo(&osVersionInfo, VER_MINORVERSION, maskCondition);
}
int main()
{
    if (EqualsMajorVersion(7) && EqualsMinorVersion(1))
        printf("Maj:7 Min:1");
    else if (EqualsMajorVersion(7) && EqualsMinorVersion(0))
        printf("Maj:7 Min:0");
    else if (EqualsMajorVersion(6) && EqualsMinorVersion(7))
        printf("Maj:6 Min:7");
    else if (EqualsMajorVersion(6) && EqualsMinorVersion(6))
        printf("Maj:6 Min:6");
    else if (EqualsMajorVersion(6) && EqualsMinorVersion(5))
        printf("Maj:6 Min:5");
    else if (EqualsMajorVersion(6) && EqualsMinorVersion(4))
        printf("Maj:6 Min:4");
    else if (EqualsMajorVersion(6) && EqualsMinorVersion(3))
        printf("Maj:6 Min:3");
    else if (EqualsMajorVersion(6) && EqualsMinorVersion(2))
        printf("Maj:6 Min:2");
    else if (EqualsMajorVersion(6) && EqualsMinorVersion(1))
        printf("Maj:6 Min:1");
    else if (EqualsMajorVersion(6) && EqualsMinorVersion(0))
        printf("Maj:6 Min:0");
    else if (EqualsMajorVersion(5) && EqualsMinorVersion(2))
        printf("Maj:5 Min:2");
    else if (EqualsMajorVersion(5) && EqualsMinorVersion(1))
        printf("Maj:5 Min:1");
    else
        printf("OS not on list...");
    getchar();
}

推荐答案

这很讽刺,但是...

BOOL WINAPI IsWindows10OrGreater(void);

文档这里.

编辑:来自 Redmond 的天才似乎在思考原型时遇到了问题,或者换句话说,OP 收到了评论中所述的未定义符号错误.还有这个功能:

Edit: The geniuses from Redmond appear to have problems thinking with prototypes, or in other words, the OP gets an undefined symbol error as stated in the comments. There's also this function:

BOOL WINAPI IsWindowsVersionOrGreater(
    WORD wMajorVersion,
    WORD wMinorVersion,
    WORD wServicePackMajor
);

并且,对于 Windows 10,wMajorVersionwMinorVersionwServicePackMajor 似乎分别是 1000.

And, for Windows 10, wMajorVersion, wMinorVersion, and wServicePackMajor appear to be, respectively, 10, 0, and 0.

该功能的文档这里.

第二次编辑:这是官方的:IsWindowsVersionOrGreater()GetVersionEx() 从 8.1 开始被破坏.始终使用 IsWindowsXXXOrGreater() 代替,其中 XXX 可以是 XPXPSP1XPSP2XPSP3VistaVistaSP1VistaSP277SP188Point110Server.

Second edit: It's official: IsWindowsVersionOrGreater() and GetVersionEx() are broken from 8.1 onwards. Use IsWindowsXXXOrGreater() always instead, where XXX can be XP, XPSP1, XPSP2, XPSP3, Vista, VistaSP1, VistaSP2, 7, 7SP1, 8, 8Point1, 10, or Server.

相关文章