-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSystemAudit.cpp
More file actions
487 lines (417 loc) · 21.2 KB
/
Copy pathSystemAudit.cpp
File metadata and controls
487 lines (417 loc) · 21.2 KB
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
#include "SystemAudit.h"
#include <windows.h>
#include <powrprof.h>
#include <sysinfoapi.h>
#include <wbemidl.h>
#include <comdef.h>
#include <iostream>
#include <vector>
#include <string>
#include <regex>
#include "Utility.h"
#include <dxgi.h>
#pragma comment(lib, "wbemuuid.lib")
#pragma comment(lib, "PowrProf.lib")
#pragma comment(lib, "Advapi32.lib")
#pragma comment(lib, "User32.lib")
#pragma comment(lib, "dxgi.lib")
// --- WINAPI HELPER FUNCTIONS ---
bool CheckIfLaptop() {
SYSTEM_POWER_CAPABILITIES spc;
if (GetPwrCapabilities(&spc)) return spc.LidPresent != 0;
return false;
}
std::string GetWindowsEdition() {
std::string osName = "Windows 10"; // Default assumption
// Check the build number in the registry to distinguish Windows 10 from Windows 11
HKEY hKey;
if (RegOpenKeyExA(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", 0, KEY_READ, &hKey) == ERROR_SUCCESS) {
char build[32];
DWORD size = sizeof(build);
if (RegQueryValueExA(hKey, "CurrentBuild", NULL, NULL, (LPBYTE)build, &size) == ERROR_SUCCESS) {
try {
int buildNumber = std::stoi(build);
if (buildNumber >= 22000) {
osName = "Windows 11";
}
}
catch (...) {}
}
RegCloseKey(hKey);
}
DWORD productType = 0;
if (GetProductInfo(10, 0, 0, 0, &productType)) {
if (productType == PRODUCT_PROFESSIONAL) return osName + " Pro";
if (productType == PRODUCT_CORE) return osName + " Home";
if (productType == PRODUCT_EDUCATION) return osName + " Education";
if (productType == PRODUCT_ENTERPRISE) return osName + " Enterprise";
return osName + " (Other edition, code: " + std::to_string(productType) + ")";
}
return "Unknown";
}
bool CheckAutopilotLock() {
if (GetFileAttributesA("C:\\Windows\\Provisioning\\Autopilot\\AutopilotConfigurationFile.json") != INVALID_FILE_ATTRIBUTES) return true;
HKEY hKey;
LONG result = RegOpenKeyExA(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Provisioning\\Diagnostics\\Autopilot", 0, KEY_READ, &hKey);
if (result == ERROR_SUCCESS) {
DWORD type, size = 0;
result = RegQueryValueExA(hKey, "TenantId", NULL, &type, NULL, &size);
RegCloseKey(hKey);
if (result == ERROR_SUCCESS) return true;
}
return false;
}
bool IsTouchscreen() {
int value = GetSystemMetrics(SM_DIGITIZER);
// NID_READY (0x80) and NID_INTEGRATED_TOUCH (0x01)
return (value & 0x80) && (value & 0x01);
}
// --- WMI ENGINE ---
// Helper functions for WMI queries, implemented based on Microsoft COM API documentation.
std::vector<std::string> QueryWMIVector(IWbemServices* pSvc, const wchar_t* query, const wchar_t* propertyName) {
std::vector<std::string> results;
IEnumWbemClassObject* pEnumerator = NULL;
HRESULT hres = pSvc->ExecQuery(bstr_t("WQL"), bstr_t(query), WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &pEnumerator);
if (FAILED(hres)) return results;
IWbemClassObject* pclsObj = NULL;
ULONG uReturn = 0;
while (pEnumerator) {
HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn);
if (0 == uReturn) break;
VARIANT vtProp;
hr = pclsObj->Get(propertyName, 0, &vtProp, 0, 0);
if (SUCCEEDED(hr)) {
if (vtProp.vt == VT_BSTR && vtProp.bstrVal != NULL) {
results.push_back((const char*)_bstr_t(vtProp.bstrVal));
}
else if (vtProp.vt == VT_I4 || vtProp.vt == VT_UI4 || vtProp.vt == VT_I2 || vtProp.vt == VT_UI2) {
results.push_back(std::to_string(vtProp.uintVal));
}
}
VariantClear(&vtProp);
pclsObj->Release();
}
pEnumerator->Release();
return results;
}
std::string QueryWMI(IWbemServices* pSvc, const wchar_t* query, const wchar_t* propertyName) {
auto vec = QueryWMIVector(pSvc, query, propertyName);
return vec.empty() ? "" : vec[0];
}
// Connects to a specific WMI namespace (e.g., Storage)
IWbemServices* ConnectToWMI(IWbemLocator* pLoc, const wchar_t* wmiNamespace) {
IWbemServices* pSvc = NULL;
HRESULT hres = pLoc->ConnectServer(_bstr_t(wmiNamespace), NULL, NULL, 0, NULL, 0, 0, &pSvc);
if (SUCCEEDED(hres)) {
CoSetProxyBlanket(pSvc, RPC_C_AUTHN_WINNT, RPC_C_AUTHZ_NONE, NULL, RPC_C_AUTHN_LEVEL_CALL, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE);
}
return pSvc;
}
// --- DATA EXTRACTION FUNCTIONS ---
void GetNetworkInfo(IWbemServices* pSvc, AuditResults& res) {
res.hasWifi = false;
res.hasBluetooth = false;
res.lanSpeed = 0.0;
IEnumWbemClassObject* pEnum = NULL;
if (SUCCEEDED(pSvc->ExecQuery(bstr_t("WQL"), bstr_t(L"SELECT Name, PNPDeviceID, Speed FROM Win32_NetworkAdapter WHERE PhysicalAdapter=True"), WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &pEnum))) {
IWbemClassObject* pObj = NULL;
ULONG uRet = 0;
while (pEnum->Next(WBEM_INFINITE, 1, &pObj, &uRet) == S_OK && uRet == 1) {
VARIANT vtName, vtPnp, vtSpeed;
pObj->Get(L"Name", 0, &vtName, 0, 0);
pObj->Get(L"PNPDeviceID", 0, &vtPnp, 0, 0);
pObj->Get(L"Speed", 0, &vtSpeed, 0, 0);
if (vtName.vt == VT_BSTR) {
std::string n = (const char*)_bstr_t(vtName.bstrVal);
std::string nameLower = n;
std::transform(nameLower.begin(), nameLower.end(), nameLower.begin(), ::tolower);
std::string pnpLower;
if (vtPnp.vt == VT_BSTR && vtPnp.bstrVal != NULL) {
pnpLower = (const char*)_bstr_t(vtPnp.bstrVal);
std::transform(pnpLower.begin(), pnpLower.end(), pnpLower.begin(), ::tolower);
}
bool isUsb = (pnpLower.find("usb") != std::string::npos);
bool isWifi = (nameLower.find("wi-fi") != std::string::npos || nameLower.find("wireless") != std::string::npos || nameLower.find("wlan") != std::string::npos || nameLower.find("802.11") != std::string::npos);
bool isBluetooth = (nameLower.find("bluetooth") != std::string::npos);
if (isWifi) res.hasWifi = true;
if (isBluetooth) res.hasBluetooth = true;
if (!isUsb && !isWifi && !isBluetooth) {
double guessedSpeed = 0.0;
if (nameLower.find("10g") != std::string::npos || nameLower.find("10 g") != std::string::npos || nameLower.find("10000") != std::string::npos) {
guessedSpeed = 10.0;
}
else if (nameLower.find("2.5") != std::string::npos || nameLower.find("i225") != std::string::npos || nameLower.find("i226") != std::string::npos) {
guessedSpeed = 2.5;
}
else if (nameLower.find("gigabit") != std::string::npos || nameLower.find("gbe") != std::string::npos ||
nameLower.find("10/100/1000") != std::string::npos || nameLower.find("1000m") != std::string::npos ||
nameLower.find("i219") != std::string::npos || nameLower.find("i218") != std::string::npos ||
nameLower.find("i217") != std::string::npos || nameLower.find("i211") != std::string::npos || nameLower.find("i210") != std::string::npos) {
guessedSpeed = 1.0;
}
else if (nameLower.find("fast ethernet") != std::string::npos || nameLower.find("10/100") != std::string::npos || nameLower.find(" fe ") != std::string::npos) {
guessedSpeed = 0.1;
}
double actualSpeed = 0.0;
if (vtSpeed.vt == VT_BSTR && vtSpeed.bstrVal != NULL) {
try {
unsigned long long speedBps = std::stoull((const char*)_bstr_t(vtSpeed.bstrVal));
actualSpeed = (double)speedBps / 1000000000.0;
}
catch (...) {}
}
double finalSpeed = (actualSpeed > guessedSpeed) ? actualSpeed : guessedSpeed;
if (finalSpeed > res.lanSpeed) {
res.lanSpeed = finalSpeed;
}
}
}
VariantClear(&vtName);
VariantClear(&vtPnp);
VariantClear(&vtSpeed);
pObj->Release();
}
pEnum->Release();
}
}
void GetRamInfo(IWbemServices* pSvc, AuditResults& res) {
// RAM Slots
std::string maxSlots = QueryWMI(pSvc, L"SELECT MemoryDevices FROM Win32_PhysicalMemoryArray", L"MemoryDevices");
res.ramSlotsTotal = maxSlots.empty() ? 0 : std::stoi(maxSlots);
// Used slots and capacity calculation (GB)
auto capacities = QueryWMIVector(pSvc, L"SELECT Capacity FROM Win32_PhysicalMemory", L"Capacity");
res.ramSlotsUsed = capacities.size();
unsigned long long totalBytes = 0;
for (const auto& cap : capacities) {
try { totalBytes += std::stoull(cap); }
catch (...) {}
}
res.ramTotalGB = (int)(totalBytes / (1024ULL * 1024 * 1024));
// RAM Speed
std::string speed = QueryWMI(pSvc, L"SELECT Speed FROM Win32_PhysicalMemory", L"Speed");
res.ramSpeedMHz = speed.empty() ? 0 : std::stoi(speed);
// Detect soldered RAM based on FormFactor (14=SMD, 21=BGA, 22=FPBGA, 24=Die)
res.hasSolderedRam = false;
auto formFactors = QueryWMIVector(pSvc, L"SELECT FormFactor FROM Win32_PhysicalMemory", L"FormFactor");
for (const auto& ff : formFactors) {
try {
int fCode = std::stoi(ff);
if (fCode == 14 || fCode == 21 || fCode == 22 || fCode == 24) {
res.hasSolderedRam = true;
}
}
catch (...) {}
}
// DDR Type (decoding SMBIOSMemoryType) + LPDDR
std::string type = QueryWMI(pSvc, L"SELECT SMBIOSMemoryType FROM Win32_PhysicalMemory", L"SMBIOSMemoryType");
int typeCode = type.empty() ? 0 : std::stoi(type);
if (typeCode == 26) res.ramType = "DDR4";
else if (typeCode == 27) { res.ramType = "LPDDR4"; res.hasSolderedRam = true; } // LPDDR is always soldered
else if (typeCode == 28) { res.ramType = "LPDDR3"; res.hasSolderedRam = true; }
else if (typeCode == 30) { res.ramType = "LPDDR2"; res.hasSolderedRam = true; }
else if (typeCode == 34) res.ramType = "DDR5";
else if (typeCode == 35) { res.ramType = "LPDDR5"; res.hasSolderedRam = true; }
else if (typeCode == 24) res.ramType = "DDR3";
else if (typeCode == 21) res.ramType = "DDR2";
else res.ramType = "Unknown (" + std::to_string(typeCode) + ")";
}
void GetGpuInfo(IWbemServices* pSvc, AuditResults& res) {
IDXGIFactory* pFactory = nullptr;
if (SUCCEEDED(CreateDXGIFactory(__uuidof(IDXGIFactory), (void**)&pFactory))) {
IDXGIAdapter* pAdapter = nullptr;
for (UINT i = 0; pFactory->EnumAdapters(i, &pAdapter) != DXGI_ERROR_NOT_FOUND; ++i) {
DXGI_ADAPTER_DESC desc;
if (SUCCEEDED(pAdapter->GetDesc(&desc))) {
// Convert from WCHAR to UTF-8 / ASCII
char buffer[256];
WideCharToMultiByte(CP_UTF8, 0, desc.Description, -1, buffer, sizeof(buffer), NULL, NULL);
std::string gpuName = buffer;
if (gpuName.find("Microsoft Basic Render Driver") == std::string::npos) {
GraphicsProcessorData gpuDetails;
gpuDetails.GraphicsProcessorName = gpuName;
// Size in GB with rounding
unsigned long long memBytes = desc.DedicatedVideoMemory;
if (memBytes > 0) {
double memGB = (double)memBytes / (1024.0 * 1024.0 * 1024.0);
gpuDetails.GraphicsProcessorMemoryGB = (int)(memGB + 0.5); // Rounding e.g. 7.9 GB to 8 GB, 15.9 to 16 GB; integrated graphics might report a value close to zero (then 0).
}
else {
gpuDetails.GraphicsProcessorMemoryGB = 0;
}
res.gpusInfo.push_back(gpuDetails);
}
}
pAdapter->Release();
}
pFactory->Release();
}
// Fallback if DXGI fails
if (res.gpusInfo.empty()) {
auto names = QueryWMIVector(pSvc, L"SELECT Name FROM Win32_VideoController", L"Name");
auto vrams = QueryWMIVector(pSvc, L"SELECT AdapterRAM FROM Win32_VideoController", L"AdapterRAM");
for (size_t i = 0; i < names.size(); ++i) {
GraphicsProcessorData gpuDetails;
gpuDetails.GraphicsProcessorName = names[i];
if (i < vrams.size() && !vrams[i].empty()) {
try {
unsigned long long bytes = std::stoull(vrams[i]);
gpuDetails.GraphicsProcessorMemoryGB = (int)((bytes + 512ULL * 1024 * 1024) / (1024ULL * 1024 * 1024));
}
catch (...) {}
}
res.gpusInfo.push_back(gpuDetails);
}
}
}
void GetDiskInfo(IWbemLocator* pLoc, AuditResults& res) {
// To reliably detect NVMe and SSD, use the Storage namespace (available since Win10)
IWbemServices* pStorageSvc = ConnectToWMI(pLoc, L"ROOT\\Microsoft\\Windows\\Storage");
if (pStorageSvc) {
IEnumWbemClassObject* pEnum = NULL;
if (SUCCEEDED(pStorageSvc->ExecQuery(bstr_t("WQL"), bstr_t(L"SELECT FriendlyName, MediaType, BusType, Size FROM MSFT_PhysicalDisk"), WBEM_FLAG_FORWARD_ONLY, NULL, &pEnum))) {
IWbemClassObject* pObj = NULL;
ULONG uRet = 0;
while (pEnum->Next(WBEM_INFINITE, 1, &pObj, &uRet) == S_OK && uRet == 1) {
DiskDetails diskDetails;
VARIANT vtName, vtMedia, vtBus, vtSize;
pObj->Get(L"FriendlyName", 0, &vtName, 0, 0);
pObj->Get(L"MediaType", 0, &vtMedia, 0, 0);
pObj->Get(L"BusType", 0, &vtBus, 0, 0);
pObj->Get(L"Size", 0, &vtSize, 0, 0);
std::string name = (vtName.vt == VT_BSTR) ? (const char*)_bstr_t(vtName.bstrVal) : "Unknown";
unsigned long long sizeBytes = (vtSize.vt == VT_BSTR) ? std::stoull((const char*)_bstr_t(vtSize.bstrVal)) : 0;
int sizeGB = (int)(sizeBytes / (1000ULL * 1000 * 1000)); // Calculate disk size in decimal (1GB = 1000MB)
// MediaType: 3 = HDD, 4 = SSD
std::string typeStr = "SSD/HDD";
if (vtMedia.vt == VT_UI2 || vtMedia.vt == VT_I2 || vtMedia.vt == VT_I4 || vtMedia.vt == VT_UI4) {
if (vtMedia.uintVal == 3) typeStr = "HDD";
else if (vtMedia.uintVal == 4) typeStr = "SSD";
}
// BusType: 11 = SATA, 17 = NVMe
std::string busStr = "SATA/PCIe";
if (vtBus.vt == VT_UI2 || vtBus.vt == VT_I2 || vtBus.vt == VT_I4 || vtBus.vt == VT_UI4) {
if (vtBus.uintVal == 11) busStr = "SATA";
else if (vtBus.uintVal == 17) busStr = "PCIe NVMe";
}
diskDetails.diskName = name;
diskDetails.diskType = typeStr;
diskDetails.diskInterface = busStr;
diskDetails.diskCapacityGB = sizeGB;
res.disksInfo.push_back(diskDetails);
VariantClear(&vtName); VariantClear(&vtMedia); VariantClear(&vtBus); VariantClear(&vtSize);
pObj->Release();
}
pEnum->Release();
}
pStorageSvc->Release();
}
// Fallback if MSFT_PhysicalDisk query fails
if (res.disksInfo.empty()) {
DiskDetails errorDisk;
errorDisk.diskName = "Manual check required (No Storage namespace privileges)";
errorDisk.diskType = "N/A";
errorDisk.diskInterface = "N/A";
errorDisk.diskCapacityGB = 0;
res.disksInfo.push_back(errorDisk);
}
}
// --- MAIN AUDIT FUNCTION ---
AuditResults RunSystemAudit() {
AuditResults res = {};
res.isLaptop = CheckIfLaptop();
res.windowsEdition = GetWindowsEdition();
res.isAutopilotLocked = CheckAutopilotLock();
res.isTouchscreen = IsTouchscreen();
res.wmiSuccess = false;
HRESULT hres = CoInitializeEx(0, COINIT_MULTITHREADED);
if (FAILED(hres)) { res.errorMessage = "COM Init Failed."; return res; }
hres = CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_DEFAULT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, NULL);
if (FAILED(hres) && hres != RPC_E_TOO_LATE) { CoUninitialize(); res.errorMessage = "COM Sec Failed."; return res; }
IWbemLocator* pLoc = NULL;
hres = CoCreateInstance(CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER, IID_IWbemLocator, (LPVOID*)&pLoc);
if (FAILED(hres)) { CoUninitialize(); res.errorMessage = "WbemLocator Failed."; return res; }
IWbemServices* pSvc = ConnectToWMI(pLoc, L"ROOT\\CIMV2");
if (!pSvc) { pLoc->Release(); CoUninitialize(); res.errorMessage = "WMI Connect Failed."; return res; }
res.wmiSuccess = true;
// Motherboard, BIOS, Baseboard
res.uuid = QueryWMI(pSvc, L"SELECT UUID FROM Win32_ComputerSystemProduct", L"UUID");
res.serialNumber = QueryWMI(pSvc, L"SELECT SerialNumber FROM Win32_BIOS", L"SerialNumber");
res.biosVersion = QueryWMI(pSvc, L"SELECT SMBIOSBIOSVersion FROM Win32_BIOS", L"SMBIOSBIOSVersion");
res.manufacturer = QueryWMI(pSvc, L"SELECT Manufacturer FROM Win32_ComputerSystem", L"Manufacturer");
res.model = QueryWMI(pSvc, L"SELECT Model FROM Win32_ComputerSystem", L"Model");
// Processor (CPU)
res.cpuName = Trim(QueryWMI(pSvc, L"SELECT Name FROM Win32_Processor", L"Name"));
std::string cores = QueryWMI(pSvc, L"SELECT NumberOfCores FROM Win32_Processor", L"NumberOfCores");
std::string threads = QueryWMI(pSvc, L"SELECT ThreadCount FROM Win32_Processor", L"ThreadCount");
std::string clock = QueryWMI(pSvc, L"SELECT MaxClockSpeed FROM Win32_Processor", L"MaxClockSpeed");
std::string l3 = QueryWMI(pSvc, L"SELECT L3CacheSize FROM Win32_Processor", L"L3CacheSize");
res.cpuCores = cores.empty() ? 0 : std::stoi(cores);
res.cpuThreads = threads.empty() ? 0 : std::stoi(threads);
res.cpuBaseClockMHz = clock.empty() ? 0 : std::stoi(clock);
res.cpuL3CacheMB = l3.empty() ? 0 : (std::stoi(l3) / 1024); // Sometimes in KB, sometimes in MB - depending on the BIOS. Usually KB.
// RAM, Disks, GPU
GetRamInfo(pSvc, res);
GetGpuInfo(pSvc, res);
GetDiskInfo(pLoc, res); // Uses pLoc to connect to ROOT\Microsoft\Windows\Storage
// Display
std::string xRes = QueryWMI(pSvc, L"SELECT CurrentHorizontalResolution FROM Win32_VideoController", L"CurrentHorizontalResolution");
std::string yRes = QueryWMI(pSvc, L"SELECT CurrentVerticalResolution FROM Win32_VideoController", L"CurrentVerticalResolution");
std::string hz = QueryWMI(pSvc, L"SELECT CurrentRefreshRate FROM Win32_VideoController", L"CurrentRefreshRate");
if (!xRes.empty() && !yRes.empty()) res.resolution = xRes + " x " + yRes;
else res.resolution = "No data";
res.refreshRateHz = hz.empty() ? 0 : std::stoi(hz);
// Battery
if (res.isLaptop) {
SYSTEM_POWER_STATUS sps;
res.hasBattery = false;
if (GetSystemPowerStatus(&sps)) {
if ((sps.BatteryFlag & 128) == 0 && sps.BatteryFlag != 255) {
res.hasBattery = true;
}
}
if (!res.hasBattery) {
std::string batId = QueryWMI(pSvc, L"SELECT DeviceID FROM Win32_Battery", L"DeviceID");
if (!batId.empty()) {
res.hasBattery = true;
}
}
std::string design = QueryWMI(pSvc, L"SELECT DesignCapacity FROM Win32_Battery", L"DesignCapacity");
std::string full = QueryWMI(pSvc, L"SELECT FullChargeCapacity FROM Win32_Battery", L"FullChargeCapacity");
// Fallback battery data retrieval from ROOT\WMI (similar to powercfg /batteryreport)
if (design.empty() || full.empty()) {
IWbemServices* pWmiSvc = ConnectToWMI(pLoc, L"ROOT\\WMI");
if (pWmiSvc) {
if (design.empty()) design = QueryWMI(pWmiSvc, L"SELECT DesignedCapacity FROM BatteryStaticData", L"DesignedCapacity");
if (full.empty()) full = QueryWMI(pWmiSvc, L"SELECT FullChargedCapacity FROM BatteryFullChargedCapacity", L"FullChargedCapacity");
pWmiSvc->Release();
}
}
if (!design.empty() && !full.empty()) {
try {
int dCap = std::stoi(design);
int fCap = std::stoi(full);
if (dCap > 0) res.batteryHealthPercent = (fCap * 100) / dCap;
res.batteryCapacityWh = dCap / 1000; // mWh to Wh
}
catch (...) {
res.batteryHealthPercent = -1;
res.batteryCapacityWh = 0;
}
}
else {
res.batteryHealthPercent = -1;
res.batteryCapacityWh = 0;
}
}
// Communication / Multimedia
GetNetworkInfo(pSvc, res);
std::string cameras = QueryWMI(pSvc, L"SELECT Name FROM Win32_PnPEntity WHERE PNPClass='Camera' OR PNPClass='Image'", L"Name");
res.hasCamera = !cameras.empty();
std::string cdrom = QueryWMI(pSvc, L"SELECT Drive FROM Win32_CDROMDrive", L"Drive");
res.hasOpticalDrive = !cdrom.empty();
// Cleanup
pSvc->Release();
pLoc->Release();
CoUninitialize();
return res;
}