《编程获取Windows的版本.pdf》由会员分享,可在线阅读,更多相关《编程获取Windows的版本.pdf(4页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、WindowsWindows 编程编程第 1 页 共 4 页编程获取编程获取 Windows 的版本的版本摘摘要:要:介绍通过编程的方法判断 Windows 是 XP、win7 还是 win8。用到的内容包括函数GetVersionEx,结构体 OSVERSIONINFO 和 OSVERSIONINFOEX。关键字:关键字:GetVersionExOSVERSIONINFOOSVERSIONINFOEX版本判断版本判断一首先给出示例代码/*函 数 名:GetSysVersion参数:返 回 值:stringXP2000Vistawin7win8win8.1false 失败old2000 以前的
2、版本功能:获取操作系统版本*/stringGetSysVersion()OSVERSIONINFOOSVersionInfo;memset(&OSVersionInfo,0,sizeof(OSVERSIONINFO);OSVersionInfo.dwOSVersionInfoSize=sizeof(OSVERSIONINFO);if(!GetVersionEx(&OSVersionInfo)returnfalse;if(VER_PLATFORM_WIN32_NT=OSVersionInfo.dwPlatformId)if(5=OSVersionInfo.dwMajorVersion&0=OSV
3、ersionInfo.dwMinorVersion)return2000;if(5=OSVersionInfo.dwMajorVersion&1=OSVersionInfo.dwMinorVersion)returnXP;if(6=OSVersionInfo.dwMajorVersion&0=OSVersionInfo.dwMinorVersion)returnVista;if(6=OSVersionInfo.dwMajorVersion&1=OSVersionInfo.dwMinorVersion)returnwin7;if(6=OSVersionInfo.dwMajorVersion&2=
4、OSVersionInfo.dwMinorVersion)returnwin8;if(6=OSVersionInfo.dwMajorVersion&3=OSVersionInfo.dwMinorVersion)returnwin8.1;WindowsWindows 编程编程第 2 页 共 4 页elsereturnold;returnfalse;二二对用到的函数和结构体作一下说明对用到的函数和结构体作一下说明OSVERSIONINFOtypedef struct _OSVERSIONINFODWORD dwOSVersionInfoSize;/这个结构体的大小,在使用前必须初始化DWORD d
5、wMajorVersion;/主版本号DWORD dwMinorVersion;/副版本号DWORD dwBuildNumber;/buildnumberDWORD dwPlatformId;/平台的 IDVER_PLATFORM_WIN32_NTTCHAR szCSDVersion 128;/一些附加的信息如Service Pack 3 OSVERSIONINFO;关于主版本号和副版本号见第三部分的对照表。OSVERSIONINFOEXtypedef struct _OSVERSIONINFOEX DWORD dwOSVersionInfoSize;DWORD dwMajorVersion;
6、DWORD dwMinorVersion;DWORD dwBuildNumber;DWORD dwPlatformId;TCHAR szCSDVersion128;WORD wServicePackMajor;/服务升级包主版本号WORD wServicePackMinor;WORD wSuiteMask;BYTE wProductType;BYTE wReserved;OSVERSIONINFOEX,*POSVERSIONINFOEX,*LPOSVERSIONINFOEX;是上面结构体体的扩展,增加了部分信息。具体见 msdn扩展的信息如是家庭版等。GetVersionExBOOL GetV
7、ersionEx(LPOSVERSIONINFOlpVersionInformation/pointer to version information structure);功能:获取系统的版本信息。参数:LPOSVERSIONINFOlpVersionInformation 指向 OSVERSIONINFO 的结构体。WindowsWindows 编程编程第 3 页 共 4 页这个参数也可以是 OSVERSIONINFOEX 结构体的指针(要进行强制转换)。上面的两个结构体在使用前都要对 dwOSVersionInfoSize 成员变量赋值的。返回值:成功返回 TRUE,失败 FALSE。本
8、函数的返回值为布尔类型。在 windows 提供的示例程序中,先给 GetVersionEx 传一个 OSVERSIONINFOEX 结构体指针,如果本函数返回 false。再调用,给其传一个OSVERSIONINFO 的指针。三三版本信息对照表版本信息对照表OperatinOperating g systemsystemVersVersionionnumnumberberdwMadwMajorVerjorVersionsiondwMdwMinorVinorVersioersion nOtherOtherWindows8.16.3*63OSVERSIONINFOEX.wProductType=
9、VER_NT_WORKSTATIONWindowsServer2012 R26.3*63OSVERSIONINFOEX.wProductType!=VER_NT_WORKSTATIONWindows86.262OSVERSIONINFOEX.wProductType=VER_NT_WORKSTATIONWindowsServer20126.262OSVERSIONINFOEX.wProductType!=VER_NT_WORKSTATIONWindows76.161OSVERSIONINFOEX.wProductType=VER_NT_WORKSTATIONWindowsServer2008
10、R26.161OSVERSIONINFOEX.wProductType!=VER_NT_WORKSTATIONWindowsWindows 编程编程第 4 页 共 4 页WindowsServer20086.060OSVERSIONINFOEX.wProductType!=VER_NT_WORKSTATIONWindowsVista6.060OSVERSIONINFOEX.wProductType=VER_NT_WORKSTATIONWindowsServer2003 R25.252GetSystemMetrics(SM_SERVERR2)!=0WindowsHomeServer5.252OS
11、VERSIONINFOEX.wSuiteMask&VER_SUITE_WH_SERVERWindowsServer20035.252GetSystemMetrics(SM_SERVERR2)=0WindowsXPProfessionalx64Edition5.252(OSVERSIONINFOEX.wProductType=VER_NT_WORKSTATION)&(SYSTEM_INFO.wProcessorArchitecture=PROCESSOR_ARCHITECTURE_AMD64)WindowsXP5.151Not applicableWindows20005.050Not applicable更多内容请访问:更多内容请访问: