forked from FAForever/uid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
machine_info_win.cpp
384 lines (328 loc) · 10.1 KB
/
machine_info_win.cpp
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#include "machine_info.h"
#include <string>
#include <sstream>
#include <vector>
#include <unicode/unistr.h>
#define _WIN32_DCOM
#define _WIN32_WINNT 0x0600
#include <iostream>
#include <windows.h>
#include <wbemidl.h>
#include <comdef.h>
#include <winuser.h>
std::string exc_value("invalid");
IWbemServices *pSvc(NULL);
IWbemLocator *pLoc(NULL);
bool machine_info_init()
{
/* https://msdn.microsoft.com/en-us/library/aa390423(v=vs.85).aspx */
HRESULT hres;
// Step 1: --------------------------------------------------
// Initialize COM. ------------------------------------------
hres = CoInitializeEx(0, COINIT_MULTITHREADED);
if (FAILED(hres))
{
std::cerr << "Failed to initialize COM library. Error code = 0x"
<< std::hex
<< hres
<< std::endl;
return false;
}
// Step 2: --------------------------------------------------
// Set general COM security levels --------------------------
hres = CoInitializeSecurity(NULL,
-1, // COM authentication
NULL, // Authentication services
NULL, // Reserved
RPC_C_AUTHN_LEVEL_DEFAULT, // Default authentication
RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation
NULL, // Authentication info
EOAC_NONE, // Additional capabilities
NULL // Reserved
);
if (FAILED(hres))
{
std::cerr << "Failed to initialize security. Error code = 0x"
<< std::hex
<< hres
<< std::endl;
CoUninitialize();
return false; // Program has failed.
}
// Step 3: ---------------------------------------------------
// Obtain the initial locator to WMI -------------------------
IWbemLocator *pLoc = NULL;
hres = CoCreateInstance(CLSID_WbemLocator,
NULL,
CLSCTX_ALL,
IID_PPV_ARGS(&pLoc));
if (FAILED(hres))
{
std::cerr << "Failed to create IWbemLocator object."
<< " Err code = 0x"
<< std::hex
<< hres
<< std::endl;
pLoc = NULL;
return false;
}
// Step 4: -----------------------------------------------------
// Connect to WMI through the IWbemLocator::ConnectServer method
// Connect to the root\cimv2 namespace with
// the current user and obtain pointer pSvc
// to make IWbemServices calls.
//BSTR wmiNamespace(L"root\\cimv2");
hres = pLoc->ConnectServer(BSTR(L"root\\cimv2"), // Object path of WMI namespace
NULL, // User name. NULL = current user
NULL, // User password. NULL = current
NULL, // Locale. NULL indicates current
WBEM_FLAG_CONNECT_USE_MAX_WAIT,// Security flags.
NULL, // Authority (for example, Kerberos)
NULL, // Context object
&pSvc // pointer to IWbemServices proxy
);
if (FAILED(hres))
{
std::cerr << "Could not connect. Error code = 0x"
<< std::hex
<< hres
<< std::endl;
pSvc = NULL;
return false;
}
// Step 5: --------------------------------------------------
// Set security levels on the proxy -------------------------
hres = CoSetProxyBlanket(pSvc, // Indicates the proxy to set
RPC_C_AUTHN_WINNT, // RPC_C_AUTHN_xxx
RPC_C_AUTHZ_NONE, // RPC_C_AUTHZ_xxx
NULL, // Server principal name
RPC_C_AUTHN_LEVEL_CALL, // RPC_C_AUTHN_LEVEL_xxx
RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx
NULL, // client identity
EOAC_NONE // proxy capabilities
);
if (FAILED(hres))
{
std::cerr << "Could not set proxy blanket. Error code = 0x"
<< std::hex
<< hres
<< std::endl;
machine_info_free();
return false;
}
return true;
}
bool machine_info_free()
{
if (pSvc != NULL)
{
pSvc->Release();
pSvc = NULL;
}
if (pLoc != NULL)
{
pLoc->Release();
pLoc = NULL;
}
CoUninitialize();
return true;
}
std::vector<std::string> read_wmi_values(std::wstring const& table,
std::wstring const& property,
std::wstring const& filter = std::wstring())
{
std::vector<std::string> result;
if (pSvc == NULL)
{
std::cerr << "WMI uninitilized. Call machine_info_init() first" << std::endl;
return result;
}
std::wstring query(L"SELECT * FROM ");
query += table;
if (filter.size())
{
query += L" WHERE ";
query += filter;
}
IEnumWbemClassObject* pEnumerator = NULL;
HRESULT hres;
hres = pSvc->ExecQuery(
BSTR(L"WQL"),
(wchar_t*)query.c_str(),
WBEM_FLAG_FORWARD_ONLY,
NULL,
&pEnumerator);
if (FAILED(hres))
{
std::wcerr << "Query for "
<< table
<< "."
<< property.c_str()
<< " failed."
<< " Error code = 0x"
<< std::hex
<< hres
<< std::endl;
return result; // Program has failed.
}
// Step 7: -------------------------------------------------
// Get the data from the query in step 6 -------------------
IWbemClassObject *pclsObj = NULL;
int numElems;
while((hres = pEnumerator->Next(WBEM_INFINITE,
1,
&pclsObj,
(ULONG*)&numElems)) != WBEM_S_FALSE)
{
VARIANT vtProp;
VariantInit(&vtProp);
// Get the value of the Name property
hres = pclsObj->Get(property.c_str(), 0, &vtProp, 0, 0);
if (FAILED(hres))
{
std::wcerr << "Getting property "
<< table
<< "."
<< property.c_str()
<< " failed."
<< " Error code = 0x"
<< std::hex
<< hres
<< " query "
<< query.c_str()
<< std::endl;
break;
}
if (vtProp.vt == VT_BSTR)
{
std::string propString;
icu::UnicodeString(vtProp.bstrVal).toUTF8String(propString);
result.push_back(propString);
}
else if (vtProp.vt == VT_I4)
{
result.push_back(static_cast< std::ostringstream & >(( std::ostringstream() << std::dec << vtProp.iVal ) ).str());
}
VariantClear(&vtProp);
pclsObj->Release();
}
pEnumerator->Release();
return result;
}
std::string wmi_value(std::wstring const& table,
std::wstring const& property,
std::wstring const& filter = std::wstring())
{
if (pSvc == NULL)
{
return exc_value;
}
std::vector<std::string> values = read_wmi_values(table,
property,
filter);
if (values.size())
{
return values.front();
}
return exc_value;
}
std::string machine_info_uuid()
{
return wmi_value(L"Win32_ComputerSystemProduct",
L"UUID");
}
std::string machine_info_model()
{
return wmi_value(L"Win32_ComputerSystem",
L"Model");
}
std::string machine_info_manufacturer()
{
return wmi_value(L"Win32_ComputerSystem",
L"Manufacturer");
}
std::string machine_info_display_width()
{
SetProcessDPIAware();
return std::to_string(GetSystemMetrics(SM_CXSCREEN));
}
std::string machine_info_display_height()
{
SetProcessDPIAware();
return std::to_string(GetSystemMetrics(SM_CYSCREEN));
}
std::string machine_info_memory_serial0()
{
return wmi_value(L"Win32_PhysicalMemory",
L"SerialNumber");
}
std::string machine_info_motherboard_vendor()
{
return wmi_value(L"Win32_BaseBoard",
L"Manufacturer");
}
std::string machine_info_motherboard_name()
{
return wmi_value(L"Win32_BaseBoard",
L"Product");
}
std::string machine_info_disks_controllerid()
{
return wmi_value(L"Win32_IDEController",
L"DeviceID");
}
std::string machine_info_disks_vserial()
{
return wmi_value(L"Win32_LogicalDisk",
L"VolumeSerialNumber",
L"DeviceID=\"C:\"");
}
std::string machine_info_bios_manufacturer()
{
return wmi_value(L"Win32_BIOS",
L"Manufacturer");
}
std::string machine_info_bios_smbbversion()
{
return wmi_value(L"Win32_BIOS",
L"SMBIOSBIOSVersion");
}
std::string machine_info_bios_serial()
{
return wmi_value(L"Win32_BIOS",
L"SerialNumber");
}
std::string machine_info_bios_description()
{
return wmi_value(L"Win32_BIOS",
L"Description");
}
std::string machine_info_bios_date()
{
return wmi_value(L"Win32_BIOS",
L"ReleaseDate");
}
std::string machine_info_bios_version()
{
return wmi_value(L"Win32_BIOS",
L"Version");
}
std::string machine_info_processor_name()
{
return wmi_value(L"Win32_Processor",
L"Name");
}
std::string machine_info_processor_id()
{
return wmi_value(L"Win32_Processor",
L"ProcessorId");
}
std::string machine_info_os_type()
{
return "Windows";
}
std::string machine_info_os_version()
{
return wmi_value(L"Win32_OperatingSystem",
L"Version");
}