PageRenderTime 34ms CodeModel.GetById 2ms app.highlight 29ms RepoModel.GetById 1ms app.codeStats 0ms

/xbmc/win32/WMIInterface.cpp

http://github.com/xbmc/xbmc
C++ | 242 lines | 175 code | 39 blank | 28 comment | 26 complexity | 380c33729eef86f8029278e2b69eed1d MD5 | raw file
  1/*
  2 *      Copyright (C) 2005-2013 Team XBMC
  3 *      http://xbmc.org
  4 *
  5 *  This Program is free software; you can redistribute it and/or modify
  6 *  it under the terms of the GNU General Public License as published by
  7 *  the Free Software Foundation; either version 2, or (at your option)
  8 *  any later version.
  9 *
 10 *  This Program is distributed in the hope that it will be useful,
 11 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 12 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 13 *  GNU General Public License for more details.
 14 *
 15 *  You should have received a copy of the GNU General Public License
 16 *  along with XBMC; see the file COPYING.  If not, see
 17 *  <http://www.gnu.org/licenses/>.
 18 *
 19 */
 20
 21#include "stdafx.h"
 22#include "WMIInterface.h"
 23#include "../Util.h"
 24
 25using namespace std;
 26
 27
 28
 29CWIN32Wmi::CWIN32Wmi(void)
 30{
 31  bconnected = false;
 32  pclsObj = NULL;
 33  Connect();
 34}
 35
 36CWIN32Wmi::~CWIN32Wmi(void)
 37{
 38  Release();
 39}
 40
 41bool CWIN32Wmi::Connect()
 42{
 43  // Initialize COM. ------------------------------------------
 44
 45  hres =  CoInitializeEx(0, COINIT_MULTITHREADED);
 46  if (FAILED(hres))
 47  {
 48      return false;                  // Program has failed.
 49  }
 50
 51	hres =  CoInitializeSecurity(
 52        NULL,
 53        -1,                          // COM authentication
 54        NULL,                        // Authentication services
 55        NULL,                        // Reserved
 56        RPC_C_AUTHN_LEVEL_DEFAULT,   // Default authentication
 57        RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation
 58        NULL,                        // Authentication info
 59        EOAC_NONE,                   // Additional capabilities
 60        NULL                         // Reserved
 61        );
 62
 63	if (FAILED(hres))
 64  {
 65      return false;                    // Program has failed.
 66  }
 67
 68	pLoc = NULL;
 69
 70  hres = CoCreateInstance(
 71      CLSID_WbemLocator,
 72      0,
 73      CLSCTX_INPROC_SERVER,
 74      IID_IWbemLocator, (LPVOID *) &pLoc);
 75
 76  if (FAILED(hres))
 77  {
 78      return false;                 // Program has failed.
 79  }
 80
 81	pSvc = NULL;
 82
 83  // Connect to the root\cimv2 namespace with
 84  // the current user and obtain pointer pSvc
 85  // to make IWbemServices calls.
 86  hres = pLoc->ConnectServer(
 87       _bstr_t(L"ROOT\\CIMV2"), // Object path of WMI namespace
 88       NULL,                    // User name. NULL = current user
 89       NULL,                    // User password. NULL = current
 90       0,                       // Locale. NULL indicates current
 91       NULL,                    // Security flags.
 92       0,                       // Authority (e.g. Kerberos)
 93       0,                       // Context object
 94       &pSvc                    // pointer to IWbemServices proxy
 95       );
 96
 97  if (FAILED(hres))
 98  {
 99      pLoc->Release();
100      CoUninitialize();
101      return false;                // Program has failed.
102  }
103
104	hres = CoSetProxyBlanket(
105       pSvc,                        // Indicates the proxy to set
106       RPC_C_AUTHN_WINNT,           // RPC_C_AUTHN_xxx
107       RPC_C_AUTHZ_NONE,            // RPC_C_AUTHZ_xxx
108       NULL,                        // Server principal name
109       RPC_C_AUTHN_LEVEL_CALL,      // RPC_C_AUTHN_LEVEL_xxx
110       RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx
111       NULL,                        // client identity
112       EOAC_NONE                    // proxy capabilities
113    );
114
115    if (FAILED(hres))
116    {
117        pSvc->Release();
118        pLoc->Release();
119        CoUninitialize();
120        return false;               // Program has failed.
121    }
122
123		pEnumerator = NULL;
124
125    bconnected = true;
126    return true;
127}
128
129void CWIN32Wmi::Release()
130{
131  if(pSvc != NULL)
132    pSvc->Release();
133  if(pLoc != NULL)
134    pLoc->Release();
135  if(pEnumerator != NULL)
136    pEnumerator->Release();
137  if(pclsObj != NULL)
138    pclsObj->Release();
139
140  CoUninitialize();
141
142  bconnected = false;
143  pSvc = NULL;
144  pLoc = NULL;
145  pEnumerator = NULL;
146  pclsObj = NULL;
147}
148
149void CWIN32Wmi::testquery()
150{
151  hres = pSvc->ExecQuery(
152      bstr_t("WQL"),
153      //bstr_t("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled=TRUE"),
154      //bstr_t("SELECT * FROM Win32_NetworkAdapter WHERE PhysicalAdapter=TRUE"),
155      bstr_t("SELECT * FROM Win32_NetworkAdapter WHERE Description='Atheros AR5008X Wireless Network Adapter'"),
156      WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
157      NULL,
158      &pEnumerator);
159
160  if (FAILED(hres))
161  {
162      return;               // Program has failed.
163  }
164  ULONG uReturn = 0;
165
166  while (pEnumerator)
167  {
168      HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1,
169          &pclsObj, &uReturn);
170
171      if(0 == uReturn)
172      {
173          break;
174      }
175
176      VARIANT vtProp;
177      VariantInit(&vtProp);
178
179      // Get the value of the Name property
180      //hr = pclsObj->Get(L"Description", 0, &vtProp, 0, 0);
181
182      vtProp.bstrVal = bstr_t("192.168.1.209");
183      hr = pclsObj->Put(L"IPAddress",0,&vtProp,0);
184      VariantClear(&vtProp);
185			//iCpu++;
186  }
187	pclsObj->Release();
188  pclsObj = NULL;
189}
190
191std::vector<CStdString> CWIN32Wmi::GetWMIStrVector(CStdString& strQuery, CStdStringW& strProperty)
192{
193  std::vector<CStdString> strResult;
194  pEnumerator = NULL;
195  pclsObj = NULL;
196
197  if(!bconnected)
198    return strResult;
199
200  hres = pSvc->ExecQuery(
201      bstr_t("WQL"),
202      bstr_t(strQuery.c_str()),
203      WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
204      NULL,
205      &pEnumerator);
206
207  if (FAILED(hres))
208  {
209      return strResult;               // Program has failed.
210  }
211  ULONG uReturn = 0;
212
213  while (pEnumerator)
214  {
215      HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1,
216          &pclsObj, &uReturn);
217
218      if(0 == uReturn)
219      {
220          break;
221      }
222
223      VARIANT vtProp;
224      VariantInit(&vtProp);
225
226      hr = pclsObj->Get(strProperty.c_str(), 0, &vtProp, 0, 0);
227      strResult.push_back(vtProp.bstrVal);
228      VariantClear(&vtProp);
229  }
230  if(pEnumerator != NULL)
231    pEnumerator->Release();
232  pEnumerator = NULL;
233  if(pclsObj != NULL)
234	  pclsObj->Release();
235  pclsObj = NULL;
236  return strResult;
237}
238
239CStdString CWIN32Wmi::GetWMIString(CStdString& strQuery, CStdStringW& strProperty)
240{
241  return GetWMIStrVector(strQuery, strProperty)[0];
242}