src/systeminfo/qwmihelper_win.cpp
changeset 0 876b1a06bc25
equal deleted inserted replaced
-1:000000000000 0:876b1a06bc25
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the Qt Mobility Components.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #ifndef Q_CC_MINGW
       
    43 #define _WIN32_DCOM
       
    44 #include "qwmihelper_win_p.h"
       
    45 #include <QDebug>
       
    46 #include <ObjBase.h>
       
    47 #include <Wbemidl.h>
       
    48 #include <Oleauto.h>
       
    49 #include <QStringList>
       
    50 #include <QUuid>
       
    51 
       
    52 QTM_BEGIN_NAMESPACE
       
    53 
       
    54 WMIHelper::WMIHelper(QObject * parent)
       
    55         : QObject(parent)
       
    56 {
       
    57    m_conditional = QString();
       
    58 }
       
    59 
       
    60 WMIHelper::~WMIHelper()
       
    61 {
       
    62     CoUninitialize();
       
    63 }
       
    64 
       
    65 QVariant WMIHelper::getWMIData()
       
    66 {
       
    67    if (!m_wmiNamespace.isEmpty() && !m_className.isEmpty() && !m_classProperties.isEmpty()) {
       
    68       return getWMIData(m_wmiNamespace, m_className, m_classProperties);
       
    69    }
       
    70    return QVariant();
       
    71 }
       
    72 
       
    73 void WMIHelper::initializeWMI(const QString &wmiNamespace)
       
    74 {
       
    75     HRESULT hres;
       
    76     wbemLocator = 0;
       
    77 
       
    78     QUuid wbemLocatorClsid = "4590f811-1d3a-11d0-891f-00aa004b2e24";
       
    79     QUuid wbemLocatorIid = "dc12a687-737f-11cf-884d-00aa004b2e24";
       
    80 
       
    81     hres = CoCreateInstance(wbemLocatorClsid,0,CLSCTX_INPROC_SERVER,
       
    82                             wbemLocatorIid, (LPVOID *) &wbemLocator);
       
    83 
       
    84     if (hres == CO_E_NOTINITIALIZED) { // COM was not initialized
       
    85         CoInitializeEx(0, COINIT_MULTITHREADED);
       
    86         hres = CoCreateInstance(wbemLocatorClsid,0,CLSCTX_INPROC_SERVER,
       
    87                                 wbemLocatorIid, (LPVOID *) &wbemLocator);
       
    88     }
       
    89 
       
    90     if (hres != S_OK) {
       
    91        qWarning() << "Failed to create IWbemLocator object." << hres;
       
    92         return ;
       
    93     }
       
    94     wbemServices = 0;
       
    95     hres = wbemLocator->ConnectServer(::SysAllocString(wmiNamespace.utf16()),0,0,0,0,0,0,&wbemServices);
       
    96 
       
    97     if (hres != WBEM_S_NO_ERROR){
       
    98         qWarning() << "Could not connect";
       
    99         return ;
       
   100     }
       
   101 
       
   102     hres = CoSetProxyBlanket( wbemServices, RPC_C_AUTHN_WINNT, RPC_C_AUTHZ_NONE, 0,
       
   103                               RPC_C_AUTHN_LEVEL_CALL, RPC_C_IMP_LEVEL_IMPERSONATE, 0, EOAC_NONE );
       
   104 
       
   105     if (hres != S_OK) {
       
   106        qWarning() << "Could not set proxy blanket" << hres;
       
   107         return ;
       
   108     }
       
   109 }
       
   110 
       
   111 QVariant WMIHelper::getWMIData(const QString &wmiNamespace, const QString &className, const QStringList &classProperty)
       
   112 {
       
   113     initializeWMI(wmiNamespace);
       
   114     HRESULT hres;
       
   115     QVariant returnVariant;
       
   116 
       
   117       wbemEnumerator = 0;
       
   118 
       
   119     if (!m_conditional.isEmpty()) {
       
   120         if (m_conditional.left(1) != " ") {
       
   121             m_conditional.prepend(" ");
       
   122         }
       
   123     }
       
   124 
       
   125     QString aString = "SELECT * FROM " + className + m_conditional;
       
   126     BSTR bstrQuery;
       
   127     bstrQuery = ::SysAllocString(aString.utf16());
       
   128 
       
   129     hres = wbemServices->ExecQuery(L"WQL", bstrQuery,
       
   130             WBEM_FLAG_BIDIRECTIONAL | WBEM_FLAG_RETURN_IMMEDIATELY,0,&wbemEnumerator);
       
   131 
       
   132     if (hres != WBEM_S_NO_ERROR){
       
   133         qWarning() << "WMI Query failed.";
       
   134         wbemLocator->Release();
       
   135         wbemEnumerator->Release();
       
   136         return returnVariant;
       
   137     }
       
   138 
       
   139     ::SysFreeString(bstrQuery);
       
   140 
       
   141     wbemCLassObject = 0;
       
   142     ULONG result = 0;
       
   143 
       
   144     wmiVariantList.clear();
       
   145     while (wbemEnumerator) {
       
   146         HRESULT hr = wbemEnumerator->Next(WBEM_INFINITE, 1,&wbemCLassObject, &result);
       
   147         if(0 == result){
       
   148             break;
       
   149         }
       
   150 
       
   151         foreach(const QString property, classProperty) {
       
   152             VARIANT msVariant;
       
   153             CIMTYPE variantType;
       
   154             hr = wbemCLassObject->Get(property.utf16(), 0, &msVariant, &variantType, 0);
       
   155             returnVariant = msVariantToQVariant(msVariant, variantType);
       
   156             wmiVariantList << returnVariant;
       
   157 
       
   158             VariantClear(&msVariant);
       
   159 
       
   160         }
       
   161 
       
   162         wbemCLassObject->Release();
       
   163     }
       
   164 
       
   165     wbemEnumerator->Release();
       
   166     wbemLocator->Release();
       
   167     wbemServices->Release();
       
   168     return returnVariant;
       
   169 }
       
   170 
       
   171 QVariant WMIHelper::msVariantToQVariant(VARIANT msVariant, CIMTYPE variantType)
       
   172 {
       
   173     QVariant returnVariant;
       
   174     switch(variantType) {
       
   175     case CIM_STRING:
       
   176     case CIM_CHAR16:
       
   177         {
       
   178             QString str((QChar*)msVariant.bstrVal, wcslen(msVariant.bstrVal));
       
   179             QVariant vs(str);
       
   180             returnVariant = vs;
       
   181         }
       
   182         break;
       
   183     case CIM_BOOLEAN:
       
   184         {
       
   185             QVariant vb(msVariant.boolVal);
       
   186             returnVariant = vb;
       
   187         }
       
   188         break;
       
   189             case CIM_UINT8:
       
   190         {
       
   191             QVariant vb(msVariant.uintVal);
       
   192             returnVariant = vb;
       
   193         }
       
   194         break;
       
   195             case CIM_UINT16:
       
   196         {
       
   197             QVariant vb(msVariant.uintVal);
       
   198             returnVariant = vb;
       
   199         }
       
   200             case CIM_UINT32:
       
   201         {
       
   202             QVariant vb(msVariant.uintVal);
       
   203             returnVariant = vb;
       
   204         }
       
   205         break;
       
   206     };
       
   207     VariantClear(&msVariant);
       
   208     return returnVariant;
       
   209 }
       
   210 
       
   211   void WMIHelper::setWmiNamespace(const QString &wmiNamespace)
       
   212 {
       
   213    m_wmiNamespace = wmiNamespace;
       
   214 }
       
   215 
       
   216    void WMIHelper::setClassName(const QString &className)
       
   217 {
       
   218    m_className = className;
       
   219 }
       
   220 
       
   221 void WMIHelper::setClassProperty(const QStringList &classProperties)
       
   222 {
       
   223    m_classProperties = classProperties;
       
   224 }
       
   225 
       
   226 void WMIHelper::setConditional(const QString &conditional)
       
   227 {
       
   228    m_conditional = conditional;
       
   229 }
       
   230 
       
   231 QTM_END_NAMESPACE
       
   232 #endif