| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2018 The Qt Company Ltd and/or its subsidiary(-ies). |
| 4 | ** Contact: http://www.qt-project.org/legal |
| 5 | ** |
| 6 | ** This file is part of the QtSensors module of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU Lesser General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 21 | ** packaging of this file. Please review the following information to |
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 24 | ** |
| 25 | ** GNU General Public License Usage |
| 26 | ** Alternatively, this file may be used under the terms of the GNU |
| 27 | ** General Public License version 2.0 or (at your option) the GNU General |
| 28 | ** Public license version 3 or any later version approved by the KDE Free |
| 29 | ** Qt Foundation. The licenses are as published by the Free Software |
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 31 | ** included in the packaging of this file. Please review the following |
| 32 | ** information to ensure the GNU General Public License requirements will |
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
| 35 | ** |
| 36 | ** $QT_END_LICENSE$ |
| 37 | ** |
| 38 | ****************************************************************************/ |
| 39 | |
| 40 | #include <QtCore/QtGlobal> // for Q_CC_MINGW |
| 41 | #if !defined (Q_CC_MINGW) || defined(__MINGW64_VERSION_MAJOR) |
| 42 | #define _WIN32_DCOM |
| 43 | |
| 44 | #include "qwmihelper_win_p.h" |
| 45 | |
| 46 | #include <QDebug> |
| 47 | #include <objbase.h> |
| 48 | #include <wbemidl.h> |
| 49 | #include <oleauto.h> |
| 50 | #include <QStringList> |
| 51 | #include <QUuid> |
| 52 | #include <comutil.h> |
| 53 | |
| 54 | QT_BEGIN_NAMESPACE |
| 55 | |
| 56 | Q_GLOBAL_STATIC(WMIHelper, wmihelper) |
| 57 | |
| 58 | WMIHelper::WMIHelper(QObject * parent) |
| 59 | : QObject(parent),initialized(0) |
| 60 | { |
| 61 | m_conditional = QString(); |
| 62 | } |
| 63 | |
| 64 | WMIHelper::~WMIHelper() |
| 65 | { |
| 66 | CoUninitialize(); |
| 67 | } |
| 68 | |
| 69 | WMIHelper *WMIHelper::instance() |
| 70 | { |
| 71 | return wmihelper(); |
| 72 | } |
| 73 | |
| 74 | QVariant WMIHelper::getWMIData() |
| 75 | { |
| 76 | // qDebug() << Q_FUNC_INFO << m_wmiNamespace << m_className << m_classProperties; |
| 77 | |
| 78 | if (!m_wmiNamespace.isEmpty() && !m_className.isEmpty() && !m_classProperties.isEmpty()) { |
| 79 | return getWMIData(m_wmiNamespace, m_className, m_classProperties); |
| 80 | } |
| 81 | return QVariant(); |
| 82 | } |
| 83 | |
| 84 | void WMIHelper::initializeWMI(const QString &wmiNamespace) |
| 85 | { |
| 86 | if (initialized) { |
| 87 | wbemLocator = 0; |
| 88 | } |
| 89 | HRESULT hres; |
| 90 | wbemLocator = 0; |
| 91 | |
| 92 | QUuid wbemLocatorClsid = "4590f811-1d3a-11d0-891f-00aa004b2e24"; |
| 93 | QUuid wbemLocatorIid = "dc12a687-737f-11cf-884d-00aa004b2e24"; |
| 94 | |
| 95 | hres = CoCreateInstance(wbemLocatorClsid,0,CLSCTX_INPROC_SERVER, |
| 96 | wbemLocatorIid, (LPVOID *) &wbemLocator); |
| 97 | |
| 98 | if (hres == CO_E_NOTINITIALIZED) { // COM was not initialized |
| 99 | CoInitializeEx(0, COINIT_MULTITHREADED); |
| 100 | hres = CoCreateInstance(wbemLocatorClsid,0,CLSCTX_INPROC_SERVER, |
| 101 | wbemLocatorIid, (LPVOID *) &wbemLocator); |
| 102 | } |
| 103 | |
| 104 | if (hres != S_OK) { |
| 105 | qWarning() << "Failed to create IWbemLocator object." << hres; |
| 106 | return ; |
| 107 | } |
| 108 | wbemServices = 0; |
| 109 | const OLECHAR *ole = reinterpret_cast<const OLECHAR *>(wmiNamespace.utf16()); |
| 110 | hres = wbemLocator->ConnectServer(::SysAllocString(ole),0,0,0,0,0,0,&wbemServices); |
| 111 | |
| 112 | if (hres != WBEM_S_NO_ERROR){ |
| 113 | qWarning() << "Could not connect"; |
| 114 | return ; |
| 115 | } |
| 116 | |
| 117 | hres = CoSetProxyBlanket( wbemServices, RPC_C_AUTHN_WINNT, RPC_C_AUTHZ_NONE, 0, |
| 118 | RPC_C_AUTHN_LEVEL_CALL, RPC_C_IMP_LEVEL_IMPERSONATE, 0, EOAC_NONE ); |
| 119 | |
| 120 | if (hres != S_OK) { |
| 121 | qWarning() << "Could not set proxy blanket" << hres; |
| 122 | return ; |
| 123 | } |
| 124 | initialized = true; |
| 125 | } |
| 126 | |
| 127 | QVariant WMIHelper::getWMIData(const QString &wmiNamespace, const QString &className, const QStringList &classProperty) |
| 128 | { |
| 129 | initializeWMI(wmiNamespace); |
| 130 | HRESULT hres; |
| 131 | QVariant returnVariant; |
| 132 | |
| 133 | wbemEnumerator = 0; |
| 134 | |
| 135 | if (!m_conditional.isEmpty() && !m_conditional.startsWith(QLatin1Char(' '))) |
| 136 | m_conditional.prepend(QLatin1Char(' ')); |
| 137 | |
| 138 | const QString aString = QStringLiteral("SELECT * FROM ") + className + m_conditional; |
| 139 | BSTR bstrQuery; |
| 140 | const OLECHAR *oleStr = reinterpret_cast<const OLECHAR *>(aString.utf16()); |
| 141 | |
| 142 | bstrQuery = ::SysAllocString(oleStr); |
| 143 | |
| 144 | BSTR wql = SysAllocString(L"WQL"); |
| 145 | hres = wbemServices->ExecQuery(wql, bstrQuery, |
| 146 | WBEM_FLAG_BIDIRECTIONAL | WBEM_FLAG_RETURN_IMMEDIATELY,0,&wbemEnumerator); |
| 147 | |
| 148 | SysFreeString(wql); |
| 149 | |
| 150 | if (hres != WBEM_S_NO_ERROR){ |
| 151 | qWarning() << "WMI Query failed."; |
| 152 | wbemLocator->Release(); |
| 153 | wbemEnumerator->Release(); |
| 154 | return returnVariant; |
| 155 | } |
| 156 | |
| 157 | ::SysFreeString(bstrQuery); |
| 158 | |
| 159 | wbemCLassObject = 0; |
| 160 | ULONG result = 0; |
| 161 | |
| 162 | wmiVariantList.clear(); |
| 163 | while (wbemEnumerator) { |
| 164 | HRESULT hr = wbemEnumerator->Next(WBEM_INFINITE, 1,&wbemCLassObject, &result); |
| 165 | if (0 == result){ |
| 166 | break; |
| 167 | } |
| 168 | |
| 169 | foreach (const QString property, classProperty) { |
| 170 | VARIANT msVariant; |
| 171 | CIMTYPE variantType; |
| 172 | hr = wbemCLassObject->Get(reinterpret_cast<const wchar_t *>(property.utf16()), 0, &msVariant, &variantType, 0); |
| 173 | returnVariant = msVariantToQVariant(msVariant, variantType); |
| 174 | wmiVariantList << returnVariant; |
| 175 | |
| 176 | VariantClear(&msVariant); |
| 177 | |
| 178 | } |
| 179 | |
| 180 | wbemCLassObject->Release(); |
| 181 | } |
| 182 | |
| 183 | wbemEnumerator->Release(); |
| 184 | wbemLocator->Release(); |
| 185 | wbemServices->Release(); |
| 186 | return returnVariant; |
| 187 | } |
| 188 | |
| 189 | QVariant WMIHelper::msVariantToQVariant(VARIANT msVariant, CIMTYPE variantType) |
| 190 | { |
| 191 | QVariant returnVariant; |
| 192 | switch (variantType) { |
| 193 | case CIM_STRING: |
| 194 | case CIM_CHAR16: |
| 195 | { |
| 196 | QString str((QChar*)msVariant.bstrVal, int(wcslen(msVariant.bstrVal))); |
| 197 | QVariant vs(str); |
| 198 | returnVariant = vs; |
| 199 | } |
| 200 | break; |
| 201 | case CIM_BOOLEAN: |
| 202 | { |
| 203 | QVariant vb(msVariant.boolVal); |
| 204 | returnVariant = vb; |
| 205 | } |
| 206 | break; |
| 207 | case CIM_UINT8: |
| 208 | { |
| 209 | QVariant vb(msVariant.uintVal); |
| 210 | returnVariant = vb; |
| 211 | } |
| 212 | break; |
| 213 | case CIM_UINT16: |
| 214 | { |
| 215 | QVariant vb(msVariant.uintVal); |
| 216 | returnVariant = vb; |
| 217 | } |
| 218 | case CIM_UINT32: |
| 219 | { |
| 220 | QVariant vb(msVariant.uintVal); |
| 221 | returnVariant = vb; |
| 222 | } |
| 223 | break; |
| 224 | case CIM_UINT64: |
| 225 | { |
| 226 | QVariant vb(msVariant.uintVal); |
| 227 | returnVariant = vb; |
| 228 | } |
| 229 | break; |
| 230 | }; |
| 231 | VariantClear(&msVariant); |
| 232 | return returnVariant; |
| 233 | } |
| 234 | |
| 235 | void WMIHelper::setWmiNamespace(const QString &wmiNamespace) |
| 236 | { |
| 237 | m_wmiNamespace = wmiNamespace; |
| 238 | } |
| 239 | |
| 240 | void WMIHelper::setClassName(const QString &className) |
| 241 | { |
| 242 | m_className = className; |
| 243 | } |
| 244 | |
| 245 | void WMIHelper::setClassProperty(const QStringList &classProperties) |
| 246 | { |
| 247 | m_classProperties = classProperties; |
| 248 | } |
| 249 | |
| 250 | void WMIHelper::setConditional(const QString &conditional) |
| 251 | { |
| 252 | m_conditional = conditional; |
| 253 | } |
| 254 | |
| 255 | void WMIHelper::setupNotfication(const QString &wmiNamespace,const QString &className, const QStringList &/*classProperties*/) |
| 256 | { |
| 257 | initializeWMI(wmiNamespace); |
| 258 | HRESULT hres; |
| 259 | |
| 260 | IUnsecuredApartment* pUnsecApp = NULL; |
| 261 | |
| 262 | QUuid clsidUnsecuredApartment = "49bd2028-1523-11d1-ad79-00c04fd8fdff"; |
| 263 | QUuid iidUnsecuredApartment = "1cfaba8c-1523-11d1-ad79-00c04fd8fdff"; |
| 264 | |
| 265 | hres = CoCreateInstance(clsidUnsecuredApartment, NULL, |
| 266 | CLSCTX_LOCAL_SERVER,iidUnsecuredApartment, |
| 267 | (void**)&pUnsecApp); |
| 268 | |
| 269 | if (hres == CO_E_NOTINITIALIZED) { // COM was not initialized |
| 270 | qDebug() << "not initialized"; |
| 271 | CoInitializeEx(0, COINIT_MULTITHREADED); |
| 272 | hres = CoCreateInstance(clsidUnsecuredApartment,0,CLSCTX_INPROC_SERVER, |
| 273 | iidUnsecuredApartment, (LPVOID *) &pUnsecApp); |
| 274 | } |
| 275 | |
| 276 | if (hres != S_OK) { |
| 277 | qDebug() << "Failed to create IWbemLocator object." << hres; |
| 278 | return ; |
| 279 | } |
| 280 | |
| 281 | EventSink* pSink = new EventSink; |
| 282 | |
| 283 | pSink->AddRef(); |
| 284 | |
| 285 | IUnknown* pStubUnk = NULL; |
| 286 | pUnsecApp->CreateObjectStub(pSink, &pStubUnk); |
| 287 | |
| 288 | IWbemObjectSink* pStubSink = NULL; |
| 289 | QUuid iidWbemObjectSink = "7c857801-7381-11cf-884d-00aa004b2e24"; |
| 290 | |
| 291 | pStubUnk->QueryInterface(iidWbemObjectSink,(void **) &pStubSink); |
| 292 | |
| 293 | QString aString = className;//"SELECT * FROM __InstanceOperationEvent WITHIN 1 WHERE (TargetInstance ISA 'Win32_LogicalDisk') AND (TargetInstance.DriveType = 5 OR TargetInstance.DriveType = 2)"; |
| 294 | |
| 295 | BSTR bstrQuery; |
| 296 | |
| 297 | const OLECHAR *oleStr = reinterpret_cast<const OLECHAR *>(aString.utf16()); |
| 298 | |
| 299 | bstrQuery = ::SysAllocString(oleStr); |
| 300 | |
| 301 | BSTR wql = SysAllocString(L"WQL"); |
| 302 | |
| 303 | hres = wbemServices->ExecNotificationQueryAsync( |
| 304 | wql, |
| 305 | bstrQuery, |
| 306 | WBEM_FLAG_SEND_STATUS, |
| 307 | NULL, |
| 308 | pStubSink); |
| 309 | |
| 310 | SysFreeString(wql); |
| 311 | |
| 312 | if (FAILED(hres)) { |
| 313 | printf("ExecNotificationQueryAsync failed " |
| 314 | "with = 0x%X\n", hres); |
| 315 | wbemLocator->Release(); |
| 316 | wbemEnumerator->Release(); |
| 317 | pUnsecApp->Release(); |
| 318 | pStubUnk->Release(); |
| 319 | pSink->Release(); |
| 320 | pStubSink->Release(); |
| 321 | CoUninitialize(); |
| 322 | return; |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | void WMIHelper::emitNotificationArrived() |
| 327 | { |
| 328 | emit wminotificationArrived(); |
| 329 | } |
| 330 | |
| 331 | ULONG EventSink::AddRef() |
| 332 | { |
| 333 | return InterlockedIncrement(&m_lRef); |
| 334 | } |
| 335 | |
| 336 | ULONG EventSink::Release() |
| 337 | { |
| 338 | LONG lRef = InterlockedDecrement(&m_lRef); |
| 339 | if (lRef == 0) |
| 340 | delete this; |
| 341 | return lRef; |
| 342 | } |
| 343 | |
| 344 | HRESULT EventSink::QueryInterface(REFIID riid, void** ppv) |
| 345 | { |
| 346 | QUuid iidWbemObjectSink = "7c857801-7381-11cf-884d-00aa004b2e24"; |
| 347 | |
| 348 | if (riid == IID_IUnknown || riid == iidWbemObjectSink){ |
| 349 | *ppv = (IWbemObjectSink *) this; |
| 350 | AddRef(); |
| 351 | return WBEM_S_NO_ERROR; |
| 352 | } |
| 353 | else return E_NOINTERFACE; |
| 354 | } |
| 355 | |
| 356 | |
| 357 | HRESULT EventSink::Indicate(long lObjectCount, |
| 358 | IWbemClassObject ** /*apObjArray*/) |
| 359 | { |
| 360 | for (int i = 0; i < lObjectCount; i++){ |
| 361 | WMIHelper::instance()->emitNotificationArrived(); |
| 362 | |
| 363 | // _variant_t msVariant; |
| 364 | // CIMTYPE variantType; |
| 365 | // QVariant returnVariant; |
| 366 | // IWbemClassObject *obj = apObjArray[0]; |
| 367 | |
| 368 | // HRESULT hr = obj->Get(L"TargetInstance", 0, &msVariant, 0, 0); |
| 369 | // if (hr == S_OK) { |
| 370 | // IUnknown *str = msVariant; |
| 371 | // VariantClear(&msVariant); |
| 372 | |
| 373 | // IWbemClassObject *obj; |
| 374 | // hr = str->QueryInterface(IID_IWbemClassObject, reinterpret_cast<void **>(&obj)); |
| 375 | // if (hr == S_OK) { |
| 376 | // _variant_t var2; |
| 377 | // hr = obj->Get(L"__Class", 0, &var2, 0, 0); |
| 378 | // _bstr_t classname = var2; |
| 379 | // VariantClear(&var2); |
| 380 | // if (classname == _bstr_t(L"Win32_LogicalDisk")) { |
| 381 | // _variant_t var3; |
| 382 | // // qDebug() << obj->Availability; |
| 383 | // hr = obj->Get(L"Availability", 0, &var3, &variantType, 0); |
| 384 | // if (hr == S_OK) { |
| 385 | // //uint av = var3; |
| 386 | // //returnVariant = WMIHelper::msVariantToQVariant(msVariant, variantType); |
| 387 | |
| 388 | // printf("variant type: 0x%X\n", variantType); |
| 389 | |
| 390 | // // int value = var3.intVal; |
| 391 | |
| 392 | // qDebug() <<"Availability" << var3.uintVal << var3.vt << var3.uiVal << var3.iVal; |
| 393 | // //var3.uintVal;//<<returnVariant << returnVariant.toUInt(); |
| 394 | // VariantClear(&var3); |
| 395 | // } else { |
| 396 | // qDebug() << hr << GetLastError(); |
| 397 | // printf("Error foo. hr = 0x%X\n", hr); |
| 398 | |
| 399 | // } |
| 400 | // } else { |
| 401 | // qDebug() << hr << GetLastError(); |
| 402 | // printf("Error 2. hr = 0x%X\n", hr); |
| 403 | |
| 404 | // } |
| 405 | // } |
| 406 | // } else { |
| 407 | // qDebug() << hr << GetLastError(); |
| 408 | // printf("Error. hResult = 0x%X\n", hr); |
| 409 | // } |
| 410 | } |
| 411 | |
| 412 | return WBEM_S_NO_ERROR; |
| 413 | } |
| 414 | |
| 415 | HRESULT EventSink::SetStatus( |
| 416 | LONG lFlags, |
| 417 | HRESULT hResult, |
| 418 | BSTR /*strParam*/, |
| 419 | IWbemClassObject __RPC_FAR * /*pObjParam*/) |
| 420 | { |
| 421 | if (lFlags == WBEM_STATUS_COMPLETE) { |
| 422 | printf("Call complete. hResult = 0x%X\n", hResult); |
| 423 | } else if (lFlags == WBEM_STATUS_PROGRESS) { |
| 424 | printf("Call in progress.\n"); |
| 425 | } |
| 426 | |
| 427 | return WBEM_S_NO_ERROR; |
| 428 | } // end of EventSink.cpp |
| 429 | |
| 430 | #include "moc_qwmihelper_win_p.cpp" |
| 431 | |
| 432 | QT_END_NAMESPACE |
| 433 | #endif |