1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
33
34
35
36
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
50
51
52
53
54
55
56
57
58 public int getMajor() {
59 return this._versionInfo.dwMajorVersion.intValue();
60 }
61
62
63
64
65
66
67
68
69
70
71
72 public int getMinor() {
73 return this._versionInfo.dwMinorVersion.intValue();
74 }
75
76
77
78
79
80
81 public int getBuildNumber() {
82 return this._versionInfo.dwBuildNumber.intValue();
83 }
84
85
86
87
88
89
90 public int getPlatformId() {
91 return this._versionInfo.dwPlatformId.intValue();
92 }
93
94
95
96
97
98
99
100
101 public String getServicePack() {
102 return Native.toString(this._versionInfo.szCSDVersion);
103 }
104
105
106
107
108
109
110 public int getSuiteMask() {
111 return this._versionInfo.wSuiteMask.intValue();
112 }
113
114
115
116
117
118
119 public byte getProductType() {
120 return this._versionInfo.wProductType;
121 }
122
123 @Override
124 public String toString() {
125 String version = null;
126
127
128
129 if (getPlatformId() == WinNT.VER_PLATFORM_WIN32_NT) {
130
131 if (getMajor() == 6 && getMinor() == 3
132 && getProductType() == WinNT.VER_NT_WORKSTATION) {
133 version = "8.1";
134 }
135
136 else if (getMajor() == 6 && getMinor() == 3
137 && getProductType() != WinNT.VER_NT_WORKSTATION) {
138 version = "Server 2012 R2";
139 }
140
141 else if (getMajor() == 6 && getMinor() == 2
142 && getProductType() == WinNT.VER_NT_WORKSTATION) {
143 version = "8";
144 }
145
146 else if (getMajor() == 6 && getMinor() == 2
147 && getProductType() != WinNT.VER_NT_WORKSTATION) {
148 version = "Server 2012";
149 }
150
151 else if (getMajor() == 6 && getMinor() == 1
152 && getProductType() == WinNT.VER_NT_WORKSTATION) {
153 version = "7";
154 }
155
156 else if (getMajor() == 6 && getMinor() == 1
157 && getProductType() != WinNT.VER_NT_WORKSTATION) {
158 version = "Server 2008 R2";
159 }
160
161 else if (getMajor() == 6 && getMinor() == 0
162 && getProductType() != WinNT.VER_NT_WORKSTATION) {
163 version = "Server 2008";
164 }
165
166 else if (getMajor() == 6 && getMinor() == 0
167 && getProductType() == WinNT.VER_NT_WORKSTATION) {
168 version = "Vista";
169 }
170
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
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
185 else if (getMajor() == 5 && getMinor() == 2
186 && getProductType() == WinNT.VER_NT_WORKSTATION) {
187 version = "XP";
188 }
189
190 else if (getMajor() == 5 && getMinor() == 1) {
191 version = "XP";
192 }
193
194 else if (getMajor() == 5 && getMinor() == 0) {
195 version = "2000";
196 }
197
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 }