C++ Check if Windows 10(C++ 检查是否是 Windows 10)
问题描述
我正在针对特定操作系统制作应用程序,但我似乎无法缩小 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,wMajorVersion
、wMinorVersion
和 wServicePackMajor
似乎分别是 10
、0
和 0
.
And, for Windows 10, wMajorVersion
, wMinorVersion
, and wServicePackMajor
appear to be, respectively, 10
, 0
, and 0
.
该功能的文档这里.
第二次编辑:这是官方的:IsWindowsVersionOrGreater()
和 GetVersionEx()
从 8.1 开始被破坏.始终使用 IsWindowsXXXOrGreater()
代替,其中 XXX
可以是 XP
、XPSP1
、XPSP2
、XPSP3
、Vista
、VistaSP1
、VistaSP2
、7
、7SP1
、8
、8Point1
、10
或 Server
.
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
.
这篇关于C++ 检查是否是 Windows 10的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:C++ 检查是否是 Windows 10
基础教程推荐
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- Windows Media Foundation 录制音频 2021-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 从 std::cin 读取密码 2021-01-01