-
Notifications
You must be signed in to change notification settings - Fork 0
/
SystemProperties.hpp
303 lines (274 loc) · 11.4 KB
/
SystemProperties.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/*MIT License
Copyright (c) 2021 CasualYouTuber31 <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.*/
#pragma once
#include <string>
#include <system_error>
#include <filesystem>
#ifdef _WIN32
#include <variant>
#include <vector>
#ifdef _WIN32_DCOM
#define _SYSTEM_PROPERTIES_DO_NOT_UNDEF
#endif
#ifndef _WIN32_DCOM
#define _WIN32_DCOM
#endif
#include <comdef.h>
#include <WbemIdl.h>
#pragma comment(lib, "wbemuuid.lib")
#elif __linux__
#include <sys/utsname.h>
#elif __APPLE__
// macOS-only includes go here
#endif
/**\file SystemProperties.hpp
* \brief This file declares the class which allows the client to query the
* computer for hardware and software information.
*/
/**
* \brief The \c System namespace contains the \c Properties class.
*/
namespace System {
/**
* \brief The units of memory available for the client to use.
* \remarks If more units are added, do not forget to update
* \c System::convert() and \c System::notation() accordingly.
*/
enum class Unit {
Bytes,
KB,
MB,
GB
};
/**
* \brief This function converts a given number of bytes into a given unit of
* memory.
* \param bytes The bytes to convert, in \c System::Unit::Bytes.
* \param unit The unit of memory to convert the given number of bytes to.
* \return The number of bytes, converted.
*/
std::uint64_t convert(const std::uint64_t bytes, const System::Unit unit)
noexcept;
/**
* \brief This function retrieves the notation for the given unit of memory.
* \param unit The unit of memory to retrieve the notation of.
* \return The notation of the given memory unit.
*/
std::string notation(const System::Unit unit) noexcept;
/**
* \brief This class lets the client query the computer for hardware and
* software information.
* \warning Due to limitations within the Windows implementation, this class
* should only be instantiated \b once, and kept alive for as long as
* necessary, before it is disposed of. It should \b not be
* instantiated more than once in a program, regardless of platform!
* \warning In addition, within the Windows implementation, this library makes
* use of the COM library. Within the constructor, the following
* functions are called: \c CoInitializeEx(),
* \c CoInitializeSecurity(), \c CoCreateInstance(),
* \c IWbemLocator::ConnectServer(), and \c CoSetProxyBlanket().
* Microsoft in their Windows API documentation instructs the
* programmer to avoid calling most of these functions more than once
* in a program, so if you call these functions at all within your own
* code, you will likely not be able to use this library verbatim. You
* can extract the relevant code for yourself, or you may amend the
* constructor to fit around your application, if this is the case.
*/
class Properties {
public:
/**
* \brief Initialises the connection between the program and the
* computer.
* \warning If this constructor throws, \b do \b not use the resulting
* object, as it will be unsafe to do so (accessing NULL pointers
* internally, etc.).
* \throws std::system_error if initialisation failed. An OS-specific
* code and error string will be generated.
*/
Properties();
/**
* \brief Safely destroys the connection between the program and the
* computer.
*/
~Properties() noexcept;
/**
* \brief Retrieves the CPU model name.
* \return User-friendly name of the CPU.
* \throws std::system_error if the the request failed. An OS-specific
* code and error string will be generated.
*/
std::string CPUModel();
/**
* \brief Retrieves the CPU architecture.
* \return Architecture of the CPU.
* \throws std::system_error if the the request failed. An OS-specific
* code and error string will be generated.
*/
std::string CPUArchitecture();
/**
* \brief Retrieves the total installed RAM available.
* \details On the Windows implementation, the total RAM installed will be
* returned.\n
* On the Linux implementation, only the RAM available for use at
* the time of calling will be returned. This could mean that 16GB
* of RAM is installed, for example, but only 8GB of it is readily
* available.
* \param unit The unit of memory to return the total RAM in. By default,
* it is \c System::Unit::GB.
* \return The total RAM installed.
* \throws std::system_error if the the request failed. An OS-specific
* code and error string will be generated.
*/
std::string RAMTotal(const System::Unit unit = System::Unit::GB);
/**
* \brief Retrieves the name of the OS the machine is running.
* \return User-friendly OS name.
* \throws std::system_error if the the request failed. An OS-specific
* code and error string will be generated.
*/
std::string OSName();
/**
* \brief Retrieves the version of the OS the machine is running.
* \return Version string.
* \throws std::system_error if the the request failed. An OS-specific
* code and error string will be generated.
*/
std::string OSVersion();
/**
* \brief Retrieves the vendor of the currently installed GPU.
* \details Note that on Linux, the \c lshw program is used to retrieve
* this information. If the program is not installed, this
* method will throw.
* \return The name of the vendor of the installed GPU.
* \throws std::system_error if the the request failed. An OS-specific
* code and error string will be generated.
*/
std::string GPUVendor();
/**
* \brief Retrieves the name of the currently installed GPU.
* \details Note that on Linux, the \c lshw program is used to retrieve
* this information. If the program is not installed, this
* method will throw.
* \return User-friendly name of the installed GPU.
* \throws std::system_error if the the request failed. An OS-specific
* code and error string will be generated.
*/
std::string GPUName();
/**
* \brief Retrieves the version of the driver the installed GPU is using.
* \details Note that on Linux, the \c lshw and \c modinfo programs are
* used to retrieve this information. If the programs are not
* installed, this method will throw.
* \return Version string.
* \throws std::system_error if the the request failed. An OS-specific
* code and error string will be generated.
*/
std::string GPUDriver();
/**
* \brief Retrieves the capacity of the drive the program is running on.
* \param unit The unit of memory to return the capacity in. By default,
* it is \c System::Unit::GB.
* \return The capacity of the drive.
* \throws std::filesystem_error if the the request failed. An OS-specific
* code and error string will be generated.
*/
std::string StorageTotal(const System::Unit unit = System::Unit::GB);
/**
* \brief Retrieves the amount of free space on the drive the program is
* running on.
* \param unit The unit of memory to return the free space in. By default,
* it is \c System::Unit::GB.
* \return The free space of the drive.
* \throws std::filesystem_error if the the request failed. An OS-specific
* code and error string will be generated.
*/
std::string StorageFree(const System::Unit unit = System::Unit::GB);
private:
#ifdef _WIN32
/**
* \brief Make a request to the WMI and retrieve the output.
* \param className Name of the WMI class containing the information to
* retrieve.
* \param objectName Name of the WMI object within the WMI class
* containing the information to retrieve.
* \param datatype The CIM data type of the information to retrieve.
* \return The output received from the WMI.
* \throws std::system_error if the the request failed. A
* Windows-specific code and error string will be generated.
*/
std::variant<std::vector<std::int64_t>, std::vector<std::uint64_t>,
std::vector<std::string>>
_wmiRequest(const char* className, const char* objectName,
const CIMTYPE datatype);
/**
* \brief Pointer to the WMI locator.
* \details This will be manually released in the destructor.
*/
IWbemLocator* _pLoc = 0;
/**
* \brief Pointer to the WMI services.
* \details This will be manually released in the destructor.
*/
IWbemServices* _pSvc = 0;
#elif __linux__
/**
* \brief Make a query for CPU information.
* \param objectName Name of the piece of CPU information to retrieve.
* \return The piece of CPU information requested.
* \throws std::system_error if the the request failed. A file I/O code and
* error string will be generated.
*/
std::string _cpuRequest(const std::string& objectName);
/**
* \brief Retrieve information about the OS.
* \return Structure containing Linux-specific OS information.
* \throws std::system_error if the the request failed. A Linux-specific
* code and error string will be generated.
*/
struct utsname _osRequest();
/**
* \brief Executes a command on the bash and retrieves the output.
* \details Many thanks to <a href="https://stackoverflow.com/a/478960"
* target=_blank>waqas</a> for the code here.
* \param cmd The command to execute.
* \return The output of the executed command.
* \throws std::system_error if the the request failed. A Linux-specific
* code and error string will be generated.
*/
std::string _exec(const char* cmd);
/**
* \brief Retrieves GPU information.
* \param name Name of the GPU information to search for.
* \return The information.
* \throws std::system_error if the the request failed. A Linux-specific
* code and error string will be generated.
*/
std::string _gpuRequest(const std::string& name);
#elif __APPLE__
// any macOS-only data required goes here
// also any macOS-only helper methods should be declared here
#endif
};
}
#ifdef _WIN32
// just in case leaving this in could cause problems in another's Windows code
// idk if this is even a useful thing to do, but it can't hurt... hopefully...
#ifndef _SYSTEM_PROPERTIES_DO_NOT_UNDEF
#undef _WIN32_DCOM
#endif
#endif