Operating System Version

methods

1. GetVersion function

Retrieves the version number of the current operating system.

Note This function has been superseded by GetVersionEx. New applications should use GetVersionEx or VerifyVersionInfo.

DWORD WINAPI GetVersion(void);
The following illustration shows the format of the bits in system version.
+-------------+----------+-------------------------+
|   Reserved  | Build Id |  Minor    |   Major     |
+-------------+----------+-------------------------+
31         30 29      16 15        8 7             0   bit

example:

    DWORD dwVersion = 0;
    DWORD dwMajorVersion = 0;
    DWORD dwMinorVersion = 0;
    DWORD dwBuild = 0;

    dwVersion = GetVersion();
 
    // Get the Windows version.

    dwMajorVersion = (DWORD)(LOBYTE(LOWORD(dwVersion)));
    dwMinorVersion = (DWORD)(HIBYTE(LOWORD(dwVersion)));

    // Get the build number.

    if (dwVersion < 0x80000000)              
        dwBuild = (DWORD)(HIWORD(dwVersion));
    

    printf("Version is %d.%d (%d)\n", 
                dwMajorVersion,
                dwMinorVersion,
                dwBuild);

2. GetVersionEx function

The GetVersionEx function retrieves the major and minor version numbers and other information about the currently running version of the operating system.

BOOL WINAPI GetVersionEx(
  __inout  LPOSVERSIONINFO lpVersionInfo
);

lpVersionInfo

An OSVERSIONINFO or OSVERSIONINFOEX structure that receives the operating system information.Before calling the GetVersionEx function, set the dwOSVersionInfoSize member of this structure as appropriate.

OSVERSIONINFO struct:

typedef struct _OSVERSIONINFO {
  DWORD dwOSVersionInfoSize;
  DWORD dwMajorVersion;
  DWORD dwMinorVersion;
  DWORD dwBuildNumber;
  DWORD dwPlatformId;
  TCHAR szCSDVersion[128];
} OSVERSIONINFO;
OSVERSIONINFOEX struct:

typedef struct _OSVERSIONINFOEX {
  DWORD dwOSVersionInfoSize;
  DWORD dwMajorVersion;
  DWORD dwMinorVersion;
  DWORD dwBuildNumber;
  DWORD dwPlatformId;
  TCHAR szCSDVersion[128];
  WORD  wServicePackMajor;
  WORD  wServicePackMinor;
  WORD  wSuiteMask;
  BYTE  wProductType;
  BYTE  wReserved;
} OSVERSIONINFOEX, *POSVERSIONINFOEX, *LPOSVERSIONINFOEX;
example:
	OSVERSIONINFO _osv;
	ZeroMemory(&_osv, sizeof(OSVERSIONINFO));
	_osv.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
	::GetVersionEx(&_osv);
	cout<<"Major:"<<_osv.dwMajorVersion<<endl;
	cout<<"Minor:"<<_osv.dwMinorVersion<<endl;
	cout<<"Build:"<<_osv.dwBuildNumber<<endl;
	cout<<"Platform:"<<_osv.dwPlatformId<<endl;
	cout<<"Others:"<<_osv.szCSDVersion<<endl;
dwMajorVersion, dwMinorVersion, dwBuildNumberGetVersion的返回值含义一样,只不过不用自己通过宏来提取,同时加入了一个关键字段 dwPlatformId,参考值如下

ValueDescription
VER_PLATFORM_WIN32sSpecifies the Windows 3.1 OS.
VER_PLATFORM_WIN32_WINDOWSSpecifies the Windows 95 or Windows 98 OS.

For Windows 95, dwMinorVersion is zero.

For Windows 98, dwMinorVersion is greater than zero.

VER_PLATFORM_WIN32_NTSpecifies the Windows NT OS.
VER_PLATFORM_WIN32_CESpecifies the Windows CE OS.
Note: VER_PLATFORM_WIN32_NT for The operating system is Windows 7, Windows Server 2008, Windows Vista, Windows Server 2003, Windows XP, or Windows 2000.

3. VerifyVersionInfo function

Compares a set of operating system version requirements to the corresponding values for the currently running version of the system.

BOOL WINAPI VerifyVersionInfo(
  __in  LPOSVERSIONINFOEX lpVersionInfo,
  __in  DWORD dwTypeMask,
  __in  DWORDLONG dwlConditionMask
);
Note:lpVersionInfo是你期望的operating system version,需要初始化其内容包括dwOSVersionInfoSize成员需要设置;dwTypeMask是你想要比较的OSVERSIONINFOEX中那个成员,使用|可以实现比较多个成员;dwlConditionMask设置operator,是大于,小于等。

使用步骤如下:

  1. Set the appropriate values in the OSVERSIONINFOEX structure.
  2. Set the appropriate condition mask using the VER_SET_CONDITION macro or VerSetConditionMask function.
  3. Call VerifyVersionInfo to perform the test.
Sample:
     OSVERSIONINFOEX osvi;
     DWORDLONG dwlConditionMask = 0;
     int op=VER_GREATER_EQUAL;
  
     // Initialize the OSVERSIONINFOEX structure.
  
     ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
     osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
     osvi.dwMajorVersion = 5;
     osvi.dwMinorVersion = 1;
     osvi.wServicePackMajor = 2;
     osvi.wServicePackMinor = 0;
  
     // Initialize the condition mask.
  
     VER_SET_CONDITION( dwlConditionMask, VER_MAJORVERSION, op );
     VER_SET_CONDITION( dwlConditionMask, VER_MINORVERSION, op );
     VER_SET_CONDITION( dwlConditionMask, VER_SERVICEPACKMAJOR, op );
     VER_SET_CONDITION( dwlConditionMask, VER_SERVICEPACKMINOR, op );
  
     // Perform the test.
  
     return VerifyVersionInfo(
        &osvi, 
        VER_MAJORVERSION | VER_MINORVERSION | 
        VER_SERVICEPACKMAJOR | VER_SERVICEPACKMINOR,
        dwlConditionMask);
VerifyVersionInfo enables you to easily determine the presence of a required set of operating system version conditions. It is preferable to use VerifyVersionInfo rather than calling the GetVersionEx function to perform your own comparisons.
注意比较minor, major与service pack的时候指定的比较条件。详细说明请参考MSDN的remark。



Reference

The following table summarizes the most recent operating system version numbers.

Operating systemVersion number
Windows 86.2
Windows Server 20126.2
Windows 76.1
Windows Server 2008 R26.1
Windows Server 20086.0
Windows Vista6.0
Windows Server 2003 R25.2
Windows Server 20035.2
Windows XP5.1
Windows 20005.0


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值