-
-
Notifications
You must be signed in to change notification settings - Fork 188
/
wmi.cpp
303 lines (246 loc) · 8.32 KB
/
wmi.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
/*---------------------------------------------------------*\
| wmi.cpp |
| |
| WMI interface functionality |
| |
| This file is part of the OpenRGB project |
| SPDX-License-Identifier: GPL-2.0-only |
\*---------------------------------------------------------*/
#include "wmi.h"
IWbemLocator* Wmi::pLoc = nullptr;
IWbemServices* Wmi::pSvc = nullptr;
HRESULT WmiInit = Wmi::init();
// Taken from https://stackoverflow.com/questions/215963/
// Convert a wide Unicode string to an UTF8 string
std::string utf8_encode(const std::wstring& wstr)
{
if (wstr.empty()) return std::string();
int size_needed = WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int) wstr.size(), nullptr, 0, nullptr, nullptr);
std::string strTo(size_needed, 0);
WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int) wstr.size(), &strTo[0], size_needed, nullptr, nullptr);
return strTo;
}
// Convert an UTF8 string to a wide Unicode String
std::wstring utf8_decode(const std::string& str)
{
if (str.empty())
{
return std::wstring();
}
int size_needed = MultiByteToWideChar(CP_UTF8, 0, &str[0], (int) str.size(), nullptr, 0);
std::wstring wstrTo(size_needed, 0);
MultiByteToWideChar(CP_UTF8, 0, &str[0], (int) str.size(), &wstrTo[0], size_needed);
return wstrTo;
}
bool isMatch(const std::string& value, const std::regex& re)
{
return std::regex_match(value, re);
}
Wmi::Wmi()
{
};
Wmi::~Wmi()
{
}
HRESULT Wmi::init()
{
HRESULT hres;
if(pLoc != nullptr && pSvc != nullptr)
{
return S_OK;
}
// Initialize COM. ------------------------------------------
hres = CoInitializeEx(0, COINIT_APARTMENTTHREADED);
if (FAILED(hres))
{
return hres;
}
// Set general COM security levels --------------------------
hres = CoInitializeSecurity(
nullptr,
-1, // COM authentication
nullptr, // Authentication services
nullptr, // Reserved
RPC_C_AUTHN_LEVEL_DEFAULT, // Default authentication
RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation
nullptr, // Authentication info
EOAC_NONE, // Additional capabilities
nullptr // Reserved
);
if (FAILED(hres))
{
CoUninitialize();
return hres;
}
// Obtain the initial locator to WMI -------------------------
hres = CoCreateInstance(
CLSID_WbemLocator,
nullptr,
CLSCTX_INPROC_SERVER,
IID_IWbemLocator, (LPVOID*) &pLoc
);
if (FAILED(hres))
{
CoUninitialize();
return hres;
}
hres = pLoc->ConnectServer(
_bstr_t(L"ROOT\\CIMV2"), // Object path of WMI namespace
nullptr, // User name. NULL = current user
nullptr, // User password. NULL = current
nullptr, // Locale. NULL indicates current
0, // Security flags.
nullptr, // Authority (for example, Kerberos)
nullptr, // Context object
&pSvc // pointer to IWbemServices proxy
);
if (FAILED(hres))
{
pLoc->Release();
CoUninitialize();
return hres;
}
// 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
nullptr, // Server principal name
RPC_C_AUTHN_LEVEL_CALL, // RPC_C_AUTHN_LEVEL_xxx
RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx
nullptr, // client identity
EOAC_NONE // proxy capabilities
);
if (FAILED(hres))
{
pSvc->Release();
pLoc->Release();
CoUninitialize();
return hres;
}
// Initialised WMI successfully
return S_OK;
}
HRESULT Wmi::query(std::string queryStr, std::vector<QueryObj>& queryVectorOut, const AdditionalFilters* filters)
{
if (pSvc == nullptr)
{
return E_FAIL;
}
HRESULT hres;
int nIdx = 0;
IEnumWbemClassObject* pEnumerator = nullptr;
// Initialize COM. ------------------------------------------
hres = CoInitializeEx(0, COINIT_APARTMENTTHREADED);
if (FAILED(hres))
{
return hres;
}
pSvc->Release();
// Reconnect to server before each query as we were seeing disconnected failures
hres = pLoc->ConnectServer(
_bstr_t(L"ROOT\\CIMV2"), // Object path of WMI namespace
nullptr, // User name. NULL = current user
nullptr, // User password. NULL = current
nullptr, // Locale. NULL indicates current
0, // Security flags.
nullptr, // Authority (for example, Kerberos)
nullptr, // Context object
&pSvc // pointer to IWbemServices proxy
);
// Make the WMI query
hres = pSvc->ExecQuery(
bstr_t("WQL"),
bstr_t(queryStr.c_str()),
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
nullptr,
&pEnumerator
);
if (FAILED(hres))
{
return hres;
}
IWbemClassObject* pclsObj = nullptr;
ULONG uReturn = 0;
while (pEnumerator)
{
hres = pEnumerator->Next(WMI_WAIT_TIME, 1, &pclsObj, &uReturn);
if (0==uReturn)
{
break;
}
VARIANT vtProp;
if (filters)
{
for (auto filter: *filters)
{
hres = pclsObj->Get(utf8_decode(filter.first).c_str(), 0, &vtProp, nullptr, nullptr);
if (FAILED(hres))
{
continue;
}
auto val = utf8_encode(vtProp.bstrVal);
if (!std::regex_match(val, filter.second))
{
goto _NextElement;
}
}
}
SAFEARRAY* sfArray;
LONG lstart, lend;
//Get Wmi objects names
hres = pclsObj->GetNames(0, WBEM_FLAG_ALWAYS, nullptr, &sfArray);
if (FAILED(hres))
{
continue;
}
// Find safe array boundaries
SafeArrayGetLBound(sfArray, 1, &lstart);
SafeArrayGetUBound(sfArray, 1, &lend);
BSTR* pbstr;
hres = SafeArrayAccessData(sfArray, (void HUGEP**) &pbstr);
nIdx = 0;
if (FAILED(hres))
{
continue;
}
{
CIMTYPE pType;
QueryObj item;
for (nIdx = lstart; nIdx < lend; nIdx++)
{
hres = pclsObj->Get(pbstr[nIdx], 0, &vtProp, &pType, 0);
if (FAILED(hres))
{
continue;
}
if (vtProp.vt == VT_NULL)
{
continue;
}
if ((pType == CIM_STRING || pType == CIM_REFERENCE) && pType != CIM_EMPTY && pType != CIM_ILLEGAL)
{
item.emplace(utf8_encode(pbstr[nIdx]), utf8_encode(vtProp.bstrVal));
}
VariantClear(&vtProp);
}
hres = pclsObj->Get(L"Dependent", 0, &vtProp, &pType, nullptr);
if (pType != CIM_EMPTY && pType != CIM_ILLEGAL && SUCCEEDED(hres))
{
item.emplace(utf8_encode(pbstr[nIdx]), utf8_encode(vtProp.bstrVal));
}
// Push item to vector
queryVectorOut.emplace_back(item);
// Empty sfArray
SafeArrayUnaccessData(sfArray);
SafeArrayDestroy(sfArray); // Delete sfArray
sfArray = nullptr; // Avoid dangling pointers
}
_NextElement:
VariantClear(&vtProp); // Clear vtProp
pclsObj->Release(); // Release pclsObj
}
pEnumerator->Release();
pEnumerator = nullptr;
return S_OK;
}