VerifyVersionInfo获取WINDOWS操作系统版本

转载地址:http://blog.csdn.net/mynote/article/details/39135651


FreeEIM在vs2013下,使用GetVersionEx来获取操作系统的版本信息,vs报错error: `GetVersionEx‘ declared deprecated,于是去msdn查看了一下,推荐使用VerifyVersionInfo来进行版本判断,官方解释是因��一般只要判断是否windows的版本大于某个特定的版本号。

于是我查看了一下VerifyVersionInfo,这个函数的使用就是针对特定的版本信息字段进行条件判断。

参数1:OSVERSIONINFOEX  这个结构里保存用户提供的系统版本信息,例如major version,minor version等,这些会用作和系统实际信息进行比较。

参数2:dwTypeMask                类型掩码,是由一些宏进行或操作之后的结果,例如 VER_MAJORVERSION | VER_MINORVERSION 告诉函数major version和minor version需要进行比较,如果只定义VER_MAJORVERSION那么就只会判断major version字段。

参数3:dwlConditionMask        条件掩码,目的是向用户提供丰富的判断条件设置,各个字段都有相应的判断条件设置,通过VER_SET_CONDITION宏进行条件设置。

 

我写了一段代码进行windows版本判断:

[cpp] view plaincopyprint?
01.BOOL isWinVersionEqualTo(DWORD dwMajorVersion, DWORD dwMinorVersion)  
02.{  
03.    OSVERSIONINFOEX osvi;  
04.    DWORDLONG dwlConditionMask = 0;  
05.  
06.    //Initialize the OSVERSIONINFOEX structure.   
07.    ZeroMemory(&osvi, sizeof(osvi));  
08.    osvi.dwOSVersionInfoSize = sizeof(osvi);  
09.    osvi.dwMajorVersion = dwMajorVersion;  
10.    osvi.dwMinorVersion = dwMinorVersion;  
11.  
12.    //Initialize the condition mask.   
13.    VER_SET_CONDITION(dwlConditionMask, VER_MAJORVERSION, VER_EQUAL);  
14.    VER_SET_CONDITION(dwlConditionMask, VER_MINORVERSION, VER_EQUAL);  
15.  
16.    return VerifyVersionInfo(  
17.        &osvi,  
18.        VER_MAJORVERSION | VER_MINORVERSION,  
19.        dwlConditionMask  
20.        );  
21.}  
22.  
23.BOOL isWinVersionGreaterThan(DWORD dwMajorVersion, DWORD dwMinorVersion)  
24.{  
25.    OSVERSIONINFOEX osvi;  
26.    DWORDLONG dwlConditionMask = 0;  
27.  
28.    //Initialize the OSVERSIONINFOEX structure.   
29.    ZeroMemory(&osvi, sizeof(osvi));  
30.    osvi.dwOSVersionInfoSize = sizeof(osvi);  
31.    osvi.dwMajorVersion = dwMajorVersion;  
32.    osvi.dwMinorVersion = dwMinorVersion;  
33.  
34.    //system major version > dwMajorVersion   
35.    VER_SET_CONDITION(dwlConditionMask, VER_MAJORVERSION, VER_GREATER);  
36.    if (VerifyVersionInfo(&osvi, VER_MAJORVERSION, dwlConditionMask))  
37.        return TRUE;  
38.  
39.    //sytem major version = dwMajorVersion && minor version > dwMinorVersion   
40.    VER_SET_CONDITION(dwlConditionMask, VER_MAJORVERSION, VER_EQUAL);  
41.    VER_SET_CONDITION(dwlConditionMask, VER_MINORVERSION, VER_GREATER);  
42.  
43.    return VerifyVersionInfo(  
44.        &osvi,  
45.        VER_MAJORVERSION | VER_MINORVERSION,  
46.        dwlConditionMask  
47.        );  
48.}  
49.  
50.BOOL isWinVersionLessThan(DWORD dwMajorVersion, DWORD dwMinorVersion)  
51.{  
52.    OSVERSIONINFOEX osvi;  
53.    DWORDLONG dwlConditionMask = 0;  
54.  
55.    //Initialize the OSVERSIONINFOEX structure.   
56.    ZeroMemory(&osvi, sizeof(osvi));  
57.    osvi.dwOSVersionInfoSize = sizeof(osvi);  
58.    osvi.dwMajorVersion = dwMajorVersion;  
59.    osvi.dwMinorVersion = dwMinorVersion;  
60.  
61.    //system major version < dwMajorVersion   
62.    VER_SET_CONDITION(dwlConditionMask, VER_MAJORVERSION, VER_LESS);  
63.    if (VerifyVersionInfo(&osvi, VER_MAJORVERSION, dwlConditionMask))  
64.        return TRUE;  
65.  
66.    //sytem major version = dwMajorVersion && minor version < dwMinorVersion   
67.    dwlConditionMask = 0;  
68.    VER_SET_CONDITION(dwlConditionMask, VER_MAJORVERSION, VER_EQUAL);  
69.    VER_SET_CONDITION(dwlConditionMask, VER_MINORVERSION, VER_LESS);  
70.  
71.    return VerifyVersionInfo(  
72.        &osvi,  
73.        VER_MAJORVERSION | VER_MINORVERSION,  
74.        dwlConditionMask  
75.        );  
76.}  
77.  
78.void initializeWindowsVersion(void)  
79.{  
80.    /* Ancient Windows */  
81.    if (IsWinVersionLessThan(5, 1)) {  
82.        WindowsVersion = WINDOWS_ANCIENT;  
83.    }  
84.    /* Windows XP */  
85.    else if (IsWinVersionEqualTo(5, 1)) {  
86.        WindowsVersion = WINDOWS_XP;  
87.    }  
88.    /* Windows Server 2003 */  
89.    else if (IsWinVersionEqualTo(5, 2)) {  
90.        WindowsVersion = WINDOWS_SERVER_2003;  
91.    }  
92.    /* Windows Vista, Windows Server 2008 */  
93.    else if (IsWinVersionEqualTo(6, 0)) {  
94.        WindowsVersion = WINDOWS_VISTA;  
95.    }  
96.    /* Windows 7, Windows Server 2008 R2 */  
97.    else if (IsWinVersionEqualTo(6, 1)) {  
98.        WindowsVersion = WINDOWS_7;  
99.    }  
100.    /* Windows 8 */  
101.    else if (IsWinVersionEqualTo(6, 2)) {  
102.        WindowsVersion = WINDOWS_8;  
103.    }  
104.    else if (isWinVersionGreaterThan(6,2)) {  
105.        WindowsVersion = WINDOWS_NEW;  
106.    }  
107.}  
BOOL isWinVersionEqualTo(DWORD dwMajorVersion, DWORD dwMinorVersion)
{
 OSVERSIONINFOEX osvi;
 DWORDLONG dwlConditionMask = 0;

 //Initialize the OSVERSIONINFOEX structure.
 ZeroMemory(&osvi, sizeof(osvi));
 osvi.dwOSVersionInfoSize = sizeof(osvi);
 osvi.dwMajorVersion = dwMajorVersion;
 osvi.dwMinorVersion = dwMinorVersion;

 //Initialize the condition mask.
 VER_SET_CONDITION(dwlConditionMask, VER_MAJORVERSION, VER_EQUAL);
 VER_SET_CONDITION(dwlConditionMask, VER_MINORVERSION, VER_EQUAL);

 return VerifyVersionInfo(
  &osvi,
  VER_MAJORVERSION | VER_MINORVERSION,
  dwlConditionMask
  );
}

BOOL isWinVersionGreaterThan(DWORD dwMajorVersion, DWORD dwMinorVersion)
{
 OSVERSIONINFOEX osvi;
 DWORDLONG dwlConditionMask = 0;

 //Initialize the OSVERSIONINFOEX structure.
 ZeroMemory(&osvi, sizeof(osvi));
 osvi.dwOSVersionInfoSize = sizeof(osvi);
 osvi.dwMajorVersion = dwMajorVersion;
 osvi.dwMinorVersion = dwMinorVersion;

 //system major version > dwMajorVersion
 VER_SET_CONDITION(dwlConditionMask, VER_MAJORVERSION, VER_GREATER);
 if (VerifyVersionInfo(&osvi, VER_MAJORVERSION, dwlConditionMask))
  return TRUE;

 //sytem major version = dwMajorVersion && minor version > dwMinorVersion
 VER_SET_CONDITION(dwlConditionMask, VER_MAJORVERSION, VER_EQUAL);
 VER_SET_CONDITION(dwlConditionMask, VER_MINORVERSION, VER_GREATER);

 return VerifyVersionInfo(
  &osvi,
  VER_MAJORVERSION | VER_MINORVERSION,
  dwlConditionMask
  );
}

BOOL isWinVersionLessThan(DWORD dwMajorVersion, DWORD dwMinorVersion)
{
 OSVERSIONINFOEX osvi;
 DWORDLONG dwlConditionMask = 0;

 //Initialize the OSVERSIONINFOEX structure.
 ZeroMemory(&osvi, sizeof(osvi));
 osvi.dwOSVersionInfoSize = sizeof(osvi);
 osvi.dwMajorVersion = dwMajorVersion;
 osvi.dwMinorVersion = dwMinorVersion;

 //system major version < dwMajorVersion
 VER_SET_CONDITION(dwlConditionMask, VER_MAJORVERSION, VER_LESS);
 if (VerifyVersionInfo(&osvi, VER_MAJORVERSION, dwlConditionMask))
  return TRUE;

 //sytem major version = dwMajorVersion && minor version < dwMinorVersion
 dwlConditionMask = 0;
 VER_SET_CONDITION(dwlConditionMask, VER_MAJORVERSION, VER_EQUAL);
 VER_SET_CONDITION(dwlConditionMask, VER_MINORVERSION, VER_LESS);

 return VerifyVersionInfo(
  &osvi,
  VER_MAJORVERSION | VER_MINORVERSION,
  dwlConditionMask
  );
}

void initializeWindowsVersion(void)
{
 /* Ancient Windows */
 if (IsWinVersionLessThan(5, 1)) {
  WindowsVersion = WINDOWS_ANCIENT;
 }
 /* Windows XP */
 else if (IsWinVersionEqualTo(5, 1)) {
  WindowsVersion = WINDOWS_XP;
 }
 /* Windows Server 2003 */
 else if (IsWinVersionEqualTo(5, 2)) {
  WindowsVersion = WINDOWS_SERVER_2003;
 }
 /* Windows Vista, Windows Server 2008 */
 else if (IsWinVersionEqualTo(6, 0)) {
  WindowsVersion = WINDOWS_VISTA;
 }
 /* Windows 7, Windows Server 2008 R2 */
 else if (IsWinVersionEqualTo(6, 1)) {
  WindowsVersion = WINDOWS_7;
 }
 /* Windows 8 */
 else if (IsWinVersionEqualTo(6, 2)) {
  WindowsVersion = WINDOWS_8;
 }
 else if (isWinVersionGreaterThan(6,2)) {
  WindowsVersion = WINDOWS_NEW;
 }
}

 

这段代码用来对windows版本进行判断,感觉比用GetVersionEx来的麻烦很多... 可能是没发现别的方法.

 

 

///////////////////////////////////////////////////////////////////////////////////////////根据余不欢的评论,我check了versionhelpers////////////////////////////////////////////////////////////////////////

Microsoft已经提供了众多的版本检查函数,例如IsWindowsXPOrGreater,IsWindows7OrGreater...


你可能感兴趣的:(VerifyVersionInfo获取WINDOWS操作系统版本)