View Javadoc
1   /**
2    * Oshi (https://github.com/dblock/oshi)
3    * 
4    * Copyright (c) 2010 - 2015 The Oshi Project Team
5    * 
6    * All rights reserved. This program and the accompanying materials
7    * are made available under the terms of the Eclipse Public License v1.0
8    * which accompanies this distribution, and is available at
9    * http://www.eclipse.org/legal/epl-v10.html
10   * 
11   * Contributors:
12   * dblock[at]dblock[dot]org
13   * alessandro[at]perucchi[dot]org
14   * widdis[at]gmail[dot]com
15   * https://github.com/dblock/oshi/graphs/contributors
16   */
17  package oshi.software.os.windows.nt;
18  
19  import oshi.software.os.OperatingSystemVersion;
20  
21  import com.sun.jna.Native;
22  import com.sun.jna.platform.win32.Advapi32Util;
23  import com.sun.jna.platform.win32.Kernel32;
24  import com.sun.jna.platform.win32.User32;
25  import com.sun.jna.platform.win32.Win32Exception;
26  import com.sun.jna.platform.win32.WinNT;
27  import com.sun.jna.platform.win32.WinNT.OSVERSIONINFOEX;
28  import com.sun.jna.platform.win32.WinReg;
29  import com.sun.jna.platform.win32.WinUser;
30  
31  /**
32   * Contains operating system version information. The information includes major
33   * and minor version numbers, a build number, a platform identifier, and
34   * descriptive text about the operating system.
35   * 
36   * @author dblock[at]dblock[dot]org
37   */
38  public class OSVersionInfoEx implements OperatingSystemVersion {
39  	private OSVERSIONINFOEX _versionInfo;
40  
41  	public OSVersionInfoEx() {
42  		this._versionInfo = new OSVERSIONINFOEX();
43  		if (!Kernel32.INSTANCE.GetVersionEx(this._versionInfo)) {
44  			throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
45  		}
46  	}
47  
48  	/**
49  	 * The major version number of the operating system.
50  	 * 
51  	 * @return The major version within the following supported operating
52  	 *         systems. Windows 8: 6.2 Windows Server 2012: 6.2 Windows 7: 6.1
53  	 *         Windows Server 2008 R2: 6.1 Windows Server 2008: 6.0 Windows
54  	 *         Vista: 6.0 Windows Server 2003 R2: 5.2 Windows Home Server: 5.2
55  	 *         Windows Server 2003: 5.2 Windows XP Professional x64 Edition: 5.2
56  	 *         Windows XP: 5.1 Windows 2000: 5.0
57  	 */
58  	public int getMajor() {
59  		return this._versionInfo.dwMajorVersion.intValue();
60  	}
61  
62  	/**
63  	 * The minor version number of the operating system.
64  	 * 
65  	 * @return The minor version within the following supported operating
66  	 *         systems. Windows 8: 6.2 Windows Server 2012: 6.2 Windows 7: 6.1
67  	 *         Windows Server 2008 R2: 6.1 Windows Server 2008: 6.0 Windows
68  	 *         Vista: 6.0 Windows Server 2003 R2: 5.2 Windows Home Server: 5.2
69  	 *         Windows Server 2003: 5.2 Windows XP Professional x64 Edition: 5.2
70  	 *         Windows XP: 5.1 Windows 2000: 5.0
71  	 */
72  	public int getMinor() {
73  		return this._versionInfo.dwMinorVersion.intValue();
74  	}
75  
76  	/**
77  	 * The build number of the operating system.
78  	 * 
79  	 * @return Build number.
80  	 */
81  	public int getBuildNumber() {
82  		return this._versionInfo.dwBuildNumber.intValue();
83  	}
84  
85  	/**
86  	 * The operating system platform. This member can be VER_PLATFORM_WIN32_NT.
87  	 * 
88  	 * @return Platform ID.
89  	 */
90  	public int getPlatformId() {
91  		return this._versionInfo.dwPlatformId.intValue();
92  	}
93  
94  	/**
95  	 * String, such as "Service Pack 3", that indicates the latest Service Pack
96  	 * installed on the system. If no Service Pack has been installed, the
97  	 * string is empty.
98  	 * 
99  	 * @return Service pack.
100 	 */
101 	public String getServicePack() {
102 		return Native.toString(this._versionInfo.szCSDVersion);
103 	}
104 
105 	/**
106 	 * A bit mask that identifies the product suites available on the system.
107 	 * 
108 	 * @return Suite mask.
109 	 */
110 	public int getSuiteMask() {
111 		return this._versionInfo.wSuiteMask.intValue();
112 	}
113 
114 	/**
115 	 * Any additional information about the system.
116 	 * 
117 	 * @return Product type.
118 	 */
119 	public byte getProductType() {
120 		return this._versionInfo.wProductType;
121 	}
122 
123 	@Override
124 	public String toString() {
125 		String version = null;
126 
127 		// see
128 		// http://msdn.microsoft.com/en-us/library/windows/desktop/ms724833%28v=vs.85%29.aspx
129 		if (getPlatformId() == WinNT.VER_PLATFORM_WIN32_NT) {
130 			// 8.1
131 			if (getMajor() == 6 && getMinor() == 3
132 					&& getProductType() == WinNT.VER_NT_WORKSTATION) {
133 				version = "8.1";
134 			}
135 			// Server 2008 R2
136 			else if (getMajor() == 6 && getMinor() == 3
137 					&& getProductType() != WinNT.VER_NT_WORKSTATION) {
138 				version = "Server 2012 R2";
139 			}
140 			// 8
141 			else if (getMajor() == 6 && getMinor() == 2
142 					&& getProductType() == WinNT.VER_NT_WORKSTATION) {
143 				version = "8";
144 			}
145 			// Server 2008
146 			else if (getMajor() == 6 && getMinor() == 2
147 					&& getProductType() != WinNT.VER_NT_WORKSTATION) {
148 				version = "Server 2012";
149 			}
150 			// 7
151 			else if (getMajor() == 6 && getMinor() == 1
152 					&& getProductType() == WinNT.VER_NT_WORKSTATION) {
153 				version = "7";
154 			}
155 			// Server 2008 R2
156 			else if (getMajor() == 6 && getMinor() == 1
157 					&& getProductType() != WinNT.VER_NT_WORKSTATION) {
158 				version = "Server 2008 R2";
159 			}
160 			// Server 2008
161 			else if (getMajor() == 6 && getMinor() == 0
162 					&& getProductType() != WinNT.VER_NT_WORKSTATION) {
163 				version = "Server 2008";
164 			}
165 			// Vista
166 			else if (getMajor() == 6 && getMinor() == 0
167 					&& getProductType() == WinNT.VER_NT_WORKSTATION) {
168 				version = "Vista";
169 			}
170 			// Server 2003
171 			else if (getMajor() == 5
172 					&& getMinor() == 2
173 					&& getProductType() != WinNT.VER_NT_WORKSTATION
174 					&& User32.INSTANCE.GetSystemMetrics(WinUser.SM_SERVERR2) != 0) {
175 				version = "Server 2003";
176 			}
177 			// Server 2003 R2
178 			else if (getMajor() == 5
179 					&& getMinor() == 2
180 					&& getProductType() != WinNT.VER_NT_WORKSTATION
181 					&& User32.INSTANCE.GetSystemMetrics(WinUser.SM_SERVERR2) == 0) {
182 				version = "Server 2003 R2";
183 			}
184 			// XP 64 bit
185 			else if (getMajor() == 5 && getMinor() == 2
186 					&& getProductType() == WinNT.VER_NT_WORKSTATION) {
187 				version = "XP";
188 			}
189 			// XP 32 bit
190 			else if (getMajor() == 5 && getMinor() == 1) {
191 				version = "XP";
192 			}
193 			// 2000
194 			else if (getMajor() == 5 && getMinor() == 0) {
195 				version = "2000";
196 			}
197 			// Windows NT
198 			else if (getMajor() == 4) {
199 				version = "NT 4";
200 
201 				if ("Service Pack 6".equals(getServicePack())) {
202 					if (Advapi32Util
203 							.registryKeyExists(WinReg.HKEY_LOCAL_MACHINE,
204 									"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Hotfix\\Q246009")) {
205 						return "NT4 SP6a";
206 					}
207 				}
208 
209 			} else {
210 				throw new RuntimeException("Unsupported Windows NT version: "
211 						+ this._versionInfo.toString());
212 			}
213 
214 			if (this._versionInfo.wServicePackMajor.intValue() > 0) {
215 				version = version + " SP"
216 						+ this._versionInfo.wServicePackMajor.intValue();
217 			}
218 
219 		} else if (getPlatformId() == WinNT.VER_PLATFORM_WIN32_WINDOWS) {
220 			if (getMajor() == 4 && getMinor() == 90) {
221 				version = "ME";
222 			} else if (getMajor() == 4 && getMinor() == 10) {
223 				if (this._versionInfo.szCSDVersion[1] == 'A') {
224 					version = "98 SE";
225 				} else {
226 					version = "98";
227 				}
228 			} else if (getMajor() == 4 && getMinor() == 0) {
229 				if (this._versionInfo.szCSDVersion[1] == 'C'
230 						|| this._versionInfo.szCSDVersion[1] == 'B') {
231 					version = "95 OSR2";
232 				} else {
233 					version = "95";
234 				}
235 			} else {
236 				throw new RuntimeException("Unsupported Windows 9x version: "
237 						+ this._versionInfo.toString());
238 			}
239 		} else {
240 			throw new RuntimeException("Unsupported Windows platform: "
241 					+ this._versionInfo.toString());
242 		}
243 
244 		return version;
245 	}
246 
247 	public OSVersionInfoEx(OSVERSIONINFOEX versionInfo) {
248 		this._versionInfo = versionInfo;
249 	}
250 }