qtmobility/src/systeminfo/qwmihelper_win.cpp
changeset 1 2b40d63a9c3d
child 4 90517678cc4f
equal deleted inserted replaced
0:cfcbf08528c4 1:2b40d63a9c3d
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 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 <QtCore/qmutex.h>
       
    51 #include <QtCore/private/qmutexpool_p.h>
       
    52 #include <QUuid>
       
    53 
       
    54 QTM_BEGIN_NAMESPACE
       
    55 
       
    56 WMIHelper::WMIHelper(QObject * parent)
       
    57         : QObject(parent)
       
    58 {
       
    59    m_conditional = QString();
       
    60 }
       
    61 
       
    62 WMIHelper::~WMIHelper()
       
    63 {
       
    64     CoUninitialize();
       
    65 }
       
    66 
       
    67 QVariant WMIHelper::getWMIData()
       
    68 {
       
    69    if (!m_wmiNamespace.isEmpty() && !m_className.isEmpty() && !m_classProperties.isEmpty()) {
       
    70       return getWMIData(m_wmiNamespace, m_className, m_classProperties);
       
    71    }
       
    72    return QVariant();
       
    73 }
       
    74 
       
    75 void WMIHelper::initializeWMI(const QString &wmiNamespace)
       
    76 {
       
    77     HRESULT hres;
       
    78     wbemLocator = 0;
       
    79 
       
    80     QUuid wbemLocatorClsid = "4590f811-1d3a-11d0-891f-00aa004b2e24";
       
    81     QUuid wbemLocatorIid = "dc12a687-737f-11cf-884d-00aa004b2e24";
       
    82 
       
    83     hres = CoCreateInstance(wbemLocatorClsid,0,CLSCTX_INPROC_SERVER,
       
    84                             wbemLocatorIid, (LPVOID *) &wbemLocator);
       
    85 
       
    86     if (hres == CO_E_NOTINITIALIZED) { // COM was not initialized
       
    87         CoInitializeEx(0, COINIT_MULTITHREADED);
       
    88         hres = CoCreateInstance(wbemLocatorClsid,0,CLSCTX_INPROC_SERVER,
       
    89                                 wbemLocatorIid, (LPVOID *) &wbemLocator);
       
    90     }
       
    91 
       
    92     if (hres != S_OK) {
       
    93        qWarning() << "Failed to create IWbemLocator object." << hres;
       
    94         return ;
       
    95     }
       
    96     wbemServices = 0;
       
    97     hres = wbemLocator->ConnectServer(::SysAllocString(wmiNamespace.utf16()),0,0,0,0,0,0,&wbemServices);
       
    98 
       
    99     if (hres != WBEM_S_NO_ERROR){
       
   100         qWarning() << "Could not connect";
       
   101         return ;
       
   102     }
       
   103 
       
   104     hres = CoSetProxyBlanket( wbemServices, RPC_C_AUTHN_WINNT, RPC_C_AUTHZ_NONE, 0,
       
   105                               RPC_C_AUTHN_LEVEL_CALL, RPC_C_IMP_LEVEL_IMPERSONATE, 0, EOAC_NONE );
       
   106 
       
   107     if (hres != S_OK) {
       
   108        qWarning() << "Could not set proxy blanket" << hres;
       
   109         return ;
       
   110     }
       
   111 }
       
   112 
       
   113 QVariant WMIHelper::getWMIData(const QString &wmiNamespace, const QString &className, const QStringList &classProperty)
       
   114 {
       
   115     initializeWMI(wmiNamespace);
       
   116     HRESULT hres;
       
   117     QVariant returnVariant;
       
   118 
       
   119       wbemEnumerator = 0;
       
   120 
       
   121     if (!m_conditional.isEmpty()) {
       
   122         if (m_conditional.left(1) != " ") {
       
   123             m_conditional.prepend(" ");
       
   124         }
       
   125     }
       
   126 
       
   127     QString aString = "SELECT * FROM " + className + m_conditional;
       
   128     BSTR bstrQuery;
       
   129     bstrQuery = ::SysAllocString(aString.utf16());
       
   130 
       
   131     hres = wbemServices->ExecQuery(L"WQL", bstrQuery,
       
   132             WBEM_FLAG_BIDIRECTIONAL | WBEM_FLAG_RETURN_IMMEDIATELY,0,&wbemEnumerator);
       
   133 
       
   134     if (hres != WBEM_S_NO_ERROR){
       
   135         qWarning() << "WMI Query failed.";
       
   136         wbemLocator->Release();
       
   137         wbemEnumerator->Release();
       
   138         return returnVariant;
       
   139     }
       
   140 
       
   141     ::SysFreeString(bstrQuery);
       
   142 
       
   143     wbemCLassObject = 0;
       
   144     ULONG result = 0;
       
   145 
       
   146     wmiVariantList.clear();
       
   147     while (wbemEnumerator) {
       
   148         HRESULT hr = wbemEnumerator->Next(WBEM_INFINITE, 1,&wbemCLassObject, &result);
       
   149         if(0 == result){
       
   150             break;
       
   151         }
       
   152 
       
   153         foreach(QString property, classProperty) {
       
   154             VARIANT msVariant;
       
   155             CIMTYPE variantType;
       
   156             hr = wbemCLassObject->Get(property.utf16(), 0, &msVariant, &variantType, 0);
       
   157             returnVariant = msVariantToQVariant(msVariant, variantType);
       
   158             wmiVariantList << returnVariant;
       
   159 
       
   160             VariantClear(&msVariant);
       
   161 
       
   162         }
       
   163 
       
   164         wbemCLassObject->Release();
       
   165     }
       
   166 
       
   167     wbemEnumerator->Release();
       
   168     wbemLocator->Release();
       
   169     wbemServices->Release();
       
   170     return returnVariant;
       
   171 }
       
   172 
       
   173 QVariant WMIHelper::msVariantToQVariant(VARIANT msVariant, CIMTYPE variantType)
       
   174 {
       
   175     QVariant returnVariant;
       
   176     switch(variantType) {
       
   177     case CIM_STRING:
       
   178     case CIM_CHAR16:
       
   179         {
       
   180             QString str((QChar*)msVariant.bstrVal, wcslen(msVariant.bstrVal));
       
   181             QVariant vs(str);
       
   182             returnVariant = vs;
       
   183         }
       
   184         break;
       
   185     case CIM_BOOLEAN:
       
   186         {
       
   187             QVariant vb(msVariant.boolVal);
       
   188             returnVariant = vb;
       
   189         }
       
   190         break;
       
   191             case CIM_UINT8:
       
   192         {
       
   193             QVariant vb(msVariant.uintVal);
       
   194             returnVariant = vb;
       
   195         }
       
   196         break;
       
   197             case CIM_UINT16:
       
   198         {
       
   199             QVariant vb(msVariant.uintVal);
       
   200             returnVariant = vb;
       
   201         }
       
   202             case CIM_UINT32:
       
   203         {
       
   204             QVariant vb(msVariant.uintVal);
       
   205             returnVariant = vb;
       
   206         }
       
   207         break;
       
   208     };
       
   209     VariantClear(&msVariant);
       
   210     return returnVariant;
       
   211 }
       
   212 
       
   213   void WMIHelper::setWmiNamespace(const QString &wmiNamespace)
       
   214 {
       
   215    m_wmiNamespace = wmiNamespace;
       
   216 }
       
   217 
       
   218    void WMIHelper::setClassName(const QString &className)
       
   219 {
       
   220    m_className = className;
       
   221 }
       
   222 
       
   223 void WMIHelper::setClassProperty(const QStringList &classProperties)
       
   224 {
       
   225    m_classProperties = classProperties;
       
   226 }
       
   227 
       
   228 void WMIHelper::setConditional(const QString &conditional)
       
   229 {
       
   230    m_conditional = conditional;
       
   231 }
       
   232 
       
   233 QTM_END_NAMESPACE
       
   234 #endif