GetVersionEx 获取较新Windows系统版本

本文转自:http://hi.baidu.com/letwin/blog/item/1750f2124e0471c4c2fd7827.html

获得Windows版本GetVersion(Windows XP、Windows Vista、Windows 7、Windows 2000、Windows Server 2003
2009-11-16 10:21

 

写程序需要查看Windows的版本号来决定程序如何操作,看到MSDN的GetVersion 函数,这个函数返回一个DWORD的数据,然后自己对数据进行转换得到想要的具体值。

对应值如下:

 

Operating systemVersion number
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

 

转自MSDN的函数参考:

GetVersion Function

Operating System Version

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.

Syntax

C++
DWORD WINAPI GetVersion(void);

Parameters

This function has no parameters.

Return Value

If the function succeeds, the return value includes the major and minor version numbers of the operating system in the low-order word, and information about the operating system platform in the high-order word.

For all platforms, the low-order word contains the version number of the operating system. The low-order byte of this word specifies the major version number, in hexadecimal notation. The high-order byte specifies the minor version (revision) number, in hexadecimal notation. The high-order bit is zero, the next 7 bits represent the build number, and the low-order byte is 5.

Remarks

The GetVersionEx function was developed because many existing applications err when examining the packed DWORD value returned by GetVersion, transposing the major and minor version numbers. GetVersionEx forces applications to explicitly examine each element of version information.VerifyVersionInfo eliminates further potential for error by comparing the required system version with the current system version for you.

Examples

The following code fragment illustrates how to extract information from the GetVersion return value:

#include <windows.h>
#include <stdio.h>

void main()
{
    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);
}

The GetVersionEx function retrieves the major and minor version numbers and other information about the currently running version of the operating system. The VerifyVersionInfo function enables you to determine whether the current system satisfies a specified set of operating system version conditions. For more information, see the following topics:

  • Getting the System Version
  • Verifying the System Version

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

Operating systemVersion number
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

Identifying the current operating system is usually not the best way to determine whether a particular operating system feature is present. This is because the operating system may have had new features added in a redistributable DLL. Rather than using GetVersionEx to determine the operating system platform or version number, test for the presence of the feature itself.

To determine the best way to test for a feature, refer to the documentation for the feature of interest. The following list discusses some common techniques for feature detection:

  • You can test for the presence of the functions associated with a feature. To test for the presence of a function in a system DLL, call the LoadLibrary function to load the DLL. Then call the GetProcAddress function to determine whether the function of interest is present in the DLL. Use the pointer returned by GetProcAddress to call the function. Note that even if the function is present, it may be a stub that just returns an error code such as ERROR_CALL_NOT_IMPLEMENTED.
  • You can determine the presence of some features by using the GetSystemMetrics function. For example, you can detect multiple display monitors by calling GetSystemMetrics(SM_CMONITORS).
  • There are several versions of the redistributable DLLs that implement shell and common control features. For information about determining which versions are present on the system your application is running on, see the topic Shell and Common Controls Versions.

If you must require a particular operating system, be sure to use it as a minimum supported version, rather than design the test for the one operating system. This way, your detection code will continue to work on future versions of Windows.

Note that a 32-bit application can detect whether it is running under WOW64 by calling the IsWow64Process function. It can obtain additional processor information by calling the GetNativeSystemInfo function.

补充:

 

vista与2008的大小版本都是一样的6.0,要区分就要通过wProductType来识别!

if( VerInfoEx.dwMajorVersion == 6 )
  {
  if (VerInfoEx.wProductType == VER_NT_SERVER || VerInfoEx.wProductType == VER_NT_DOMAIN_CONTROLLER)
  return WINDOWS_2008;


  return WINDOWS_VISTA;
  }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值