获得操作系统版本的一个函数。用c语言实现

原创 2011年10月24日 20:07:33

GetVersionEx:用它能够得到更详细的Windows系统的版本信息。

函数原型:

BOOL GetVersionEx(POSVERSIONINFO pVersionInformation);

我们先来看看OSVERSIONINFOEX这个结构:

typedef struct {

DWORD dwOSVersionInfoSize;       //在使用GetVersionEx之前要将此初始化为结构的大小

DWORD dwMajorVersion;               //系统主版本号

DWORD dwMinorVersion;               //系统次版本号

DWORD dwBuildNumber;               //系统构建号

DWORD dwPlatformId;                  //系统支持的平台(详见附1)

TCHAR szCSDVersion[128];          //系统补丁包的名称

WORD wServicePackMajor;            //系统补丁包的主版本

WORD wServicePackMinor;            //系统补丁包的次版本

WORD wSuiteMask;                      //标识系统上的程序组(详见附2)

BYTE wProductType;                    //标识系统类型(详见附3)

BYTE wReserved;                         //保留,未使用

} OSVERSIONINFOEX, *POSVERSIONINFOEX;

这个结构在Windows 2000后出现,老版本的OSVERSIONINFO结构没有wServicePackMajor、wServicePackMinor、wSuiteMask、wProductType和wReserved这几个成员。

 

接着在调用函数之前先初始化结构的大小:

OSVERSIONINFOEX os;

os.dwOSVersionInfoSize=sizeof(os);

函数返回值为TRUE表示成功:

if(!GetVersionEx((OSVERSIONINFO *)&os))

{

      return FALSE;

}

[代码] [C/C++]代码

01 CString getSystemName()
02  
03 {
04  
05     SYSTEM_INFO info;                                   //用SYSTEM_INFO结构判断64位AMD处理器
06     GetSystemInfo(&info);                               //调用GetSystemInfo函数填充结构
07     OSVERSIONINFOEX os;
08     os.dwOSVersionInfoSize=sizeof(OSVERSIONINFOEX);  /*在调用函数前必须用sizeof(OSVERSIONINFOEXA)填充dwOSVersionInfoSize结构成员*/
09     if(GetVersionEx((OSVERSIONINFO *)&os))                  /*调用GetVersionEx函数OSVERSIONINFOEX结构必须将指针类型强制转换*/
10     {
11         CString vname;
12  
13         //下面根据版本信息判断操作系统名称
14  
15         switch(os.dwMajorVersion){                        //判断主版本号
16             case 4:
17                 switch(os.dwMinorVersion){                //判断次版本号
18                     case 0:
19                         if(os.dwPlatformId==VER_PLATFORM_WIN32_NT)
20                             vname=_T("Microsoft Windows NT 4.0");                //1996年7月发布
21                         else if(os.dwPlatformId==VER_PLATFORM_WIN32_WINDOWS)
22                             vname=_T("Microsoft Windows 95");
23                         break;
24                     case 10:
25                         vname=_T("Microsoft Windows 98");
26                         break;
27                     case 90:
28                         vname=_T("Microsoft Windows Me");
29                         break;
30                 }
31                 break;
32             case 5:
33                 switch(os.dwMinorVersion){               //再比较dwMinorVersion的值
34                     case 0:
35                         vname=_T("Microsoft Windows 2000");                    //1999年12月发布
36                         break;
37                     case 1:
38                         vname=_T("Microsoft Windows XP");                    //2001年8月发布
39                         break;
40                     case 2:
41                         if(os.wProductType==VER_NT_WORKSTATION && /
42  
43                            info.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_AMD64)
44  
45                             vname=_T("Microsoft Windows XP Professional x64 Edition");
46                         else if(GetSystemMetrics(SM_SERVERR2)==0)
47                             vname=_T("Microsoft Windows Server 2003");        //2003年3月发布
48                         else if(GetSystemMetrics(SM_SERVERR2)!=0)
49                             vname=_T("Microsoft Windows Server 2003 R2");
50                         break;
51                 }
52                 break;
53             case 6:
54                 switch(os.dwMinorVersion){
55                     case 0:
56                         if(os.wProductType==VER_NT_WORKSTATION)/*
57  
58                         VER_NT_WORKSTATION是桌面系统 */
59                             vname=_T("Microsoft Windows Vista");
60                         else
61                             vname=_T("Microsoft Windows Server 2008");          //服务器版本
62                         break;
63                     case 1:
64                         if(os.wProductType==VER_NT_WORKSTATION)
65                             vname=_T("Microsoft Windows 7");
66                         else
67                             vname=_T("Microsoft Windows Server 2008 R2");
68                         break;
69                 }
70                 break;
71             default:
72                 vname=_T("未知操作系统");
73         }
74         return vname;
75     }
76     else
77         return _T("");
78  
79 }

[代码] 返回具体的版本名称

01 CString getVersionMark()
02  
03 {
04  
05     OSVERSIONINFOEX os;
06     os.dwOSVersionInfoSize=sizeof(OSVERSIONINFOEX);
07     if(GetVersionEx((OSVERSIONINFO *)&os)){
08         CString vmark;
09  
10         switch(os.dwMajorVersion){                //先判断操作系统版本
11