VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/win/RTSystemQueryOSInfo-win.cpp@ 96124

Last change on this file since 96124 was 95858, checked in by vboxsync, 2 years ago

IPRT/RTSystemQueryOSInfo-win.cpp: Use RTStrCat instead of strcat because we don't have a nocrt impl of it yet (lazy bird). [doxyfix] bugref:10261

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 17.3 KB
Line 
1/* $Id: RTSystemQueryOSInfo-win.cpp 95858 2022-07-26 23:56:16Z vboxsync $ */
2/** @file
3 * IPRT - RTSystemQueryOSInfo, generic stub.
4 */
5
6/*
7 * Copyright (C) 2008-2022 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*********************************************************************************************************************************
29* Header Files *
30*********************************************************************************************************************************/
31#include "internal/iprt.h"
32#include <iprt/win/windows.h>
33#include <WinUser.h>
34
35#include "internal-r3-win.h"
36#include <iprt/system.h>
37#include <iprt/assert.h>
38#include <iprt/ctype.h>
39#include <iprt/errcore.h>
40#include <iprt/string.h>
41#include <iprt/utf16.h>
42
43
44/*********************************************************************************************************************************
45* Structures and Typedefs *
46*********************************************************************************************************************************/
47
48/**
49 * These are the PRODUCT_* defines found in the Vista Platform SDK and returned
50 * by GetProductInfo().
51 *
52 * We define them ourselves because we don't necessarily have any Vista PSDK around.
53 */
54typedef enum RTWINPRODTYPE
55{
56 kRTWinProdType_UNDEFINED = 0x00000000, ///< An unknown product
57 kRTWinProdType_BUSINESS = 0x00000006, ///< Business Edition
58 kRTWinProdType_BUSINESS_N = 0x00000010, ///< Business Edition
59 kRTWinProdType_CLUSTER_SERVER = 0x00000012, ///< Cluster Server Edition
60 kRTWinProdType_DATACENTER_SERVER = 0x00000008, ///< Server Datacenter Edition (full installation)
61 kRTWinProdType_DATACENTER_SERVER_CORE = 0x0000000C, ///< Server Datacenter Edition (core installation)
62 kRTWinProdType_ENTERPRISE = 0x00000004, ///< Enterprise Edition
63 kRTWinProdType_ENTERPRISE_N = 0x0000001B, ///< Enterprise Edition
64 kRTWinProdType_ENTERPRISE_SERVER = 0x0000000A, ///< Server Enterprise Edition (full installation)
65 kRTWinProdType_ENTERPRISE_SERVER_CORE = 0x0000000E, ///< Server Enterprise Edition (core installation)
66 kRTWinProdType_ENTERPRISE_SERVER_IA64 = 0x0000000F, ///< Server Enterprise Edition for Itanium-based Systems
67 kRTWinProdType_HOME_BASIC = 0x00000002, ///< Home Basic Edition
68 kRTWinProdType_HOME_BASIC_N = 0x00000005, ///< Home Basic Edition
69 kRTWinProdType_HOME_PREMIUM = 0x00000003, ///< Home Premium Edition
70 kRTWinProdType_HOME_PREMIUM_N = 0x0000001A, ///< Home Premium Edition
71 kRTWinProdType_HOME_SERVER = 0x00000013, ///< Home Server Edition
72 kRTWinProdType_SERVER_FOR_SMALLBUSINESS = 0x00000018, ///< Server for Small Business Edition
73 kRTWinProdType_SMALLBUSINESS_SERVER = 0x00000009, ///< Small Business Server
74 kRTWinProdType_SMALLBUSINESS_SERVER_PREMIUM = 0x00000019, ///< Small Business Server Premium Edition
75 kRTWinProdType_STANDARD_SERVER = 0x00000007, ///< Server Standard Edition (full installation)
76 kRTWinProdType_STANDARD_SERVER_CORE = 0x0000000D, ///< Server Standard Edition (core installation)
77 kRTWinProdType_STARTER = 0x0000000B, ///< Starter Edition
78 kRTWinProdType_STORAGE_ENTERPRISE_SERVER = 0x00000017, ///< Storage Server Enterprise Edition
79 kRTWinProdType_STORAGE_EXPRESS_SERVER = 0x00000014, ///< Storage Server Express Edition
80 kRTWinProdType_STORAGE_STANDARD_SERVER = 0x00000015, ///< Storage Server Standard Edition
81 kRTWinProdType_STORAGE_WORKGROUP_SERVER = 0x00000016, ///< Storage Server Workgroup Edition
82 kRTWinProdType_ULTIMATE = 0x00000001, ///< Ultimate Edition
83 kRTWinProdType_ULTIMATE_N = 0x0000001C, ///< Ultimate Edition
84 kRTWinProdType_WEB_SERVER = 0x00000011, ///< Web Server Edition (full)
85 kRTWinProdType_WEB_SERVER_CORE = 0x0000001D ///< Web Server Edition (core)
86} RTWINPRODTYPE;
87
88
89/**
90 * Wrapper around the GetProductInfo API.
91 *
92 * @returns The vista type.
93 */
94static RTWINPRODTYPE rtSystemWinGetProductInfo(DWORD dwOSMajorVersion, DWORD dwOSMinorVersion, DWORD dwSpMajorVersion, DWORD dwSpMinorVersion)
95{
96 BOOL (WINAPI *pfnGetProductInfo)(DWORD, DWORD, DWORD, DWORD, PDWORD);
97 pfnGetProductInfo = (BOOL (WINAPI *)(DWORD, DWORD, DWORD, DWORD, PDWORD))GetProcAddress(GetModuleHandle("kernel32.dll"),
98 "GetProductInfo");
99 if (pfnGetProductInfo)
100 {
101 DWORD dwProductType = kRTWinProdType_UNDEFINED;
102 if (pfnGetProductInfo(dwOSMajorVersion, dwOSMinorVersion, dwSpMajorVersion, dwSpMinorVersion, &dwProductType))
103 return (RTWINPRODTYPE)dwProductType;
104 }
105 return kRTWinProdType_UNDEFINED;
106}
107
108
109
110/**
111 * Appends the product type if available (Vista & 2003 only for some reason).
112 *
113 * @param pszTmp The buffer.
114 * @param cbTmp The buffer size.
115 */
116static void rtSystemWinAppendProductType(char *pszTmp, size_t cbTmp)
117{
118 RTWINPRODTYPE enmVistaType = rtSystemWinGetProductInfo(6, 0, 0, 0);
119 switch (enmVistaType)
120 {
121 case kRTWinProdType_BUSINESS: RTStrCat(pszTmp, cbTmp, " Business Edition"); break;
122 case kRTWinProdType_BUSINESS_N: RTStrCat(pszTmp, cbTmp, " Business Edition"); break;
123 case kRTWinProdType_CLUSTER_SERVER: RTStrCat(pszTmp, cbTmp, " Cluster Server Edition"); break;
124 case kRTWinProdType_DATACENTER_SERVER: RTStrCat(pszTmp, cbTmp, " Server Datacenter Edition (full installation)"); break;
125 case kRTWinProdType_DATACENTER_SERVER_CORE: RTStrCat(pszTmp, cbTmp, " Server Datacenter Edition (core installation)"); break;
126 case kRTWinProdType_ENTERPRISE: RTStrCat(pszTmp, cbTmp, " Enterprise Edition"); break;
127 case kRTWinProdType_ENTERPRISE_N: RTStrCat(pszTmp, cbTmp, " Enterprise Edition"); break;
128 case kRTWinProdType_ENTERPRISE_SERVER: RTStrCat(pszTmp, cbTmp, " Server Enterprise Edition (full installation)"); break;
129 case kRTWinProdType_ENTERPRISE_SERVER_CORE: RTStrCat(pszTmp, cbTmp, " Server Enterprise Edition (core installation)"); break;
130 case kRTWinProdType_ENTERPRISE_SERVER_IA64: RTStrCat(pszTmp, cbTmp, " Server Enterprise Edition for Itanium-based Systems"); break;
131 case kRTWinProdType_HOME_BASIC: RTStrCat(pszTmp, cbTmp, " Home Basic Edition"); break;
132 case kRTWinProdType_HOME_BASIC_N: RTStrCat(pszTmp, cbTmp, " Home Basic Edition"); break;
133 case kRTWinProdType_HOME_PREMIUM: RTStrCat(pszTmp, cbTmp, " Home Premium Edition"); break;
134 case kRTWinProdType_HOME_PREMIUM_N: RTStrCat(pszTmp, cbTmp, " Home Premium Edition"); break;
135 case kRTWinProdType_HOME_SERVER: RTStrCat(pszTmp, cbTmp, " Home Server Edition"); break;
136 case kRTWinProdType_SERVER_FOR_SMALLBUSINESS: RTStrCat(pszTmp, cbTmp, " Server for Small Business Edition"); break;
137 case kRTWinProdType_SMALLBUSINESS_SERVER: RTStrCat(pszTmp, cbTmp, " Small Business Server"); break;
138 case kRTWinProdType_SMALLBUSINESS_SERVER_PREMIUM: RTStrCat(pszTmp, cbTmp, " Small Business Server Premium Edition"); break;
139 case kRTWinProdType_STANDARD_SERVER: RTStrCat(pszTmp, cbTmp, " Server Standard Edition (full installation)"); break;
140 case kRTWinProdType_STANDARD_SERVER_CORE: RTStrCat(pszTmp, cbTmp, " Server Standard Edition (core installation)"); break;
141 case kRTWinProdType_STARTER: RTStrCat(pszTmp, cbTmp, " Starter Edition"); break;
142 case kRTWinProdType_STORAGE_ENTERPRISE_SERVER: RTStrCat(pszTmp, cbTmp, " Storage Server Enterprise Edition"); break;
143 case kRTWinProdType_STORAGE_EXPRESS_SERVER: RTStrCat(pszTmp, cbTmp, " Storage Server Express Edition"); break;
144 case kRTWinProdType_STORAGE_STANDARD_SERVER: RTStrCat(pszTmp, cbTmp, " Storage Server Standard Edition"); break;
145 case kRTWinProdType_STORAGE_WORKGROUP_SERVER: RTStrCat(pszTmp, cbTmp, " Storage Server Workgroup Edition"); break;
146 case kRTWinProdType_ULTIMATE: RTStrCat(pszTmp, cbTmp, " Ultimate Edition"); break;
147 case kRTWinProdType_ULTIMATE_N: RTStrCat(pszTmp, cbTmp, " Ultimate Edition"); break;
148 case kRTWinProdType_WEB_SERVER: RTStrCat(pszTmp, cbTmp, " Web Server Edition (full installation)"); break;
149 case kRTWinProdType_WEB_SERVER_CORE: RTStrCat(pszTmp, cbTmp, " Web Server Edition (core installation)"); break;
150 case kRTWinProdType_UNDEFINED: break;
151 }
152}
153
154
155/**
156 * Services the RTSYSOSINFO_PRODUCT, RTSYSOSINFO_RELEASE
157 * and RTSYSOSINFO_SERVICE_PACK requests.
158 *
159 * @returns See RTSystemQueryOSInfo.
160 * @param enmInfo See RTSystemQueryOSInfo.
161 * @param pszInfo See RTSystemQueryOSInfo.
162 * @param cchInfo See RTSystemQueryOSInfo.
163 */
164static int rtSystemWinQueryOSVersion(RTSYSOSINFO enmInfo, char *pszInfo, size_t cchInfo)
165{
166 /*
167 * Make sure it's terminated correctly in case of error.
168 */
169 *pszInfo = '\0';
170
171 /*
172 * Check that we got the windows version at init time.
173 */
174 AssertReturn(g_WinOsInfoEx.dwOSVersionInfoSize, VERR_WRONG_ORDER);
175
176 /*
177 * Service the request.
178 */
179 char szTmp[512];
180 szTmp[0] = '\0';
181 switch (enmInfo)
182 {
183 /*
184 * The product name.
185 */
186 case RTSYSOSINFO_PRODUCT:
187 {
188 switch (g_enmWinVer)
189 {
190 case kRTWinOSType_95: strcpy(szTmp, "Windows 95"); break;
191 case kRTWinOSType_95SP1: strcpy(szTmp, "Windows 95 (Service Pack 1)"); break;
192 case kRTWinOSType_95OSR2: strcpy(szTmp, "Windows 95 (OSR 2)"); break;
193 case kRTWinOSType_98: strcpy(szTmp, "Windows 98"); break;
194 case kRTWinOSType_98SP1: strcpy(szTmp, "Windows 98 (Service Pack 1)"); break;
195 case kRTWinOSType_98SE: strcpy(szTmp, "Windows 98 (Second Edition)"); break;
196 case kRTWinOSType_ME: strcpy(szTmp, "Windows Me"); break;
197 case kRTWinOSType_NT310: strcpy(szTmp, "Windows NT 3.10"); break;
198 case kRTWinOSType_NT350: strcpy(szTmp, "Windows NT 3.50"); break;
199 case kRTWinOSType_NT351: strcpy(szTmp, "Windows NT 3.51"); break;
200 case kRTWinOSType_NT4: strcpy(szTmp, "Windows NT 4.0"); break;
201 case kRTWinOSType_2K: strcpy(szTmp, "Windows 2000"); break;
202 case kRTWinOSType_XP:
203 strcpy(szTmp, "Windows XP");
204 if (g_WinOsInfoEx.wSuiteMask & VER_SUITE_PERSONAL)
205 RTStrCat(szTmp, sizeof(szTmp), " Home");
206 if ( g_WinOsInfoEx.wProductType == VER_NT_WORKSTATION
207 && !(g_WinOsInfoEx.wSuiteMask & VER_SUITE_PERSONAL))
208 RTStrCat(szTmp, sizeof(szTmp), " Professional");
209#if 0 /** @todo fixme */
210 if (GetSystemMetrics(SM_MEDIACENTER))
211 RTStrCat(szTmp, sizeof(szTmp), " Media Center");
212#endif
213 break;
214
215 case kRTWinOSType_2003: strcpy(szTmp, "Windows 2003"); break;
216 case kRTWinOSType_VISTA:
217 {
218 strcpy(szTmp, "Windows Vista");
219 rtSystemWinAppendProductType(szTmp, sizeof(szTmp));
220 break;
221 }
222 case kRTWinOSType_2008: strcpy(szTmp, "Windows 2008"); break;
223 case kRTWinOSType_7: strcpy(szTmp, "Windows 7"); break;
224 case kRTWinOSType_2008R2: strcpy(szTmp, "Windows 2008 R2"); break;
225 case kRTWinOSType_8: strcpy(szTmp, "Windows 8"); break;
226 case kRTWinOSType_2012: strcpy(szTmp, "Windows 2012"); break;
227 case kRTWinOSType_81: strcpy(szTmp, "Windows 8.1"); break;
228 case kRTWinOSType_2012R2: strcpy(szTmp, "Windows 2012 R2"); break;
229 case kRTWinOSType_10: strcpy(szTmp, "Windows 10"); break;
230 case kRTWinOSType_2016: strcpy(szTmp, "Windows 2016"); break;
231 case kRTWinOSType_2019: strcpy(szTmp, "Windows 2019"); break;
232 case kRTWinOSType_2022: strcpy(szTmp, "Windows 2022"); break;
233 case kRTWinOSType_11: strcpy(szTmp, "Windows 11"); break;
234
235 case kRTWinOSType_NT_UNKNOWN:
236 RTStrPrintf(szTmp, sizeof(szTmp), "Unknown NT v%u.%u",
237 g_WinOsInfoEx.dwMajorVersion, g_WinOsInfoEx.dwMinorVersion);
238 break;
239
240 default:
241 AssertFailed();
242 case kRTWinOSType_UNKNOWN:
243 RTStrPrintf(szTmp, sizeof(szTmp), "Unknown %d v%u.%u",
244 g_WinOsInfoEx.dwPlatformId, g_WinOsInfoEx.dwMajorVersion, g_WinOsInfoEx.dwMinorVersion);
245 break;
246 }
247 break;
248 }
249
250 /*
251 * The release.
252 */
253 case RTSYSOSINFO_RELEASE:
254 {
255 RTStrPrintf(szTmp, sizeof(szTmp), "%u.%u.%u",
256 g_WinOsInfoEx.dwMajorVersion, g_WinOsInfoEx.dwMinorVersion, g_WinOsInfoEx.dwBuildNumber);
257 break;
258 }
259
260
261 /*
262 * Get the service pack.
263 */
264 case RTSYSOSINFO_SERVICE_PACK:
265 {
266 if (g_WinOsInfoEx.wServicePackMajor)
267 {
268 if (g_WinOsInfoEx.wServicePackMinor)
269 RTStrPrintf(szTmp, sizeof(szTmp), "%u.%u",
270 (unsigned)g_WinOsInfoEx.wServicePackMajor, (unsigned)g_WinOsInfoEx.wServicePackMinor);
271 else
272 RTStrPrintf(szTmp, sizeof(szTmp), "%u",
273 (unsigned)g_WinOsInfoEx.wServicePackMajor);
274 }
275 else if (g_WinOsInfoEx.szCSDVersion[0])
276 {
277 /* just copy the entire string. */
278 char *pszTmp = szTmp;
279 int rc = RTUtf16ToUtf8Ex(g_WinOsInfoEx.szCSDVersion, RT_ELEMENTS(g_WinOsInfoEx.szCSDVersion),
280 &pszTmp, sizeof(szTmp), NULL);
281 if (RT_SUCCESS(rc))
282 RTStrStripR(szTmp);
283 else
284 szTmp[0] = '\0';
285 AssertCompile(sizeof(szTmp) > sizeof(g_WinOsInfoEx.szCSDVersion));
286 }
287 else
288 {
289 switch (g_enmWinVer)
290 {
291 case kRTWinOSType_95SP1: strcpy(szTmp, "1"); break;
292 case kRTWinOSType_98SP1: strcpy(szTmp, "1"); break;
293 default:
294 break;
295 }
296 }
297 break;
298 }
299
300 default:
301 AssertFatalFailed();
302 }
303
304 /*
305 * Copy the result to the return buffer.
306 */
307 size_t cchTmp = strlen(szTmp);
308 Assert(cchTmp < sizeof(szTmp));
309 if (cchTmp < cchInfo)
310 {
311 memcpy(pszInfo, szTmp, cchTmp + 1);
312 return VINF_SUCCESS;
313 }
314 memcpy(pszInfo, szTmp, cchInfo - 1);
315 pszInfo[cchInfo - 1] = '\0';
316 return VERR_BUFFER_OVERFLOW;
317}
318
319
320RTDECL(int) RTSystemQueryOSInfo(RTSYSOSINFO enmInfo, char *pszInfo, size_t cchInfo)
321{
322 /*
323 * Quick validation.
324 */
325 AssertReturn(enmInfo > RTSYSOSINFO_INVALID && enmInfo < RTSYSOSINFO_END, VERR_INVALID_PARAMETER);
326 AssertPtrReturn(pszInfo, VERR_INVALID_POINTER);
327 if (!cchInfo)
328 return VERR_BUFFER_OVERFLOW;
329
330
331 /*
332 * Handle the request.
333 */
334 switch (enmInfo)
335 {
336 case RTSYSOSINFO_PRODUCT:
337 case RTSYSOSINFO_RELEASE:
338 case RTSYSOSINFO_SERVICE_PACK:
339 return rtSystemWinQueryOSVersion(enmInfo, pszInfo, cchInfo);
340
341 case RTSYSOSINFO_VERSION:
342 default:
343 *pszInfo = '\0';
344 }
345
346 return VERR_NOT_SUPPORTED;
347}
348
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette