這就需要使用 OSVERSIONINFOEX structure [1]
它的定義是這樣的
typedef struct _OSVERSIONINFOEX {
DWORD dwOSVersionInfoSize;
DWORD dwMajorVersion; //系統主版本
DWORD dwMinorVersion; //系統次版本
DWORD dwBuildNumber; //系統建構版本號
DWORD dwPlatformId; //系統平台ID
TCHAR szCSDVersion[128];
WORD wServicePackMajor;
WORD wServicePackMinor;
WORD wSuiteMask;
BYTE wProductType;
BYTE wReserved;
} OSVERSIONINFOEX, *POSVERSIONINFOEX, *LPOSVERSIONINFOEX;
可以用系統主版本與系統次版本來簡單判斷 XP 跟 win7
win7 = 6.1
xp = 5.1
其他的系統判定方法如下
作業系統 | 版本 | 主版本 | 次版本 | 其他條件 |
---|---|---|---|---|
Windows 7 | 6.1 | 6 | 1 | OSVERSIONINFOEX.wProductType == VER_NT_WORKSTATION |
Windows Server 2008 R2 | 6.1 | 6 | 1 | OSVERSIONINFOEX.wProductType != VER_NT_WORKSTATION |
Windows Server 2008 | 6.0 | 6 | 0 | OSVERSIONINFOEX.wProductType != VER_NT_WORKSTATION |
Windows Vista | 6.0 | 6 | 0 | OSVERSIONINFOEX.wProductType == VER_NT_WORKSTATION |
Windows Server 2003 R2 | 5.2 | 5 | 2 | GetSystemMetrics(SM_SERVERR2) != 0 |
Windows Home Server | 5.2 | 5 | 2 | OSVERSIONINFOEX.wSuiteMask & VER_SUITE_WH_SERVER |
Windows Server 2003 | 5.2 | 5 | 2 | GetSystemMetrics(SM_SERVERR2) == 0 |
Windows XP Professional x64 Edition | 5.2 | 5 | 2 | (OSVERSIONINFOEX.wProductType == VER_NT_WORKSTATION) && (SYSTEM_INFO.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_AMD64) |
Windows XP | 5.1 | 5 | 1 | Not applicable |
Windows 2000 | 5.0 | 5 | 0 | Not applicable |
系統測試結果
c++程式碼
#include <cstdlib>
#include <iostream>
#include <windows.h>
using namespace std;
int main(int argc, char *argv[]){
int a,b;
OSVERSIONINFO osvi;
ZeroMemory(&osvi, sizeof(OSVERSIONINFO));
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
GetVersionEx(&osvi);
a=osvi.dwMajorVersion; //主版本
b=osvi.dwMinorVersion; //次版本
printf("作業系統: ");
if(a==6&&b==1)printf("Windows 7 Ver:%d.%d\n",a,b);
if(a==6&&b==0)printf("Windows Vista Ver:%d.%d\n",a,b);
if(a==5&&b==0)printf("Windows 2000 Ver:%d.%d\n",a,b);
if(a==5&&b==1)printf("Windows XP Ver:%d.%d\n",a,b);
if(a==4&&b==0)printf("Windows NT Ver:%d.%d\n",a,b);
if(a==4&&b==10)printf("Windows 98 Ver:%d.%d\n",a,b);
if(a==4&&b==90)printf("Windows ME Ver:%d.%d\n",a,b);
system("PAUSE");
return 0;
}
參考資料:
[1].http://msdn.microsoft.com/en-us/library/windows/desktop/ms724833%28v=vs.85%29.aspx
[2].http://msdn.microsoft.com/zh-cn/library/ee663094.aspx
沒有留言:
張貼留言
請提供您的寶貴意見 ;-)