OSVERSIONINFO结构

来源:互联网 发布:文明5 mac 中文 编辑:程序博客网 时间:2024/06/03 13:17
typedef struct _OSVERSIONINFO {
DWORD dwOSVersionInfoSize;
DWORD dwMajorVersion;
DWORD dwMinorVersion;
DWORD dwBuildNumber;
DWORD dwPlatformId;
TCHAR szCSDVersion[128];
OSVERSIONINFO;


该结构体包含操作系统的版本信息。包括操作系统的主版本号、副版本号、创建号、以及操作系统平台ID号和关于操作系统的其他描述信息。

dwOSVersionInfoSize:指定该数据结构的字节大小。

dwMajorVersion:操作系统的主版本号

dwMinorVersion:操作系统的副版本号

dwBuildNumber:操作系统的创建号

dwPlatformId:操作系统平台ID号
其中dwPlatformId可为以下值:
VER_PLATFORM_WIN32s:标识为Windows 3.1;
VER_PLATFORM_WIN32_WINDOWS:标识为Windows 95或Windows 98;对于Windows 95操作系统而言,dwMinorVersion值为0,对Windows 98操作系统dwMinorVersion则大于0;
VER_PLATFORM_WIN32_NT:标识为WindowsNT。
VER_PLATFORM_WIN32_CE:标识为Windows CE


szCSDVersion:关于该操作系统的附近信息


下表总结了Windows支持的版本号:
     Windows 76.161OSVERSIONINFOEX.wProductType == VER_NT_WORKSTATIONWindows Server 2008 R26.161OSVERSIONINFOEX.wProductType != VER_NT_WORKSTATIONWindows Server 20086.060OSVERSIONINFOEX.wProductType != VER_NT_WORKSTATIONWindows Vista6.060OSVERSIONINFOEX.wProductType == VER_NT_WORKSTATIONWindows Server 2003 R25.252GetSystemMetrics(SM_SERVERR2) != 0Windows Server 20035.252GetSystemMetrics(SM_SERVERR2) == 0Windows XP5.151Not applicableWindows 20005.050Not applicable

SVERSIONINFO结构包含了操作系统的版本信息,包括操作系统的主版本号、副版本号、创建号、以及操作系统平台ID号和关于操作系统的其他描述信息。其定义为:

  typedef struct _OSVERSIONINFO{

  DWORD dwOSVersionInfoSize;

  //指定该数据结构的字节大小

  DWORD dwMajorVersion;

  //操作系统的主版本号

  DWORD dwMinorVersion;

  //操作系统的副版本号

  DWORD dwBuildNumber;

  //操作系统的创建号

  DWORD dwPlatformId;

  //操作系统ID号

  TCHAR szCSDVersion[ 128 ];

  //关于操作系统的一些附加信息

  } OSVERSIONINFO;

  其中dwPlatformId可为以下值:

  VER_PLATFORM_WIN32s:标识为Windows 3.1;

  VER_PLATFORM_WIN32_WINDOWS:标识为Windows 95或Windows 98;

  对于Windows 95操作系统而言,dwMinorVersion值为0,对Windows 98操作系统dwMinorVersion则大于0;

  VER_PLATFORM_WIN32_NT:标识为WindowsNT。

dwMajorVersion:

Identifies the major version number of the operating system as follows. Operating System Value

Windows 95 4
Windows 98 4
Windows Me 4
Windows NT 3.51 3
Windows NT 4.0 4
Windows 2000 5
Windows XP 5
Windows .NET Server 5

dwMinorVersion:

Identifies the minor version number of the operating system as follows. Operating System Value

Windows 95 0
Windows 98 10
Windows Me 90
Windows NT 3.51 51
Windows NT 4.0 0
Windows 2000 0
Windows XP 1
Windows .NET Server

需求:
Windows NT/2000/XP: Included in Windows NT 3.5 and later.
Windows 95/98/Me: Included in Windows 95 and later.
头文件: Declared in Winnt.h; include Windows.h.
Unicode: Declared as Unicode and ANSI structures.

程序具体实现步骤

  1.使用AppWizard新建一个基于单文档的工程SystemJudge。

  2.在工程中添加两个文件,即定义文件judge.h和实现文件judge.cpp。

  在judge.h文件中添加如下代码:

  #ifndef __JUDGE_H__

  #define __ JUDGE _H__

  Cstring JudgeOperatingSystem();

  //判断操作系统函数定义

  #endif

  在judge.cpp文件中添加如下代码:

  #include "stdafx.h"

  #include "judge.h"

  Cstring JudgeOperatingSystem()

  //判断操作系统函数的实现

  {

  OSVERSIONINFO OsVersionInfo;

  OsVersionInfo.dwOSVersionInfoSize=sizeof(OSVERSIONINFO);

  GetVersionEx(&&OsVersionInfo);

  if(OsVersionInfo.dwPlatformId=VER_PLATFORM_WIN32_WINDOWS )

   {

   if(OsVersionInfo.dwMajorVersion>4) return "Windows98";

  else if(OsVersionInfo.dwMajorVersion=4)

   {

   if(OsVersionInfo.dwMinorVersion>0) return "Windows98";

  else return "Windows95";

   }

   else return "Windows3.1";

   }

  elseif(OsVersionInfo.dwPlatformId= VER_PLATFORM_WIN32_NT )

   {

   return "WindowsNT";

   }

   else if(OsVersionInfo.dwPlatformId== VER_PLATFORM_WIN32s)

   {

   return "Windows3.1";

   }

   else return "NoName";

  }

  3.在CmainFrame类的实现文件MainFrm.cpp中开头添加#include"judge.h"。并在其OnCreate函数中return语句前加入如下判断代码:

  Cstring sOperatingSystem = JudgeOperatingSystem();

  if( sOperatingSystem == "Windows98")

  {

  //假如本软件需要在WindowsNT下运行

  MessageBox(

   "本软件在WindowsNT4.0或更高的版本下运行,您的操作系统是Windows98 "

  "请安装WindowsNT4.0以上的版本或使用Windows98版!",

  "警告",

  MB_OK

  );

  }

   else if( sOperatingSystem == "WindowsNT")

   {

   //假如软件需要在Windows98下运行,我们可在此添加警告对话框

   }

   else return -1;

  至此,操作系统判断功能已经实现,在软件启动时会自动判断软件当前运行的操作系统并提示用户是否在正确的操作系统环境,从而保证了软件运行的正常性。