OWLNext    7.0
Borland's Object Windows Library for the modern age
Loading...
Searching...
No Matches
system.cpp
Go to the documentation of this file.
1//
2/// \file system.cpp
3/// Implementation of TSystem class
4//
5// Part of OWLNext - the next generation Object Windows Library
6// Copyright (c) 1995, 1997 by Borland International, All Rights Reserved.
7//
8// For more information, including license details, see
9// http://owlnext.sourceforge.net
10//
11
12#include <owl/pch.h>
13#include <owl/system.h>
14#include <owl/module.h>
15#include <owl/registry.h>
16#include <map>
17
18#if defined(__BORLANDC__)
19# pragma warn -rvl // Turn off W8070 "Function should return a value" due to our assembler code.
20#endif
21
22using namespace owl;
23
24//
25// Local helpers
26//
27namespace
28{
29
30 //
31 // Returns an initialized system info object.
32 //
33 static const SYSTEM_INFO& GetSystemInfo_()
34 {
35 struct TLocal
36 {
37 static SYSTEM_INFO Init()
38 {
41 return s;
42 }
43 };
44 static SYSTEM_INFO systemInfo = TLocal::Init();
45 return systemInfo;
46 }
47
48} // namespace
49
50namespace owl
51{
52
53//
54/// Retrieves the product edition for the operating system on the local computer,
55/// and maps the type to the product types supported by the specified operating system, e.g.
56/// PRODUCT_HOME_BASIC, PRODUCT_HOME_PREMIUM, PRODUCT_PROFESSIONAL, PRODUCT_ENTERPRISE.
57/// This function is only supported on Vista and later, and returns 0 (PRODUCT_UNDEFINED) on XP.
58/// For more information see GetProductInfo in the Windows API documentation.
59//
61{
62 static auto productType = []
63 {
64 auto p = DWORD{PRODUCT_UNDEFINED};
65 const auto r = ::GetProductInfo(GetMajorVersion(), GetMinorVersion(), 0, 0, &p);
66 CHECK(r); InUse(r);
67 return p;
68 }();
69 return productType;
70}
71
72//
73/// Returns the product version for "kernel32.dll".
74//
76{
77 static TModuleVersionInfo systemVersion{_T("kernel32.dll")};
78 return systemVersion;
79}
80
82{
83 return GetVersionInfo().GetProductVersionMS() >> 16;
84}
85
87{
88 return GetVersionInfo().GetProductVersionMS() & 0x0000FFFFu;
89}
90
92{
93 return GetVersionInfo().GetProductVersionLS() >> 16;
94}
95
97{
98 return GetVersionInfo().GetProductVersion();
99}
100
101//
102/// Returns true if the OS is 64-bit.
103//
105{
106
107#ifdef _WIN64
108
109 return true;
110
111#else
112
113 static const auto is64Bit = []
114 {
115 auto is64Bit = BOOL{FALSE};
117 WARN(!r, _T("IsWow64Process failed. LastError: ") << GetLastError()); InUse(r);
118 return is64Bit;
119 }();
120 return is64Bit != FALSE;
121
122#endif
123
124}
125
126//
127/// Returns the value of the SYSTEM_INFO::dwNumberOfProcessors field returned by ::GetSystemInfo.
128/// Note that the value may specify the number of CPU cores, not necessarily the number of physical
129/// processor packages/chips. See the Windows API for details.
130//
132{
133 return static_cast<int>(GetSystemInfo_().dwNumberOfProcessors);
134}
135
136//
137/// Returns the value of the SYSTEM_INFO::wProcessorArchitecture field returned by ::GetSystemInfo.
138/// See the Windows API for details of possible return values.
139/// Use GetProcessorArchitectureName to get a description string.
140//
142{
143 return GetSystemInfo_().wProcessorArchitecture;
144}
145
146//
147/// Returns the description string for the given processor architecture.
148/// If the given architecture ID is unknown, then an empty string is returned.
149/// See GetProcessorArchitecture.
150//
152{
153 typedef std::map<uint, tstring> TMap;
154 typedef TMap::value_type T;
155 const T i[] =
156 {
157 T(PROCESSOR_ARCHITECTURE_INTEL, _T("IA-32 (x86)")),
159 T(PROCESSOR_ARCHITECTURE_ALPHA, _T("Alpha")),
163 T(PROCESSOR_ARCHITECTURE_IA64, _T("IA-64")),
164 T(PROCESSOR_ARCHITECTURE_ALPHA64, _T("Alpha64")),
166 T(PROCESSOR_ARCHITECTURE_AMD64, _T("AMD64 (x86-64)")),
167 T(PROCESSOR_ARCHITECTURE_IA32_ON_WIN64, _T("IA-32 on IA-64"))
168 };
169 static const TMap m(i, i + COUNTOF(i));
170 TMap::const_iterator r = m.find(architecture);
171 return r == m.end() ? tstring() : r->second;
172}
173
174//
175/// Returns info for the current processor.
176//
182
183} // namespace
184
185//-------------------------------------------------------------------------------------------------
186
187//
188// Local helpers for looking up processor information in the Windows Registry.
189//
190namespace
191{
192
193 template <class TReturn>
195 {
196 const auto keyName = _T("HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\") + to_tstring(cpuIndex);
197 const auto k = TRegKey::GetLocalMachine().GetSubkey(keyName);
198 return k ? k->GetValueOrDefault(value, TReturn{}) : TReturn{};
199 }
200
202 {
203 return static_cast<int>(GetCpuInfo_<uint32>(cpuIndex, _T("~MHz")));
204 }
205
207 {
208 return GetCpuInfo_<tstring>(cpuIndex, _T("ProcessorNameString"));
209 }
210
212 {
213 return GetCpuInfo_<tstring>(cpuIndex, _T("VendorIdentifier"));
214 }
215
217 {
218 return GetCpuInfo_<tstring>(cpuIndex, _T("Identifier"));
219 }
220
221} // namespace
222
223namespace owl
224{
225
226struct TSystem::TProcessorInfo::TImpl
227{
228 tstring Name;
229 tstring VendorId;
230 tstring Identifier;
231 int Frequency;
232
233 TImpl(int cpuIndex = 0)
236 Identifier(GetCpuIdentifier_(cpuIndex)),
237 Frequency(GetCpuFrequency_(cpuIndex))
238 {}
239};
240
241//
242/// Queries CPU info and initializes members.
243//
247
249{
250 delete Pimpl;
251}
252
253//
255{
256 return Pimpl->Name;
257}
258
260{
261 return Pimpl->VendorId;
262}
263
265{
266 return Pimpl->Identifier;
267}
268
269//
270/// Returns the name of the CPU vendor.
271/// If the vendor is unknown to this function, an empty string is returned.
272//
274{
275 typedef std::map<tstring, tstring> TMap;
276 typedef TMap::value_type T;
277 const T i[] =
278 {
279 T(_T("AuthenticAMD"), _T("AMD")),
280 T(_T("GenuineIntel"), _T("Intel")),
281 T(_T("CyrixInstead"), _T("Cyrix")),
282 T(_T("CentaurHauls"), _T("Centaur")),
283 T(_T("RiseRiseRise"), _T("Rise")),
284 T(_T("GenuineTMx86"), _T("Transmeta")),
285 T(_T("UMC UMC UMC "), _T("UMC"))
286 };
287 static const TMap m(i, i + COUNTOF(i));
288 TMap::const_iterator r = m.find(vendorId);
289 return r == m.end() ? tstring() : r->second;
290}
291
292//
293/// Returns the frequency of CPU 0 as reported by the Windows Registry ("CentralProcessor/0/~MHz").
294/// The returned frequency is specified in MHz.
295//
297{
298 return Pimpl->Frequency;
299}
300
305
310
315
320
325
326} // OWL namespace
#define CHECK(condition)
Definition checks.h:239
#define WARN(condition, message)
Definition checks.h:273
TModuleVersionInfo provides access to a TModule's VERSIONINFO resource.
Definition module.h:182
static auto GetLocalMachine() -> TRegKey &
Special predefined root key defining the physical state of the computer (HKEY_LOCAL_MACHINE).
Definition registry.cpp:87
Encapsulates information about a processor core.
Definition system.h:57
int GetNominalFrequency() const
Returns the frequency of CPU 0 as reported by the Windows Registry ("CentralProcessor/0/~MHz").
Definition system.cpp:296
TProcessorInfo()
Queries CPU info and initializes members.
Definition system.cpp:244
static tstring GetVendorName(const tstring &vendorId)
Returns the name of the CPU vendor.
Definition system.cpp:273
tstring GetVendorId() const
Definition system.cpp:259
auto GetIdentifier() const -> tstring
Definition system.cpp:264
tstring GetName() const
Definition system.cpp:254
static uint GetBuildNumber()
Definition system.cpp:91
static bool IsWin64Bit()
Returns true if the OS is 64-bit.
Definition system.cpp:104
static int GetNumberOfProcessors()
Returns the value of the SYSTEM_INFO::dwNumberOfProcessors field returned by ::GetSystemInfo.
Definition system.cpp:131
static auto GetVersionStr() -> tstring
Definition system.cpp:96
static uint GetMinorVersion()
Definition system.cpp:86
static uint GetProcessorArchitecture()
Returns the value of the SYSTEM_INFO::wProcessorArchitecture field returned by ::GetSystemInfo.
Definition system.cpp:141
static uint GetMajorVersion()
Definition system.cpp:81
static tstring GetProcessorArchitectureName(uint architecture)
Returns the description string for the given processor architecture.
Definition system.cpp:151
static auto GetVersionInfo() -> TModuleVersionInfo &
Returns the product version for "kernel32.dll".
Definition system.cpp:75
static const TProcessorInfo & GetProcessorInfo()
Returns info for the current processor.
Definition system.cpp:177
static uint GetProductInfo()
Retrieves the product edition for the operating system on the local computer, and maps the type to th...
Definition system.cpp:60
#define _T(x)
Definition cygwin.h:51
Definition of class TModule.
Object Windows Library (OWLNext Core)
Definition animctrl.h:22
void InUse(const T &arg)
Handy utility to avoid compiler warnings about unused parameters.
Definition defs.h:299
std::string tstring
Definition defs.h:79
auto to_tstring(const T &v) -> tstring
Definition defs.h:82
#define COUNTOF(s)
Array element count Important: Only use this with an argument of array type.
Definition defs.h:376
General Registry access & registration implementation TRegKey, TRegValue, TRegKeyIterator,...
Definition of TSystem, a system information provider class.