2 * Copyright (C) 2005-2013 Team XBMC
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)
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.
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/>.
22 #include "WMIInterface.h"
29 CWIN32Wmi::CWIN32Wmi(void)
36 CWIN32Wmi::~CWIN32Wmi(void)
41 bool CWIN32Wmi::Connect()
43 // Initialize COM. ------------------------------------------
45 hres = CoInitializeEx(0, COINIT_MULTITHREADED);
48 return false; // Program has failed.
51 hres = CoInitializeSecurity(
53 -1, // COM authentication
54 NULL, // Authentication services
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
65 return false; // Program has failed.
70 hres = CoCreateInstance(
74 IID_IWbemLocator, (LPVOID *) &pLoc);
78 return false; // Program has failed.
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)
94 &pSvc // pointer to IWbemServices proxy
101 return false; // Program has failed.
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
120 return false; // Program has failed.
129 void CWIN32Wmi::Release()
135 if(pEnumerator != NULL)
136 pEnumerator->Release();
149 void CWIN32Wmi::testquery()
151 hres = pSvc->ExecQuery(
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,
162 return; // Program has failed.
168 HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1,
177 VariantInit(&vtProp);
179 // Get the value of the Name property
180 //hr = pclsObj->Get(L"Description", 0, &vtProp, 0, 0);
182 vtProp.bstrVal = bstr_t("192.168.1.209");
183 hr = pclsObj->Put(L"IPAddress",0,&vtProp,0);
184 VariantClear(&vtProp);
191 std::vector<CStdString> CWIN32Wmi::GetWMIStrVector(CStdString& strQuery, CStdStringW& strProperty)
193 std::vector<CStdString> strResult;
200 hres = pSvc->ExecQuery(
202 bstr_t(strQuery.c_str()),
203 WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
209 return strResult; // Program has failed.
215 HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1,
224 VariantInit(&vtProp);
226 hr = pclsObj->Get(strProperty.c_str(), 0, &vtProp, 0, 0);
227 strResult.push_back(vtProp.bstrVal);
228 VariantClear(&vtProp);
230 if(pEnumerator != NULL)
231 pEnumerator->Release();
239 CStdString CWIN32Wmi::GetWMIString(CStdString& strQuery, CStdStringW& strProperty)
241 return GetWMIStrVector(strQuery, strProperty)[0];