API得到Windows版本
/**
* Windows Version
* https://msdn.microsoft.com/en-us/library/windows/desktop/dn481241(v=vs.85).aspx
* In Windows 8.1 and Windows 10, the GetVersion and GetVersionEx functions have been deprecated.
* In Windows 10, the VerifyVersionInfo function has also been deprecated.
* Operating system Version number (Major.Minor)
*
* Windows 10 6.4, 7.x, 10.0
* Windows 8.1 6.3
* Windows 8 6.2
* Windows 7 6.1
* Windows Server 2008 R2 6.1
* Windows Server 2008 6.0
* Windows Vista 6.0
* Windows Server 2003 R2 5.2
* Windows Server 2003 5.2
* Windows XP 5.1
* Windows 2000 5.0
* Windows NT40 4.0
* Windows NT35 3.0
*/
typedef enum OSWindowsVersion {
OS_UNKNOWN = 0,
OS_WINNT35 = 35,
OS_WINNT40 = 40,
OS_WIN2000 = 50,
OS_WINXP = 51,
OS_WIN2003 = 52,
OS_WINXP64 = 52,
OS_Vista = 60,
OS_WIN7 = 61,
OS_WIN8 = 62,
OS_WIN2012 = 62,
OS_WIN8_1 = 63,
OS_WIN10 = 100
} OSWindowsVersion;
OSWindowsVersion GetWindowsVersion();
// Author: cheungmine
// 20161205
BOOL GetNtdllVersion(DWORD *dwMajorVer, DWORD *dwMinorVer, DWORD *dwBuildNum)
{
typedef void (WINAPI *PFN_RTLGETNTVERSIONNUMBERS)(DWORD*, DWORD*, DWORD*);
HMODULE hNtdll = LoadLibrary("ntdll.dll");
if (hNtdll) {
PFN_RTLGETNTVERSIONNUMBERS pfnGetWinVer = (PFN_RTLGETNTVERSIONNUMBERS)
GetProcAddress(hNtdll, "RtlGetNtVersionNumbers");
if (pfnGetWinVer) {
pfnGetWinVer(dwMajorVer, dwMinorVer, dwBuildNum);
FreeLibrary(hNtdll);
return TRUE;
}
FreeLibrary(hNtdll);
}
return FALSE;
}
OSWindowsVersion GetWindowsVersion()
{
DWORD majorVer = 0;
DWORD minorVer = 0;
DWORD buildNum = 0;
BOOL isNtVer = 0;
OSVERSIONINFO info;
OSWindowsVersion osVer = OS_UNKNOWN;
SeZero(&info, sizeof(info));
info.dwOSVersionInfoSize = sizeof(info);
if (GetVersionEx(&info) && info.dwPlatformId == VER_PLATFORM_WIN32_NT) {
switch (info.dwMajorVersion) {
case 3:
osVer = OS_WINNT35;
break;
case 4:
osVer = OS_WINNT40;
break;
case 5:
if (info.dwMinorVersion == 0) {
osVer = OS_WIN2000;
} else if (info.dwMinorVersion == 1) {
osVer = OS_WINXP;
} else if (info.dwMinorVersion >= 2) {
osVer = OS_WIN2003;
}
break;
case 6:
if (info.dwMinorVersion == 0) {
osVer = OS_Vista;
} else if (info.dwMinorVersion == 1) {
osVer = OS_WIN7;
} else if (info.dwMinorVersion == 2) {
// 8 or 8.1 or 10
osVer = OS_WIN8;
if (GetNtdllVersion(&majorVer, &minorVer, &buildNum)) {
if (majorVer == 6) {
if (minorVer == 3) {
osVer = OS_WIN8_1;
} else if (minorVer >= 4) {
osVer = OS_WIN10;
}
} else if (majorVer >= 7) {
osVer = OS_WIN10;
}
}
} else if (info.dwMinorVersion == 3) {
osVer = OS_WIN8_1;
} else if (info.dwMinorVersion >= 4) {
osVer = OS_WIN10;
}
break;
default:
if (info.dwMajorVersion >= 7) {
osVer = OS_WIN10;
}
break;
}
} else if (GetNtdllVersion(&majorVer, &minorVer, &buildNum)) {
// should not run to this!!
switch (majorVer) {
case 3:
osVer = OS_WINNT35;
break;
case 4:
osVer = OS_WINNT40;
break;
case 5:
if (minorVer == 0) {
osVer = OS_WIN2000;
} else if (minorVer == 1) {
osVer = OS_WINXP;
} else if (minorVer >= 2) {
osVer = OS_WIN2003;
}
break;
case 6:
if (minorVer == 0) {
osVer = OS_Vista;
} else if (minorVer == 1) {
osVer = OS_WIN7;
} else if (minorVer == 2) {
osVer = OS_WIN8;
} else if (minorVer == 3) {
osVer = OS_WIN8_1;
} else if (minorVer >= 4) {
osVer = OS_WIN10;
}
break;
default:
if (majorVer >= 7) {
osVer = OS_WIN10;
}
break;
}
}
return osVer;
}