首先看一看函数原型:
BOOL GetVersionEx(POSVERSIONINFO pVersionInformation);
typedef struct { DWORD dwOSVersionInfoSize; //在使用GetVersionEx之前要将此初始化为结构的大小 DWORD dwMajorVersion; //系统主版本号 DWORD dwMinorVersion; //系统次版本号 DWORD dwBuildNumber; //系统构建号 DWORD dwPlatformId; //系统支持的平台 TCHAR szCSDVersion[128]; //系统补丁包的名称 WORD wServicePackMajor; //系统补丁包的主版本 WORD wServicePackMinor; //系统补丁包的次版本 WORD wSuiteMask; //标识系统上的程序组 BYTE wProductType; //标识系统类型 BYTE wReserved; //保留,未使用 } OSVERSIONINFOEX, *POSVERSIONINFOEX;
OSVERSIONINFOEX os; os.dwOSVersionInfoSize=sizeof(os); if(!GetVersionEx((OSVERSIONINFO *)&os)) { return FALSE; }
下面是一个通过系统版本来判断操作系统名称的例子:
CString getSystemName() { SYSTEM_INFO info; //用SYSTEM_INFO结构判断64位AMD处理器 GetSystemInfo(&info); //调用GetSystemInfo函数填充结构 OSVERSIONINFOEX os; os.dwOSVersionInfoSize=sizeof(OSVERSIONINFOEX); if(GetVersionEx((OSVERSIONINFO *)&os)) { CString vname; //下面根据版本信息判断操作系统名称 switch(os.dwMajorVersion){ //判断主版本号 case 4: switch(os.dwMinorVersion){ //判断次版本号 case 0: if(os.dwPlatformId==VER_PLATFORM_WIN32_NT) vname=_T("Microsoft Windows NT 4.0"); //1996年7月发布 else if(os.dwPlatformId==VER_PLATFORM_WIN32_WINDOWS) vname=_T("Microsoft Windows 95"); break; case 10: vname=_T("Microsoft Windows 98"); break; case 90: vname=_T("Microsoft Windows Me"); break; } break; case 5: switch(os.dwMinorVersion){ //再比较dwMinorVersion的值 case 0: vname=_T("Microsoft Windows 2000"); //1999年12月发布 break; case 1: vname=_T("Microsoft Windows XP"); //2001年8月发布 break; case 2: if(os.wProductType==VER_NT_WORKSTATION && info.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_AMD64) vname=_T("Microsoft Windows XP Professional x64 Edition"); else if(GetSystemMetrics(SM_SERVERR2)==0) vname=_T("Microsoft Windows Server 2003"); //2003年3月发布 else if(GetSystemMetrics(SM_SERVERR2)!=0) vname=_T("Microsoft Windows Server 2003 R2"); break; } break; case 6: switch(os.dwMinorVersion){ case 0: if(os.wProductType==VER_NT_WORKSTATION) vname=_T("Microsoft Windows Vista"); else vname=_T("Microsoft Windows Server 2008"); //服务器版本 break; case 1: if(os.wProductType==VER_NT_WORKSTATION) vname=_T("Microsoft Windows 7"); else vname=_T("Microsoft Windows Server 2008 R2"); break; case 2: vname=_T("Microsoft Windows 7"); break; } break; default: vname=_T("未知操作系统"); } return vname; } else return _T(""); }
CString getVersionMark() { OSVERSIONINFOEX os; os.dwOSVersionInfoSize=sizeof(OSVERSIONINFOEX); if(GetVersionEx((OSVERSIONINFO *)&os)){ CString vmark; switch(os.dwMajorVersion){ //先判断操作系统版本 case 5: switch(os.dwMinorVersion){ case 0: //Windows 2000 if(os.wSuiteMask==VER_SUITE_ENTERPRISE) vmark=_T("Advanced Server"); break; case 1: //Windows XP if(os.wSuiteMask==VER_SUITE_EMBEDDEDNT) vmark=_T("Embedded"); else if(os.wSuiteMask==VER_SUITE_PERSONAL) vmark=_T("Home Edition"); else vmark=_T("Professional"); break; case 2: if(GetSystemMetrics(SM_SERVERR2)==0 && / os.wSuiteMask==VER_SUITE_BLADE) //Windows Server 2003 vmark=_T("Web Edition"); else if(GetSystemMetrics(SM_SERVERR2)==0 && os.wSuiteMask==VER_SUITE_COMPUTE_SERVER) vmark=_T("Compute Cluster Edition"); else if(GetSystemMetrics(SM_SERVERR2)==0 && os.wSuiteMask==VER_SUITE_STORAGE_SERVER) vmark=_T("Storage Server"); else if(GetSystemMetrics(SM_SERVERR2)==0 && os.wSuiteMask==VER_SUITE_DATACENTER) vmark=_T("Datacenter Edition"); else if(GetSystemMetrics(SM_SERVERR2)==0 && os.wSuiteMask==VER_SUITE_ENTERPRISE) vmark=_T("Enterprise Edition"); else if(GetSystemMetrics(SM_SERVERR2)!=0 && os.wSuiteMask==VER_SUITE_STORAGE_SERVER) vmark=_T("Storage Server"); break; } break; case 6: switch(os.dwMinorVersion){ case 0: if(os.wProductType!=VER_NT_WORKSTATION && os.wSuiteMask==VER_SUITE_DATACENTER) vmark=_T("Datacenter Server"); else if(os.wProductType!=VER_NT_WORKSTATION && os.wSuiteMask==VER_SUITE_ENTERPRISE) vmark=_T("Enterprise"); else if(os.wProductType==VER_NT_WORKSTATION && os.wSuiteMask==VER_SUITE_PERSONAL) //Windows Vista vmark=_T("Home"); break; } break; default: vmark=_T(""); } return vmark; } else return _T(""); }
FYI