C ++检查Windows 10

我正在制作一个应用程序到特定的操作系统,但我似乎无法缩小Windows 10,它出现在Windows 8中。我已经在Window 10 Pro上testing它,结果是Major:6 Min:2。 是否有另一种方法来检查是否更有效的Windows 10?

编辑:find一个正常工作的API RtlGetVersion()适用于所有操作系统的正确!

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

Solutions Collecting From Web of "C ++检查Windows 10"

这很讽刺,但是…

 BOOL WINAPI IsWindows10OrGreater(void); 

文档在这里 。

编辑 :来自雷德蒙德的天才似乎有思考原型的问题,或者换句话说,OP得到一个未定义的符号错误,如评论中所述。 还有这个功能:

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

而对于Windows 10, wMajorVersionwMinorVersionwServicePackMajor分别显示为wServicePackMajor0

这里的功能文档。

第二编辑 :这是官方的: IsWindowsVersionOrGreater()GetVersionEx()从8.1开始被打破。 总是改用IsWindowsXXXOrGreater() ,其中XXX可以是XPXPSP1XPSP2XPSP3VistaVistaSP1VistaSP2server

更新:检查win7,win8.1和win10。 现在一切都确定。 所以代码:

 #include <windows.h> #include <VersionHelpers.h> #include <string> std::string getWinVerion() { OSVERSIONINFO info; ZeroMemory(&info, sizeof(OSVERSIONINFO)); info.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); GetVersionEx(&info); std::string version = std::to_string(info.dwMajorVersion); version.append(".").append(std::to_string(info.dwMinorVersion)); if (IsWindows7SP1OrGreater()) { if (version == "6.1") return "6.1"; } if (IsWindows8Point1OrGreater()) return "6.3"; else return "10.0"; } 

这是真的,唯一的方法来检测,如果这是Windows 10是通过使用

 BOOL WINAPI IsWindows10OrGreater(void); 

尽管您需要使用Visual Studio 2015获得最新的SDK。请注意

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

不在Windows 10上工作,同样的:

 BOOL WINAPI GetVersionEx( _Inout_ LPOSVERSIONINFO lpVersionInfo); 

有时返回6.2或6.3。 (在Windows 10上完全隐藏)

以下为我工作:

 . . . #include <windows.h> #include <stdio.h> #include <tchar.h> . . . typedef void (WINAPI * RtlGetVersion_FUNC) (OSVERSIONINFOEXW *); BOOL GetVersion(OSVERSIONINFOEX * os) { HMODULE hMod; RtlGetVersion_FUNC func; #ifdef UNICODE OSVERSIONINFOEXW * osw = os; #else OSVERSIONINFOEXW o; OSVERSIONINFOEXW * osw = &o; #endif hMod = LoadLibrary(TEXT("ntdll.dll")); if (hMod) { func = (RtlGetVersion_FUNC)GetProcAddress(hMod, "RtlGetVersion"); if (func == 0) { FreeLibrary(hMod); return FALSE; } ZeroMemory(osw, sizeof (*osw)); osw->dwOSVersionInfoSize = sizeof (*osw); func(osw); #ifndef UNICODE os->dwBuildNumber = osw->dwBuildNumber; os->dwMajorVersion = osw->dwMajorVersion; os->dwMinorVersion = osw->dwMinorVersion; os->dwPlatformId = osw->dwPlatformId; os->dwOSVersionInfoSize = sizeof (*os); DWORD sz = sizeof (os->szCSDVersion); WCHAR * src = osw->szCSDVersion; unsigned char * dtc = (unsigned char *)os->szCSDVersion; while (*src) * Dtc++ = (unsigned char)* src++; *Dtc = '\ 0'; #endif } else return FALSE; FreeLibrary(hMod); return TRUE; } . . . //check win10 void mymethod() { OSVERSIONINFOEX os; if (GetVersion(&os) == TRUE && os.dwMajorVersion == 10) { isWin10 = TRUE; } } 

参考。 :( http://yamatyuu.net/computer/program/vc2013/rtlgetversion/index.html